
function randomSessionId() {
let ua = new Uint8Array(20);
new DataView(ua.buffer).setUint32(0, Math.floor(+new Date() / 1000));
let crypto = window.crypto || window.msCrypto;
if (crypto) {
crypto.getRandomValues(ua.subarray(4, 20));
}
return (
"1." +
transformUint8ArrayToBase64(ua)
.replaceAll("+", "-")
.replaceAll("/", "_")
);
}
解决方法为:
function randomSessionId() {
let ua = new Uint8Array(20);
new DataView(ua.buffer).setUint32(0, Math.floor(+new Date() / 1000));
let crypto = window.crypto || window.msCrypto;
if (crypto) {
crypto.getRandomValues(ua.subarray(4, 20));
}
return (
"1." +
transformUint8ArrayToBase64(ua)
.replace(/\+/g, "-")
.replace(/\//g, "_")
);
}
本文介绍了一种生成随机会话ID的方法,通过结合当前时间戳与浏览器提供的加密随机数生成唯一标识符,并采用Base64编码确保其独特性和不可预测性。
2816

被折叠的 条评论
为什么被折叠?



