test.js 483 B

123456789101112131415161718
  1. async function sha256(message) {
  2. // encode as UTF-8
  3. const msgBuffer = new TextEncoder().encode(message);
  4. // hash the message
  5. const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer);
  6. // convert ArrayBuffer to Array
  7. const hashArray = Array.from(new Uint8Array(hashBuffer));
  8. // convert bytes to hex string
  9. return hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
  10. }
  11. sha256("212121").then((hash) => {
  12. console.log(hash);
  13. })