Skip to content

JS SHA256 加密

Published: at 05:27 PMSuggest Changes

问题

JS Sha256 加密

解决

网上找到了个不错的函数,可以不用库了

async function sha256(message) {
  // encode as UTF-8
  const msgBuffer = new TextEncoder().encode(message);

  // hash the message
  const hashBuffer = await crypto.subtle.digest("SHA-256", msgBuffer);

  // convert ArrayBuffer to Array
  const hashArray = Array.from(new Uint8Array(hashBuffer));

  // convert bytes to hex string
  const hashHex = hashArray
    .map((b) => b.toString(16).padStart(2, "0"))
    .join("");
  return hashHex;
}

Previous Post
C 语言学习笔记:十进制转二进制
Next Post
JS 获取浏览器指纹