|
| 1 | +/** |
| 2 | + * Implements base32 and base32hex from [RFC4648](https://datatracker.ietf.org/doc/html/rfc4648) |
| 3 | + * (no differences from [RFC3548](https://datatracker.ietf.org/doc/html/rfc4648)). |
| 4 | + * |
| 5 | + * ```js |
| 6 | + * import { fromBase32, toBase32 } from '@exodus/bytes/base32.js' |
| 7 | + * import { fromBase32hex, toBase32hex } from '@exodus/bytes/base32.js' |
| 8 | + * ``` |
| 9 | + * |
| 10 | + * @module @exodus/bytes/base32.js |
| 11 | + */ |
| 12 | + |
| 13 | +/// <reference types="node" /> |
| 14 | + |
| 15 | +import type { OutputFormat, Uint8ArrayBuffer } from './array.js'; |
| 16 | + |
| 17 | +/** |
| 18 | + * Options for base32 encoding |
| 19 | + */ |
| 20 | +export interface ToBase32Options { |
| 21 | + /** Whether to include padding characters (default: false) */ |
| 22 | + padding?: boolean; |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Padding mode for base32 decoding |
| 27 | + * - `true`: padding is required |
| 28 | + * - `false`: padding is not allowed |
| 29 | + * - `'both'`: padding is optional (default) |
| 30 | + */ |
| 31 | +export type PaddingMode = boolean | 'both'; |
| 32 | + |
| 33 | +/** |
| 34 | + * Options for base32 decoding |
| 35 | + */ |
| 36 | +export interface FromBase32Options { |
| 37 | + /** Output format (default: 'uint8') */ |
| 38 | + format?: OutputFormat; |
| 39 | + /** Padding mode */ |
| 40 | + padding?: PaddingMode; |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Encode a `Uint8Array` to a base32 string (RFC 4648) |
| 45 | + * |
| 46 | + * @param arr - The input bytes |
| 47 | + * @param options - Encoding options |
| 48 | + * @returns The base32 encoded string |
| 49 | + */ |
| 50 | +export function toBase32(arr: Uint8ArrayBuffer, options?: ToBase32Options): string; |
| 51 | + |
| 52 | +/** |
| 53 | + * Encode a `Uint8Array` to a base32hex string (RFC 4648) |
| 54 | + * |
| 55 | + * @param arr - The input bytes |
| 56 | + * @param options - Encoding options (padding defaults to false) |
| 57 | + * @returns The base32hex encoded string |
| 58 | + */ |
| 59 | +export function toBase32hex(arr: Uint8ArrayBuffer, options?: ToBase32Options): string; |
| 60 | + |
| 61 | +/** |
| 62 | + * Decode a base32 string to bytes |
| 63 | + * |
| 64 | + * Operates in strict mode for last chunk, does not allow whitespace |
| 65 | + * |
| 66 | + * @param string - The base32 encoded string |
| 67 | + * @param options - Decoding options |
| 68 | + * @returns The decoded bytes |
| 69 | + */ |
| 70 | +export function fromBase32(string: string, options?: FromBase32Options): Uint8ArrayBuffer; |
| 71 | +export function fromBase32(string: string, options: FromBase32Options & { format: 'buffer' }): Buffer; |
| 72 | + |
| 73 | +/** |
| 74 | + * Decode a base32hex string to bytes |
| 75 | + * |
| 76 | + * Operates in strict mode for last chunk, does not allow whitespace |
| 77 | + * |
| 78 | + * @param string - The base32hex encoded string |
| 79 | + * @param options - Decoding options |
| 80 | + * @returns The decoded bytes |
| 81 | + */ |
| 82 | +export function fromBase32hex(string: string, options?: FromBase32Options): Uint8ArrayBuffer; |
| 83 | +export function fromBase32hex(string: string, options: FromBase32Options & { format: 'buffer' }): Buffer; |
0 commit comments