2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2020-02-02 21:49:41 +00:00
|
|
|
// Copyright (c) 2014 Jameson Little. MIT License.
|
2023-03-18 12:36:00 +00:00
|
|
|
// This module is browser compatible.
|
2022-08-11 11:51:20 +00:00
|
|
|
|
|
|
|
/**
|
2023-12-13 02:57:59 +00:00
|
|
|
* Utilities for
|
2024-05-20 07:14:09 +00:00
|
|
|
* {@link https://www.rfc-editor.org/rfc/rfc4648.html#section-6 | base32}
|
2023-12-13 02:57:59 +00:00
|
|
|
* encoding and decoding.
|
2022-08-11 11:51:20 +00:00
|
|
|
*
|
2023-12-13 02:57:59 +00:00
|
|
|
* Modified from {@link https://github.com/beatgammit/base64-js}.
|
2022-08-11 11:51:20 +00:00
|
|
|
*
|
2024-04-02 10:42:55 +00:00
|
|
|
* ```ts
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { encodeBase32, decodeBase32 } from "@std/encoding/base32";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2024-04-02 10:42:55 +00:00
|
|
|
*
|
2024-05-24 02:23:24 +00:00
|
|
|
* assertEquals(encodeBase32("foobar"), "MZXW6YTBOI======");
|
2024-04-02 10:42:55 +00:00
|
|
|
*
|
2024-05-24 02:23:24 +00:00
|
|
|
* assertEquals(
|
|
|
|
* decodeBase32("MZXW6YTBOI======"),
|
|
|
|
* new TextEncoder().encode("foobar")
|
|
|
|
* );
|
2024-04-02 10:42:55 +00:00
|
|
|
* ```
|
|
|
|
*
|
2022-08-11 11:51:20 +00:00
|
|
|
* @module
|
|
|
|
*/
|
2024-08-09 09:57:21 +00:00
|
|
|
import { decode, encode } from "./_base32_common.ts";
|
2024-01-25 14:08:29 +00:00
|
|
|
|
2024-02-24 20:24:08 +00:00
|
|
|
const lookup: string[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567".split("");
|
2020-02-02 21:49:41 +00:00
|
|
|
const revLookup: number[] = [];
|
2024-08-09 09:57:21 +00:00
|
|
|
lookup.forEach((c, i) => (revLookup[c.charCodeAt(0)] = i));
|
2020-02-02 21:49:41 +00:00
|
|
|
|
|
|
|
/**
|
2023-12-13 02:57:59 +00:00
|
|
|
* Decodes a base32-encoded string.
|
|
|
|
*
|
2024-05-20 07:14:09 +00:00
|
|
|
* @see {@link https://www.rfc-editor.org/rfc/rfc4648.html#section-6}
|
2023-12-13 02:57:59 +00:00
|
|
|
*
|
2024-04-02 10:42:55 +00:00
|
|
|
* @param b32 The base32-encoded string to decode.
|
|
|
|
* @returns The decoded data.
|
|
|
|
*
|
2024-05-24 02:23:24 +00:00
|
|
|
* @example Usage
|
2023-12-13 02:57:59 +00:00
|
|
|
* ```ts
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { decodeBase32 } from "@std/encoding/base32";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2023-12-13 02:57:59 +00:00
|
|
|
*
|
2024-05-24 02:23:24 +00:00
|
|
|
* assertEquals(
|
|
|
|
* decodeBase32("GZRTMMDDGA======"),
|
|
|
|
* new TextEncoder().encode("6c60c0"),
|
|
|
|
* );
|
2023-12-13 02:57:59 +00:00
|
|
|
* ```
|
2020-02-02 21:49:41 +00:00
|
|
|
*/
|
2023-09-21 09:29:13 +00:00
|
|
|
export function decodeBase32(b32: string): Uint8Array {
|
2024-08-09 09:57:21 +00:00
|
|
|
return decode(b32, lookup);
|
2020-02-02 21:49:41 +00:00
|
|
|
}
|
|
|
|
|
2023-09-21 09:29:13 +00:00
|
|
|
/**
|
2024-04-02 10:42:55 +00:00
|
|
|
* Converts data into a base32-encoded string.
|
2023-12-13 02:57:59 +00:00
|
|
|
*
|
2024-05-20 07:14:09 +00:00
|
|
|
* @see {@link https://www.rfc-editor.org/rfc/rfc4648.html#section-6}
|
2023-12-13 02:57:59 +00:00
|
|
|
*
|
2024-04-02 10:42:55 +00:00
|
|
|
* @param data The data to encode.
|
|
|
|
* @returns The base32-encoded string.
|
|
|
|
*
|
2024-05-24 02:23:24 +00:00
|
|
|
* @example Usage
|
2023-12-13 02:57:59 +00:00
|
|
|
* ```ts
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { encodeBase32 } from "@std/encoding/base32";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2023-12-13 02:57:59 +00:00
|
|
|
*
|
2024-05-24 02:23:24 +00:00
|
|
|
* assertEquals(encodeBase32("6c60c0"), "GZRTMMDDGA======");
|
2023-12-13 02:57:59 +00:00
|
|
|
* ```
|
2023-09-21 09:29:13 +00:00
|
|
|
*/
|
|
|
|
export function encodeBase32(data: ArrayBuffer | Uint8Array | string): string {
|
2024-08-09 09:57:21 +00:00
|
|
|
return encode(data, lookup);
|
2020-02-02 21:49:41 +00:00
|
|
|
}
|