Hash string with Web Crypto API

PHOTO EMBED

Wed Dec 22 2021 03:33:44 GMT+0000 (Coordinated Universal Time)

Saved by @Explosion #javascript #crypto

async function hash(str, iterations = 1000) {
  //Gotta love that crypto API
  const buf = await crypto.subtle.digest(
    "SHA-256",
    new TextEncoder("utf-8").encode(str)
  );
  //One liner from stackoverflow
  let result = Array.prototype.map
    .call(new Uint8Array(buf), (x) => ("00" + x.toString(16)).slice(-2))
    .join("");
  if (iterations === 0) {
    return result;
  } else {
    //Recusive function for multiple iterations
    return await hash(result, iterations - 1);
  }
}
content_copyCOPY

https://explosion-scratch.github.io/blog/0-knowledge-auth/