-1

Given the string hello world how do I get the string b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9 as provided by the "SHA256 Online" generator found here: https://emn178.github.io/online-tools/sha256.html.

I'm looking to use Deno for this. There aren't many good resources online about how to do this so I wanted to create a single reference that was easier to find.

ThomasReggi
  • 55,053
  • 85
  • 237
  • 424

1 Answers1

0

Deno uses the web crypto api, so how you'd achieve this in the browser is the same as with Deno. This answer was taken from here.

async function hash(message) {
  const data = new TextEncoder().encode(message);
  const hashBuffer = await crypto.subtle.digest('SHA-256', data)
  const hashArray = Array.from(new Uint8Array(hashBuffer))
  const hashHex = hashArray.map((b) => b.toString(16).padStart(2, '0')).join('')
  return hashHex
}

hash('hello world').then(console.log)
ThomasReggi
  • 55,053
  • 85
  • 237
  • 424