js-sha256: 一个浏览器端的加密方式
一个类似md5的加密方式
实现方式1
function hash(string) {
  const utf8 = new TextEncoder().encode(string);
  return crypto.subtle.digest('SHA-256', utf8).then((hashBuffer) => {
    const hashArray = Array.from(new Uint8Array(hashBuffer));
    const hashHex = hashArray
      .map((bytes) => bytes.toString(16).padStart(2, '0'))
      .join('');
    return hashHex;
  });
}
hash('foo').then((hex) => console.log(hex)); // '2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae'推荐方式2
兼容性更好的。
<script src="https://cdn.jsdelivr.net/npm/js-sha256@0.9.0/build/sha256.min.js"></script>
<script>
  const hash = sha256('hello world');
  console.log(hash);
  // 输出: b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
</script>NodeJS端
const { createHash } = require('crypto');
function hash(string) {
  return createHash('sha256').update(string).digest('hex');
}
const hash = sha256('hello world');
console.log(hash); # b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9