当前位置:网站首页>Randomly generate session_ id

Randomly generate session_ id

2022-07-07 05:46:00 Month better

 Insert picture description here

// 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(" Random generation session_id:", randomSessionId());
原网站

版权声明
本文为[Month better]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207062345307002.html