deno.land / std@0.166.0 / encoding / base32.ts

نووسراو ببینە
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
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.// Copyright (c) 2014 Jameson Little. MIT License.
/** * {@linkcode encode} and {@linkcode decode} for * [base32](https://en.wikipedia.org/wiki/Base32) encoding. * * Modified from https://github.com/beatgammit/base64-js * * This module is browser compatible. * * @module */
const lookup: string[] = [];const revLookup: number[] = [];
// RFC4648 base32const code = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";for (let i = 0, len = code.length; i < len; ++i) { lookup[i] = code[i]; revLookup[code.charCodeAt(i)] = i;}
const placeHolderPadLookup = [0, 1, , 2, 3, , 4];function _getPadLen(placeHoldersLen: number): number { const maybeLen = placeHolderPadLookup[placeHoldersLen]; if (typeof maybeLen !== "number") { throw new Error("Invalid pad length"); } return maybeLen;}
function getLens(b32: string): [number, number] { const len = b32.length;
if (len % 8 > 0) { throw new Error("Invalid string. Length must be a multiple of 8"); }
let validLen = b32.indexOf("="); if (validLen === -1) validLen = len;
const placeHoldersLen = validLen === len ? 0 : 8 - (validLen % 8);
return [validLen, placeHoldersLen];}
/** * Returns number of bytes encoded in the given RFC4648 base32 string input. * @param b32 */export function byteLength(b32: string): number { const [validLen, placeHoldersLen] = getLens(b32); return _byteLength(validLen, placeHoldersLen);}
function _byteLength(validLen: number, placeHoldersLen: number): number { return ((validLen + placeHoldersLen) * 5) / 8 - _getPadLen(placeHoldersLen);}
/** * Decodes a given RFC4648 base32 encoded string. * @param b32 */export function decode(b32: string): Uint8Array { let tmp: number; const [validLen, placeHoldersLen] = getLens(b32);
const arr = new Uint8Array(_byteLength(validLen, placeHoldersLen));
let curByte = 0;
// if there are placeholders, only get up to the last complete 8 chars const len = placeHoldersLen > 0 ? validLen - 8 : validLen;
let i: number; for (i = 0; i < len; i += 8) { tmp = (revLookup[b32.charCodeAt(i)] << 20) | (revLookup[b32.charCodeAt(i + 1)] << 15) | (revLookup[b32.charCodeAt(i + 2)] << 10) | (revLookup[b32.charCodeAt(i + 3)] << 5) | revLookup[b32.charCodeAt(i + 4)]; arr[curByte++] = (tmp >> 17) & 0xff; arr[curByte++] = (tmp >> 9) & 0xff; arr[curByte++] = (tmp >> 1) & 0xff;
tmp = ((tmp & 1) << 15) | (revLookup[b32.charCodeAt(i + 5)] << 10) | (revLookup[b32.charCodeAt(i + 6)] << 5) | revLookup[b32.charCodeAt(i + 7)]; arr[curByte++] = (tmp >> 8) & 0xff; arr[curByte++] = tmp & 0xff; }
if (placeHoldersLen === 1) { tmp = (revLookup[b32.charCodeAt(i)] << 20) | (revLookup[b32.charCodeAt(i + 1)] << 15) | (revLookup[b32.charCodeAt(i + 2)] << 10) | (revLookup[b32.charCodeAt(i + 3)] << 5) | revLookup[b32.charCodeAt(i + 4)]; arr[curByte++] = (tmp >> 17) & 0xff; arr[curByte++] = (tmp >> 9) & 0xff; arr[curByte++] = (tmp >> 1) & 0xff; tmp = ((tmp & 1) << 7) | (revLookup[b32.charCodeAt(i + 5)] << 2) | (revLookup[b32.charCodeAt(i + 6)] >> 3); arr[curByte++] = tmp & 0xff; } else if (placeHoldersLen === 3) { tmp = (revLookup[b32.charCodeAt(i)] << 19) | (revLookup[b32.charCodeAt(i + 1)] << 14) | (revLookup[b32.charCodeAt(i + 2)] << 9) | (revLookup[b32.charCodeAt(i + 3)] << 4) | (revLookup[b32.charCodeAt(i + 4)] >> 1); arr[curByte++] = (tmp >> 16) & 0xff; arr[curByte++] = (tmp >> 8) & 0xff; arr[curByte++] = tmp & 0xff; } else if (placeHoldersLen === 4) { tmp = (revLookup[b32.charCodeAt(i)] << 11) | (revLookup[b32.charCodeAt(i + 1)] << 6) | (revLookup[b32.charCodeAt(i + 2)] << 1) | (revLookup[b32.charCodeAt(i + 3)] >> 4); arr[curByte++] = (tmp >> 8) & 0xff; arr[curByte++] = tmp & 0xff; } else if (placeHoldersLen === 6) { tmp = (revLookup[b32.charCodeAt(i)] << 3) | (revLookup[b32.charCodeAt(i + 1)] >> 2); arr[curByte++] = tmp & 0xff; }
return arr;}
function encodeChunk(uint8: Uint8Array, start: number, end: number): string { let tmp: number; const output = []; for (let i = start; i < end; i += 5) { tmp = ((uint8[i] << 16) & 0xff0000) | ((uint8[i + 1] << 8) & 0xff00) | (uint8[i + 2] & 0xff); output.push(lookup[(tmp >> 19) & 0x1f]); output.push(lookup[(tmp >> 14) & 0x1f]); output.push(lookup[(tmp >> 9) & 0x1f]); output.push(lookup[(tmp >> 4) & 0x1f]); tmp = ((tmp & 0xf) << 16) | ((uint8[i + 3] << 8) & 0xff00) | (uint8[i + 4] & 0xff); output.push(lookup[(tmp >> 15) & 0x1f]); output.push(lookup[(tmp >> 10) & 0x1f]); output.push(lookup[(tmp >> 5) & 0x1f]); output.push(lookup[tmp & 0x1f]); } return output.join("");}
/** * Encodes a given Uint8Array into RFC4648 base32 representation * @param uint8 */export function encode(uint8: Uint8Array): string { let tmp: number; const len = uint8.length; const extraBytes = len % 5; const parts = []; const maxChunkLength = 16385; // must be multiple of 5 const len2 = len - extraBytes;
// go through the array every 5 bytes, we'll deal with trailing stuff later for (let i = 0; i < len2; i += maxChunkLength) { parts.push( encodeChunk( uint8, i, i + maxChunkLength > len2 ? len2 : i + maxChunkLength, ), ); }
// pad the end with zeros, but make sure to not forget the extra bytes if (extraBytes === 4) { tmp = ((uint8[len2] & 0xff) << 16) | ((uint8[len2 + 1] & 0xff) << 8) | (uint8[len2 + 2] & 0xff); parts.push(lookup[(tmp >> 19) & 0x1f]); parts.push(lookup[(tmp >> 14) & 0x1f]); parts.push(lookup[(tmp >> 9) & 0x1f]); parts.push(lookup[(tmp >> 4) & 0x1f]); tmp = ((tmp & 0xf) << 11) | (uint8[len2 + 3] << 3); parts.push(lookup[(tmp >> 10) & 0x1f]); parts.push(lookup[(tmp >> 5) & 0x1f]); parts.push(lookup[tmp & 0x1f]); parts.push("="); } else if (extraBytes === 3) { tmp = ((uint8[len2] & 0xff) << 17) | ((uint8[len2 + 1] & 0xff) << 9) | ((uint8[len2 + 2] & 0xff) << 1); parts.push(lookup[(tmp >> 20) & 0x1f]); parts.push(lookup[(tmp >> 15) & 0x1f]); parts.push(lookup[(tmp >> 10) & 0x1f]); parts.push(lookup[(tmp >> 5) & 0x1f]); parts.push(lookup[tmp & 0x1f]); parts.push("==="); } else if (extraBytes === 2) { tmp = ((uint8[len2] & 0xff) << 12) | ((uint8[len2 + 1] & 0xff) << 4); parts.push(lookup[(tmp >> 15) & 0x1f]); parts.push(lookup[(tmp >> 10) & 0x1f]); parts.push(lookup[(tmp >> 5) & 0x1f]); parts.push(lookup[tmp & 0x1f]); parts.push("===="); } else if (extraBytes === 1) { tmp = (uint8[len2] & 0xff) << 2; parts.push(lookup[(tmp >> 5) & 0x1f]); parts.push(lookup[tmp & 0x1f]); parts.push("======"); }
return parts.join("");}
std
Deno standard library
GitHub Repositorydenoland/deno_std
GitHub Stars
2447

Version Info

Tagged at
2 months ago