2024-08-09 09:57:21 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
|
|
|
// Copyright (c) 2014 Jameson Little. MIT License.
|
|
|
|
// This module is browser compatible.
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Utilities for
|
|
|
|
* {@link https://www.rfc-editor.org/rfc/rfc4648.html#section-7 | base32hex}
|
|
|
|
* encoding and decoding.
|
|
|
|
*
|
|
|
|
* Modified from {@link [base32]}.
|
|
|
|
*
|
|
|
|
* This module is browser compatible.
|
|
|
|
*
|
|
|
|
* ```ts
|
2024-09-12 06:31:01 +00:00
|
|
|
* import { encodeBase32Hex, decodeBase32Hex } from "@std/encoding/unstable-base32hex";
|
2024-08-09 09:57:21 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
|
|
|
*
|
|
|
|
* assertEquals(encodeBase32Hex("foobar"), "CPNMUOJ1E8======");
|
|
|
|
*
|
|
|
|
* assertEquals(
|
|
|
|
* decodeBase32Hex("CPNMUOJ1E8======"),
|
|
|
|
* new TextEncoder().encode("foobar")
|
|
|
|
* );
|
|
|
|
* ```
|
|
|
|
*
|
2024-09-12 06:31:01 +00:00
|
|
|
* @experimental **UNSTABLE**: New API, yet to be vetted.
|
|
|
|
*
|
2024-08-09 09:57:21 +00:00
|
|
|
* @module
|
|
|
|
*/
|
|
|
|
import { decode, encode } from "./_base32_common.ts";
|
|
|
|
|
|
|
|
const lookup: string[] = "0123456789ABCDEFGHIJKLMNOPQRSTUV".split("");
|
|
|
|
const revLookup: number[] = [];
|
|
|
|
lookup.forEach((c, i) => revLookup[c.charCodeAt(0)] = i);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decodes a base32hex-encoded string.
|
|
|
|
*
|
docs(archive,assert,cache,cli,encoding,html,http,net,streams,text): remove unstable Markdown alert (#5672)
* docs(archive,cli,html,http,net,streams,text): remove unstable Markdown alert
* update
* fix
* update
* fmt
* fix
2024-08-22 06:55:17 +00:00
|
|
|
* @experimental **UNSTABLE**: New API, yet to be vetted.
|
|
|
|
*
|
2024-08-09 09:57:21 +00:00
|
|
|
* @see {@link https://www.rfc-editor.org/rfc/rfc4648.html#section-7}
|
|
|
|
*
|
|
|
|
* @param b32 The base32hex-encoded string to decode.
|
|
|
|
* @returns The decoded data.
|
|
|
|
*
|
|
|
|
* @example Usage
|
|
|
|
* ```ts
|
2024-09-12 06:31:01 +00:00
|
|
|
* import { decodeBase32Hex } from "@std/encoding/unstable-base32hex";
|
2024-08-09 09:57:21 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
|
|
|
*
|
|
|
|
* assertEquals(
|
|
|
|
* decodeBase32Hex("6PHJCC3360======"),
|
|
|
|
* new TextEncoder().encode("6c60c0"),
|
|
|
|
* );
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
export function decodeBase32Hex(b32: string): Uint8Array {
|
|
|
|
return decode(b32, lookup);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts data into a base32hex-encoded string.
|
|
|
|
*
|
docs(archive,assert,cache,cli,encoding,html,http,net,streams,text): remove unstable Markdown alert (#5672)
* docs(archive,cli,html,http,net,streams,text): remove unstable Markdown alert
* update
* fix
* update
* fmt
* fix
2024-08-22 06:55:17 +00:00
|
|
|
* @experimental **UNSTABLE**: New API, yet to be vetted.
|
|
|
|
*
|
2024-08-09 09:57:21 +00:00
|
|
|
* @see {@link https://www.rfc-editor.org/rfc/rfc4648.html#section-7}
|
|
|
|
*
|
|
|
|
* @param data The data to encode.
|
|
|
|
* @returns The base32hex-encoded string.
|
|
|
|
*
|
|
|
|
* @example Usage
|
|
|
|
* ```ts
|
2024-09-12 06:31:01 +00:00
|
|
|
* import { encodeBase32Hex } from "@std/encoding/unstable-base32hex";
|
2024-08-09 09:57:21 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
|
|
|
*
|
|
|
|
* assertEquals(encodeBase32Hex("6c60c0"), "6PHJCC3360======");
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
export function encodeBase32Hex(
|
|
|
|
data: ArrayBuffer | Uint8Array | string,
|
|
|
|
): string {
|
|
|
|
return encode(data, lookup);
|
|
|
|
}
|