Added alphabet check before decoding #7
21
node-atob.js
21
node-atob.js
|
@ -1,11 +1,24 @@
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const b64Alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
|
// var b64Alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
|
||||||
|
|
||||||
|
function toCharCode(ch) {
|
||||||
|
return ch.charCodeAt(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
function isValidB64Char(charCode) {
|
||||||
|
return charCode >= toCharCode('A') && charCode <= toCharCode('Z')
|
||||||
|
|| charCode >= toCharCode('a') && charCode <= toCharCode('z')
|
||||||
|
|| charCode >= toCharCode('0') && charCode <= toCharCode('9')
|
||||||
|
|| charCode === toCharCode('+')
|
||||||
|
|| charCode === toCharCode('/')
|
||||||
|
|| charCode === toCharCode('=');
|
||||||
|
}
|
||||||
|
|
||||||
function atob(str) {
|
function atob(str) {
|
||||||
for (const ch of str)
|
for (var idx in str)
|
||||||
if (!b64Alphabet.includes(ch))
|
if (!isValidB64Char(str.charCodeAt(idx)))
|
||||||
throw new Error(`Invalid character '${ch}' in base64 String`);
|
throw new Error('Invalid character ' + str.charAt(idx) + ' in base64 String');
|
||||||
return Buffer.from(str, 'base64').toString('binary');
|
return Buffer.from(str, 'base64').toString('binary');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue