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
- // The function from MDN has been modified to provide a double sha256
- /*
- 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 provideSha256ByteArray(message) {
- const msgUint8 = new TextEncoder().encode(message);
- const hashBuffer = await crypto.subtle.digest("SHA-256", msgUint8);
- return new Uint8Array(hashBuffer);
- }
- async function provideDoubleSha256(message){
- const sha256ByteArray = await provideSha256ByteArray(message);
- const hashBuffer = await crypto.subtle.digest("SHA-256", sha256ByteArray);
- const hashBufferByteArray = new Uint8Array(hashBuffer);
- const hashArray = Array.from(hashBufferByteArray);
- const hashHex = hashArray
- .map((b) => b.toString(16).padStart(2, "0"))
- .join("");
- return hashHex;
- }
- /*
- Get the double sha256 hash for any string, in this case the string assigned to the text variable
- */
- const result = await provideDoubleSha256(text);
- /*
- The result is "255c3a66cd15953821c8c3a04dc22d10a826209a705d13f8072f674c2bf3294c"
- Repeat this with any other string
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement