[JavaScript] 纯文本查看 复制代码
const afX = ['K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'];
let afY = new Array(128).fill(-1);
function init() {
for (let i = 0; i < afX.length; i++) {
afY[afX.charCodeAt(0)] = i;
}
}
function encode(byteArray) {
let sb = [];
for (let i = 0; i < byteArray.length; i += 3) {
let bArr2 = new Array(4).fill(0);
let b = 0;
for (let i2 = 0; i2 <= 2; i2++) {
let i3 = i + i2;
if (i3 <= byteArray.length - 1) {
bArr2[i2] = b | (byteArray[i3] >>> ((i2 * 2) + 2));
b = (((byteArray[i3] << ((2 - i2) * 2 + 2)) & 0xFF) >>> 2);
} else {
bArr2[i2] = b;
b = 64;
}
}
bArr2[3] = b;
for (let i4 = 0; i4 <= 3; i4++) {
if (bArr2[i4] <= 63) {
sb.push(afX[bArr2[i4]]);
} else {
sb.push('=');
}
}
}
return sb.join('');
}
function decode(str) {
let byteArrayOutputStream = [];
let bytes = new TextEncoder().encode(str);
let bArr = new Array(bytes.length).fill(0);
for (let i = 0; i < bytes.length; i++) {
bArr = afY[bytes];
}
for (let i2 = 0; i2 < bArr.length; i2 += 4) {
let bArr2 = new Array(3).fill(0);
let i3 = 0;
for (let i4 = 0; i4 <= 2; i4++) {
let i5 = i2 + i4;
let i6 = i5 + 1;
if (i6 <= bArr.length - 1 && bArr[i6] >= 0) {
bArr2[i4] = (((bArr[i5] << ((i4 * 2) + 2)) & 0xFF) | (bArr[i6] >>> (((2 - (i4 + 1)) * 2) + 2)));
i3++;
}
}
for (let i7 = 0; i7 < i3; i7++) {
byteArrayOutputStream.push(bArr2[i7]);
}
}
return new Uint8Array(byteArrayOutputStream);
}
init();