-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase32.js
300 lines (265 loc) · 6.96 KB
/
base32.js
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
"use strict";
/**
* Generate a character map.
* @param {string} alphabet e.g. "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"
* @param {CharacterMap} mappings map overrides from key to value
* @method
* @returns {CharacterMap} key value map
*/
function charmap(alphabet, mappings) {
mappings || (mappings = {});
alphabet.split("").forEach(function (c, i) {
if (!(c in mappings)) mappings[c] = i;
});
return mappings;
}
/**
* The RFC 4648 base 32 alphabet and character map.
* @see {@link https://tools.ietf.org/html/rfc4648}
* @type {Base32Variant}
*/
const rfc4648 = {
alphabet: "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",
charmap: {
0: 14,
1: 8
}
};
rfc4648.charmap = charmap(rfc4648.alphabet, rfc4648.charmap);
/**
* The Crockford base 32 alphabet and character map.
* @see {@link http://www.crockford.com/wrmg/base32.html}
* @type {Base32Variant}
*/
const crockford = {
alphabet: "0123456789ABCDEFGHJKMNPQRSTVWXYZ",
charmap: {
O: 0,
I: 1,
L: 1
}
};
crockford.charmap = charmap(crockford.alphabet, crockford.charmap);
/**
* base32hex
* @see {@link https://en.wikipedia.org/wiki/Base32#base32hex}
* @type {Base32Variant}
*/
const base32hex = {
alphabet: "0123456789ABCDEFGHIJKLMNOPQRSTUV",
charmap: {}
};
base32hex.charmap = charmap(base32hex.alphabet, base32hex.charmap);
/**
* Create a new `Decoder` with the given options.
*
* @param {DecoderOptions} [options]
* @param {string} [options.type] Supported Base-32 variants are:
* "rfc4648", "base32hex", and "crockford".
* @param {CharacterMap} [options.charmap] Override the character map used in decoding.
* @constructor
*/
function Decoder (options) {
/** @type ByteArray */
this.buf = [];
this.shift = 8;
this.carry = 0;
if (options) {
switch (options.type) {
case "rfc4648":
this.charmap = exports.rfc4648.charmap;
break;
case "crockford":
this.charmap = exports.crockford.charmap;
break;
case "base32hex":
this.charmap = exports.base32hex.charmap;
break;
default:
throw new Error("invalid type");
}
if (options.charmap) this.charmap = options.charmap;
}
}
/**
* The default character map corresponds to RFC4648.
*/
Decoder.prototype.charmap = rfc4648.charmap;
/**
* Decode a string, continuing from the previous state.
*
* @param {string} str
* @return {Decoder} this
*/
Decoder.prototype.write = function (str) {
const charmap = this.charmap;
const buf = this.buf;
let shift = this.shift;
let carry = this.carry;
// decode string
for (const char of str.toUpperCase().split("")) {
// ignore padding
if (char == "=") continue;
// lookup symbol
const symbol = charmap[char] & 0xff;
// 1: 00000 000
// 2: 00 00000 0
// 3: 0000 0000
// 4: 0 00000 00
// 5: 000 00000
// 6: 00000 000
// 7: 00 00000 0
shift -= 5;
if (shift > 0) {
carry |= symbol << shift;
} else if (shift < 0) {
buf.push(carry | (symbol >> -shift));
shift += 8;
carry = (symbol << shift) & 0xff;
} else {
buf.push(carry | symbol);
shift = 8;
carry = 0;
}
}
// save state
this.shift = shift;
this.carry = carry;
// for chaining
return this;
};
/**
* Finish decoding.
*
* @param {string} [str] The final string to decode.
* @return {ByteArray} Decoded byte array.
*/
Decoder.prototype.finalize = function (str) {
if (str) {
this.write(str);
}
if (this.shift !== 8 && this.carry !== 0) {
this.buf.push(this.carry);
this.shift = 8;
this.carry = 0;
}
return this.buf;
};
/**
* Create a new `Encoder` with the given options.
*
* @param {EncoderOptions} [options]
* @param {string} [options.type] Supported Base-32 variants are:
* "rfc4648", "base32hex", and "crockford".
* @param {string} [options.alphabet] Override the alphabet used in encoding.
* @param {boolean} [options.lc] Use lower-case alphabet.
* @constructor
*/
function Encoder (options) {
this.buf = "";
this.shift = 3;
this.carry = 0;
if (options) {
switch (options.type) {
case "rfc4648":
this.alphabet = exports.rfc4648.alphabet;
break;
case "crockford":
this.alphabet = exports.crockford.alphabet;
break;
case "base32hex":
this.alphabet = exports.base32hex.alphabet;
break;
default:
throw new Error("invalid type");
}
if (options.alphabet) this.alphabet = options.alphabet;
else if (options.lc) this.alphabet = this.alphabet.toLowerCase();
}
}
/**
* The default alphabet corresponds to RFC4648.
* @type string
*/
Encoder.prototype.alphabet = rfc4648.alphabet;
/**
* Encode a byte array, continuing from the previous state.
*
* @param {ByteArray} buf The byte array to encode.
* @return {Encoder} this
*/
Encoder.prototype.write = function (buf) {
let shift = this.shift;
let carry = this.carry;
let symbol;
// encode each byte in buf
for (let i = 0; i < buf.length; i++) {
const byte = buf[i];
// 1: 00000 000
// 2: 00 00000 0
// 3: 0000 0000
// 4: 0 00000 00
// 5: 000 00000
// 6: 00000 000
// 7: 00 00000 0
symbol = carry | (byte >> shift);
this.buf += this.alphabet[symbol & 0x1f];
if (shift > 5) {
shift -= 5;
symbol = byte >> shift;
this.buf += this.alphabet[symbol & 0x1f];
}
shift = 5 - shift;
carry = byte << shift;
shift = 8 - shift;
}
// save state
this.shift = shift;
this.carry = carry;
// for chaining
return this;
};
/**
* Finish encoding.
*
* @param {ByteArray} [buf] The final byte array to encode.
* @return {string} The encoded byte array.
*/
Encoder.prototype.finalize = function (buf) {
if (buf) {
this.write(buf);
}
if (this.shift !== 3) {
this.buf += this.alphabet[this.carry & 0x1f];
this.shift = 3;
this.carry = 0;
}
return this.buf;
};
/**
* Convenience encoder.
*
* @param {ByteArray} buf The byte array to encode.
* @param {DecoderOptions} [options] Options to pass to the encoder.
* @return {string} The encoded string.
*/
exports.encode = function (buf, options) {
return new Encoder(options).finalize(buf);
};
/**
* Convenience decoder.
*
* @param {string} str The string to decode.
* @param {DecoderOptions} [options] Options to pass to the decoder.
* @return {ByteArray} The decoded byte array.
*/
exports.decode = function (str, options) {
return new Decoder(options).finalize(str);
};
// Exports.
exports.Decoder = Decoder;
exports.Encoder = Encoder;
exports.charmap = charmap;
exports.crockford = crockford;
exports.rfc4648 = rfc4648;
exports.base32hex = base32hex;