Skip to content

Instantly share code, notes, and snippets.

@dbaron
Created September 29, 2015 06:28
Show Gist options
  • Save dbaron/acee89cb2039f0425f2e to your computer and use it in GitHub Desktop.
Save dbaron/acee89cb2039f0425f2e to your computer and use it in GitHub Desktop.
Emulating the sha256sum command on the Web
<!DOCTYPE HTML>
<meta charset="UTF-8">
<title>sha256sum</title>
<script>
function buffer_to_hex(buffer)
{
return Array.from(new Uint8Array(buffer)).map((e) => ("0" + Number(e).toString(16)).slice(-2)).join("");
}
function sha256_string(s)
{
var bytes = (new TextEncoder("UTF-8")).encode(s);
return crypto.subtle.digest("SHA-256", bytes).then((hashBuffer) => buffer_to_hex(hashBuffer));
}
sha256_string("hello").then((res) => { console.log("hello hashes to", res); },
(e) => { console.log("exception:", e); });
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment