当前位置:网站首页>随机生成session_id

随机生成session_id

2022-07-06 23:45:00 月来better

在这里插入图片描述

// transformUint8ArrayToBase64
function transformUint8ArrayToBase64(array) {
    
  var binary = "";
  for (var len = array.byteLength, i = 0; i < len; i++) {
    
    binary += String.fromCharCode(array[i]);
  }
  return window.btoa(binary).replace(/=/g, "");
}
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, "_")
  );
}

console.log("随机生成session_id:", randomSessionId());
原网站

版权声明
本文为[月来better]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Kiruthika/article/details/125626929