forked from auth0/jwt-handbook-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase64.js
141 lines (117 loc) · 3.7 KB
/
base64.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
import { isString } from './utils.js';
const table = [
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', '-', '_'
];
/**
* @param input a Buffer, Uint8Array or Int8Array, Array
* @returns a String with the encoded values
*/
export function encode(input) {
let result = "";
for(let i = 0; i < input.length; i += 3) {
const remaining = input.length - i;
let concat = input[i] << 16;
result += (table[concat >>> (24 - 6)]);
if(remaining > 1) {
concat |= input[i + 1] << 8;
result += table[(concat >>> (24 - 12)) & 0x3F];
if(remaining > 2) {
concat |= input[i + 2];
result += table[(concat >>> (24 - 18)) & 0x3F] +
table[concat & 0x3F];
} else {
result += table[(concat >>> (24 - 18)) & 0x3F] + "=";
}
} else {
result += table[(concat >>> (24 - 12)) & 0x3F] + "==";
}
}
return result;
}
/**
* @param input a String
* @returns a Uint8Array with the decoded data
*/
export function decode(input) {
if(!isString(input)) {
throw new TypeError("input must be a string");
}
let resultLength = Math.trunc(input.length * 6 / 8);
if(input.endsWith('==')) {
resultLength -= 2;
} else if(input.endsWith('=')) {
--resultLength;
}
const result = new Uint8Array(resultLength);
function getVal(i) {
if(i >= input.length) {
return 0;
}
const char = input[i];
if(char === '=') {
return 0;
}
// There are faster ways to do this, but this code is educational, so
// we keep it simple
const val = table.indexOf(char);
if(val === -1) {
throw new Error(`Invalid input: ${input[i]}`);
}
return val;
}
let i = 0;
let j = 0;
// TODO: check bounds for the last group of bytes. JavaScript allows us
// to skip these checks as out-of-bounds essentially become no-ops.
for(; j < result.length; i += 4, j += 3) {
result[j] = getVal(i) << 2;
result[j] |= getVal(i + 1) >>> 4;
result[j + 1] = getVal(i + 1) << 4;
result[j + 1] |= getVal(i + 2) >>> 2;
result[j + 2] = getVal(i + 2) << 6;
result[j + 2] |= getVal(i + 3);
}
return result;
}
if(process.env.TEST) {
function genData(length) {
const result = new Uint8Array(length);
for(let i = 0; i < result.length; ++i) {
result[i] = Math.round(Math.random() * 255);
}
return result;
}
function compare(a, b) {
if(a.length !== b.length) {
return false;
}
for(let i = 0; i < a.length; ++i) {
if(a[i] != b[i]) {
return false;
}
}
return true;
}
const data = [];
for(let i = 0; i < 101; ++i) {
data.push(genData(i));
}
data.forEach(d => {
const encoded = encode(d);
const decoded = decode(encoded);
const decoded2 = decode(encoded.replace(/=/g, ''));
if(!compare(d, decoded) || !compare(d, decoded2)) {
console.log(`Test failed for data: ${d} \n\n ` +
`encoded: ${encoded} \n\n ` +
`decoded: ${decoded} \n\n`);
process.exit(-1);
}
});
console.log('base64: all tests passed');
}