提醒:本文最后更新于 2021-09-26 18:25,文中所关联的信息可能已发生改变,请知悉!
var base64hash = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
function btoa(s) {
if (/([^\u0000-\u00ff])/.test(s)) {
throw new Error('INVALID_CHARACTER_ERR');
}
var i = 0,
prev, ascii, mod, result = [];
while (i < s.length) {ascii = s.charCodeAt(i); mod = i % 3; switch (mod) {case 0: result.push(base64hash.charAt(ascii>> 2));
break;
case 1:
result.push(base64hash.charAt((prev & 3) << 4 | (ascii>> 4)));
break;
case 2:
result.push(base64hash.charAt((prev & 0x0f) << 2 | (ascii>> 6)));
result.push(base64hash.charAt(ascii & 0x3f));
break
}
prev = ascii;
i++
}
if (mod == 0) {
result.push(base64hash.charAt((prev & 3) << 4));
result.push('==')
} else if (mod == 1) {
result.push(base64hash.charAt((prev & 0x0f) << 2));
result.push('=')
}
return result.join('')
}
atob 方法
// 逆转 encode 的思路即可
function _atob(s) {
s = s.replace(/\s|=/g, '');
var cur, prev, mod, i = 0,
result = [];
while (i < s.length) {
cur = base64hash.indexOf(s.charAt(i));
mod = i % 4;
switch (mod) {
case 0:
//TODO
break;
case 1:
result.push(String.fromCharCode(prev << 2 | cur>> 4));
break;
case 2:
result.push(String.fromCharCode((prev & 0x0f) << 4 | cur >> 2));
break;
case 3:
result.push(String.fromCharCode((prev & 3) << 6 | cur));
break;
}
prev = cur;
i++;
}
return result.join('');
}