Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest#converting_a_digest_to_a_hex_string
- /*
- Open a new browser tab or window and paste the following in your developer console (CTRL + SHIFT + I)
- (You may need to type "allow pasting", as indicated by your browser)
- */
- const text =
- "An obscure body in the S-K System, your majesty. The inhabitants refer to it as the planet Earth.";
- async function digestMessage(message) {
- const msgUint8 = new TextEncoder().encode(message); // encode as (utf-8) Uint8Array
- const hashBuffer = await crypto.subtle.digest("SHA-256", msgUint8); // hash the message
- const hashArray = Array.from(new Uint8Array(hashBuffer)); // convert buffer to byte array
- const hashHex = hashArray
- .map((b) => b.toString(16).padStart(2, "0"))
- .join(""); // convert bytes to hex string
- return hashHex;
- }
- /*
- Get the sha256 hash for any string, in this case the string assigned to the text variable
- */
- const result = await digestMessage(text);
- /*
- The result is "6efd383745a964768989b9df420811abc6e5873f874fc22a76fe9258e020c2e1"
- Repeat this with any other string
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement