反正上周在解决开发公司小工具时,需要将用户输入的字符串加密处理传输,那就直接贴上代码吧,希望能帮上你们:
strToArrayBufferToBase64(str) {
let pos = 0,
len = str.length,
buffer = new Array(),
binary = "",
bytes,
bytesLen;
if (len % 2 != 0) {
str.padStart(len + 1, 0)
}
len /= 2;
for (let i = 0; i < len; i++) {
let s = str.substr(pos, 2);
let v = parseInt(s, 16);
buffer.push(v);
pos += 2;
}
bytes = new Uint8Array(buffer);
bytesLen = bytes.byteLength;
for (let i = 0; i < len; i++) {
binary += String.fromCharCode(bytes[i]);
}
return window.btoa(binary);
},
大家肯定自己也能想办法解决的,我记录下来的目的也是想让自己在开发过程中遇到不懂的能慢慢一点一滴的的汇聚起来!