atob.js/node-atob.js

26 lines
789 B
JavaScript
Raw Normal View History

"use strict";
2021-03-12 00:26:12 +11:00
// 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('=');
}
2021-03-04 16:08:29 +11:00
function atob(str) {
2021-03-12 00:26:12 +11:00
for (var idx in str)
if (!isValidB64Char(str.charCodeAt(idx)))
throw new Error('Invalid character ' + str.charAt(idx) + ' in base64 String');
2018-03-27 22:26:00 -06:00
return Buffer.from(str, 'base64').toString('binary');
}
module.exports = atob.atob = atob;