-
-
Notifications
You must be signed in to change notification settings - Fork 241
/
Base32.js
269 lines (243 loc) · 6.37 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
import Chain from '../Chain'
import Encoder from '../Encoder'
import InvalidInputError from '../Error/InvalidInput'
const meta = {
name: 'base32',
title: 'Base32',
category: 'Encoding',
type: 'encoder'
}
/**
* Array of Base32 variant options.
* @type {object[]}
*/
const variants = [
{
name: 'base32',
label: 'Base32 (RFC 3548, RFC 4648)',
alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567',
padding: 0x3D,
decodeMap: {
0x30: 14,
0x31: 8
}
},
{
name: 'base32hex',
label: 'Base32hex (RFC 4648)',
alphabet: '0123456789ABCDEFGHIJKLMNOPQRSTUV',
padding: 0x3D
},
{
name: 'z-base-32',
label: 'z-base-32',
alphabet: 'ybndrfg8ejkmcpqxot1uwisza345h769',
padding: null
},
{
name: 'crockford-base32',
label: 'Crockford\'s Base32',
alphabet: '0123456789ABCDEFGHJKMNPQRSTVWXYZ',
padding: null,
decodeContentFilter: 'uppercase',
decodeMap: {
0x4F: 0,
0x49: 1,
0x4C: 1
}
},
{
name: 'custom',
label: 'Custom'
}
]
/**
* Encoder brick for base32 encoding and decoding
*/
export default class Base32Encoder extends Encoder {
/**
* Returns brick meta.
* @return {object}
*/
static getMeta () {
return meta
}
/**
* Constructor
*/
constructor () {
super()
this.addSettings([
{
name: 'variant',
type: 'enum',
value: variants[0].name,
elements: variants.map(variant => variant.name),
labels: variants.map(variant => variant.label),
randomizable: false
},
{
name: 'alphabet',
type: 'text',
value: variants[0].alphabet,
uniqueChars: true,
minLength: 32,
maxLength: 32,
caseSensitivity: true,
visible: false
},
{
name: 'padding',
type: 'text',
value: '=',
blacklistChars: variants[0].alphabet,
minLength: 0,
maxLength: 1,
randomizable: false,
visible: false
}
])
}
/**
* Performs encode on given content.
* @protected
* @param {Chain} content
* @return {number[]|string|Uint8Array|Chain|Promise} Encoded content
*/
performEncode (content) {
const { alphabet, padding } = this.getVariantOptions()
const alphabetCodePoints = alphabet.getCodePoints()
// Prepare input and output arrays
const input = content.getBytes()
const inputLength = input.length
const resultLength = Math.ceil(inputLength / 5) * 8
const result = new Array(resultLength)
let j = 0
let shift = 3
let carry = 0
let byte, index
// Go through every input byte
for (let i = 0; i < inputLength; i++) {
byte = input[i]
index = carry | (byte >> shift)
result[j++] = alphabetCodePoints[index & 0x1f]
if (shift > 5) {
shift -= 5
index = byte >> shift
result[j++] = alphabetCodePoints[index & 0x1f]
}
shift = 5 - shift
carry = byte << shift
shift = 8 - shift
}
if (shift !== 3) {
result[j++] = alphabetCodePoints[carry & 0x1f]
}
// Finalize result padding
if (padding !== null) {
while (j < resultLength) {
result[j++] = padding
}
return result
} else {
return result.slice(0, j)
}
}
/**
* Performs decode on given content.
* @protected
* @param {Chain} content
* @return {number[]|string|Uint8Array|Chain|Promise} Decoded content
*/
performDecode (content) {
const { alphabet, decodeMap, padding, decodeContentFilter } =
this.getVariantOptions()
// Apply variant filter before decoding
if (decodeContentFilter === 'uppercase') {
content = content.toUpperCase()
}
// Add alphabet characters to decode map
for (let i = 0; i < alphabet.getLength(); i++) {
decodeMap[alphabet.getCodePointAt(i)] = i
}
// Prepare input and output arrays
const input = content.getCodePoints()
const inputLength = input.length
const resultSize = Math.ceil(inputLength / 8) * 5
const result = new Uint8Array(resultSize)
let j = 0
let shift = 8
let carry = 0
let codePoint, index
// Go through every input code point
for (let i = 0; i < inputLength; i++) {
codePoint = input[i]
if (codePoint === padding) {
// Ignore padding
} else if (decodeMap[codePoint] === undefined) {
// Unexpected code point not being part of the alphabet or map
throw new InvalidInputError(`Unexpected code point at index ${i}`)
} else {
// Decode character
index = decodeMap[codePoint] & 0xff
shift -= 5
if (shift > 0) {
carry |= index << shift
} else if (shift < 0) {
result[j++] = carry | (index >> -shift)
shift += 8
carry = (index << shift) & 0xff
} else {
result[j++] = carry | index
shift = 8
carry = 0
}
}
}
// Slice result to remove padding
return result.slice(0, j)
}
/**
* Triggered when a setting field has changed.
* @protected
* @param {Field} setting Sender setting field
* @param {mixed} value New field value
*/
settingValueDidChange (setting, value) {
switch (setting.getName()) {
case 'variant': {
// Make alphabet and padding settings available with the custom variant
this.getSetting('alphabet').setVisible(value === 'custom')
this.getSetting('padding').setVisible(value === 'custom')
break
}
case 'alphabet': {
// Alphabet characters are not allowed to be used as padding
this.getSetting('padding').setBlacklistChars(value)
break
}
}
}
/**
* Returns the current variant options.
* @return {object} Variant options
*/
getVariantOptions () {
const name = this.getSettingValue('variant')
if (name === 'custom') {
// Compose custom variant options
const padding = this.getSettingValue('padding')
return {
alphabet: this.getSettingValue('alphabet'),
padding: padding.getLength() === 1 ? padding.getCodePointAt(0) : null,
decodeContentFilter: null,
decodeMap: {}
}
}
// Find variant options
const options = variants.find(variant => variant.name === name)
options.alphabet = Chain.wrap(options.alphabet)
options.decodeMap = options.decodeMap || {}
return options
}
}