Created
September 29, 2015 06:28
-
-
Save dbaron/acee89cb2039f0425f2e to your computer and use it in GitHub Desktop.
Emulating the sha256sum command on the Web
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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