Uint8Array转换成Base64

详细官方地址:https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues
https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder

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));
  crypto.getRandomValues(ua.subarray(4, 20));
  return (
    "1." +
    transformUint8ArrayToBase64(ua)
      .replaceAll("+", "-")
      .replaceAll("/", "_")
  );
},

Guess you like

Origin blog.csdn.net/Kiruthika/article/details/120301193