All files / encoding / unstable_base32.ts

87.50% Branches 7/8
94.55% Lines 52/55
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x9
x9
 
 
x9
x9
x9
x9
x9
x9
x9
x9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x9
x9
x9
 
x89
x89
x190
x165
x241
x241
x89
x89
x89
 
x89
x89
x89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x9
x9
x9
x9
 
x49
 
 
 
x88
x88
x49
x49
x67
x67
x71
x71
x71
x71
x49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x9
x9
x9
 
x82
x82
x82
x82
x82
x82
x82
 
x82

































































































































































I



























































// Copyright 2018-2025 the Deno authors. MIT license.
// This module is browser compatible.

/**
 * Functions to encode and decode to and from base32 strings.
 *
 * ```ts
 * import { assertEquals } from "@std/assert";
 * import {
 *   encodeBase32,
 *   type Uint8Array_
 * } from "@std/encoding/unstable-base32";
 *
 * assertEquals(
 *   encodeBase32("Hello World", { alphabet: "base32" }),
 *   "JBSWY3DPEBLW64TMMQ======",
 * );
 * assertEquals(
 *   encodeBase32(
 *     new TextEncoder().encode("Hello World") as Uint8Array_,
 *     { alphabet: "base32" },
 *   ),
 *   "JBSWY3DPEBLW64TMMQ======",
 * );
 * ```
 *
 * @see {@link https://www.rfc-editor.org/rfc/rfc4648.html#section-6}
 * @experimental **UNSTABLE**: New API, yet to be vetted.
 *
 * @module
 */

import type { Uint8Array_ } from "./_types.ts";
export type { Uint8Array_ };
import {
  alphabet,
  type Base32Alphabet,
  type Base32Options,
  calcSizeBase32,
  decode,
  encode,
  padding,
  rAlphabet,
} from "./_common32.ts";
export { type Base32Alphabet, type Base32Options, calcSizeBase32 };
import { detach } from "./_common_detach.ts";

/**
 * `encodeBase32` takes an input source and encodes it into a base32 string. If
 * a {@linkcode Uint8Array<ArrayBuffer>} or {@linkcode ArrayBuffer} is provided,
 * the underlying source will be detached and reused for the encoding. If you
 * need the input source after providing it to this function, call `.slice()` to
 * pass in a copy.
 *
 * @experimental **UNSTABLE**: New API, yet to be vetted.
 *
 * @param input The input source to encode.
 * @param options The options to use for encoding.
 * @returns The base32 string representation of the input.
 *
 * @example Basic Usage
 * ```ts
 * import { assertEquals } from "@std/assert";
 * import {
 *   encodeBase32,
 *   type Uint8Array_,
 * } from "@std/encoding/unstable-base32";
 *
 * assertEquals(
 *   encodeBase32("Hello World", { alphabet: "base32" }),
 *   "JBSWY3DPEBLW64TMMQ======",
 * );
 * assertEquals(
 *   encodeBase32(
 *     new TextEncoder().encode("Hello World") as Uint8Array_,
 *     { alphabet: "base32" },
 *   ),
 *   "JBSWY3DPEBLW64TMMQ======",
 * );
 *
 * assertEquals(
 *   encodeBase32("Hello World", { alphabet: "base32hex" }),
 *   "91IMOR3F41BMUSJCCG======",
 * );
 * assertEquals(
 *   encodeBase32(
 *     new TextEncoder().encode("Hello World") as Uint8Array_,
 *     { alphabet: "base32hex" },
 *   ),
 *   "91IMOR3F41BMUSJCCG======",
 * );
 *
 * assertEquals(
 *   encodeBase32("Hello World", { alphabet: "base32crockford" }),
 *   "91JPRV3F41BPYWKCCG======",
 * );
 * assertEquals(
 *   encodeBase32(
 *     new TextEncoder().encode("Hello World") as Uint8Array_,
 *     { alphabet: "base32crockford" },
 *   ),
 *   "91JPRV3F41BPYWKCCG======",
 * );
 * ```
 */
export function encodeBase32(
  input: string | Uint8Array_ | ArrayBuffer,
  options: Base32Options = {},
): string {
  options.alphabet ??= "base32";
  if (typeof input === "string") {
    input = new TextEncoder().encode(input) as Uint8Array_;
  } else if (input instanceof ArrayBuffer) {
    input = new Uint8Array(input);
  }
  const [output, i] = detach(
    input as Uint8Array_,
    calcSizeBase32((input as Uint8Array_).length),
  );
  encode(output, i, 0, alphabet[options.alphabet], padding);
  return new TextDecoder().decode(output);
}

/**
 * `encodeIntoBase32` takes an input source and encodes it as base32 into the
 * output buffer.
 *
 * @experimental **UNSTABLE**: New API, yet to be vetted.
 *
 * @param input the source to encode.
 * @param output the buffer to write the encoded source to.
 * @param options the options to use for encoding.
 * @returns the number of bytes written to the buffer.
 *
 * @example Basic Usage
 * ```ts
 * import { assertEquals } from "@std/assert";
 * import {
 *   calcSizeBase32,
 *   encodeBase32,
 *   encodeIntoBase32,
 * } from "@std/encoding/unstable-base32";
 *
 * const prefix = "data:url/fake,";
 * const input = await Deno.readFile("./deno.lock");
 * const output = new Uint8Array(prefix.length + calcSizeBase32(input.length));
 *
 * const o = new TextEncoder().encodeInto(prefix, output).written;
 * encodeIntoBase32(input, output.subarray(o), { alphabet: "base32" });
 * assertEquals(
 *   new TextDecoder().decode(output),
 *   "data:url/fake," +
 *     encodeBase32(await Deno.readFile("./deno.lock"), { alphabet: "base32" }),
 * );
 * ```
 */
export function encodeIntoBase32(
  input: string | Uint8Array_ | ArrayBuffer,
  output: Uint8Array_,
  options: Base32Options = {},
): number {
  options.alphabet ??= "base32";
  if (typeof input === "string") {
    input = new TextEncoder().encode(input) as Uint8Array_;
  } else if (input instanceof ArrayBuffer) {
    input = new Uint8Array(input);
  }
  const min = calcSizeBase32((input as Uint8Array_).length);
  if (output.length < min) {
    throw new RangeError("Cannot encode input as base32: Output too small");
  }
  output = output.subarray(0, min);
  const i = min - (input as Uint8Array_).length;
  output.set(input as Uint8Array_, i);
  return encode(output, i, 0, alphabet[options.alphabet], padding);
}

/**
 * `decodeBase32` takes an input source and decodes it into a
 * {@linkcode Uint8Array<ArrayBuffer>} using the specified format. If a
 * {@linkcode Uint8Array<ArrayBuffer>} is provided as input then a subarray of
 * the input containing the decoded data is returned.
 *
 * @experimental **UNSTABLE**: New API, yet to be vetted.
 *
 * @param input The input source to decode.
 * @param options The options to use for decoding.
 * @returns The decoded {@linkcode Uint8Array<ArrayBuffer>}.
 *
 * @example Basic Usage
 * ```ts
 * import { assertEquals } from "@std/assert";
 * import { decodeBase32 } from "@std/encoding/unstable-base32";
 *
 * assertEquals(
 *   decodeBase32("JBSWY3DPEBLW64TMMQ======", { alphabet: "base32"}),
 *   new TextEncoder().encode("Hello World"),
 * );
 *
 * assertEquals(
 *   decodeBase32("91IMOR3F41BMUSJCCG======", { alphabet: "base32hex"}),
 *   new TextEncoder().encode("Hello World"),
 * );
 *
 * assertEquals(
 *   decodeBase32("91JPRV3F41BPYWKCCG======", { alphabet: "base32crockford"}),
 *   new TextEncoder().encode("Hello World"),
 * );
 * ```
 */
export function decodeBase32(
  input: string | Uint8Array_,
  options: Base32Options = {},
): Uint8Array_ {
  options.alphabet ??= "base32";
  if (typeof input === "string") {
    input = new TextEncoder().encode(input) as Uint8Array_;
  }
  return input.subarray(
    0,
    decode(input, 0, 0, rAlphabet[options.alphabet], padding),
  );
}