57 lines
1.4 KiB
JavaScript
57 lines
1.4 KiB
JavaScript
// base64Utils.js
|
|
let _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
|
|
const useBase64 = {
|
|
encode64:(e) => {
|
|
let t = "";
|
|
let f = 0;
|
|
e = useBase64.encodeUTF8(e);
|
|
while (f < e.length) {
|
|
const n = e.charCodeAt(f++);
|
|
const r = e.charCodeAt(f++);
|
|
const i = e.charCodeAt(f++);
|
|
let s = n >> 2;
|
|
let o = (n & 3) << 4 | r >> 4;
|
|
let u = (r & 15) << 2 | i >> 6;
|
|
let a = i & 63;
|
|
if (isNaN(r)) {
|
|
u = a = 64;
|
|
} else if (isNaN(i)) {
|
|
a = 64;
|
|
}
|
|
t += _keyStr[s] + _keyStr[o] + _keyStr[u] + _keyStr[a];
|
|
}
|
|
return t;
|
|
},
|
|
decode64: (e) => {
|
|
let t = "";
|
|
let f = 0;
|
|
e = e.replace(/[^A-Za-z0-9+/=]/g, "");
|
|
while (f < e.length) {
|
|
const s = _keyStr.indexOf(e.charAt(f++));
|
|
const o = _keyStr.indexOf(e.charAt(f++));
|
|
const u = _keyStr.indexOf(e.charAt(f++));
|
|
const a = _keyStr.indexOf(e.charAt(f++));
|
|
let n = s << 2 | o >> 4;
|
|
let r = (o & 15) << 4 | u >> 2;
|
|
let i = (u & 3) << 6 | a;
|
|
t += String.fromCharCode(n);
|
|
if (u !== 64) {
|
|
t += String.fromCharCode(r);
|
|
}
|
|
if (a !== 64) {
|
|
t += String.fromCharCode(i);
|
|
}
|
|
}
|
|
return useBase64.decodeUTF8(t);
|
|
},
|
|
|
|
encodeUTF8: (input) => {
|
|
return unescape(encodeURIComponent(input));
|
|
},
|
|
|
|
decodeUTF8: (input) => {
|
|
return decodeURIComponent(escape(input));
|
|
},
|
|
}
|
|
export default useBase64;
|