Advertisement
benjaminvr

util-ref-edited-double-sha256

May 10th, 2024
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest#converting_a_digest_to_a_hex_string
  2. // The function from MDN has been modified to provide a double sha256
  3.  
  4. /*
  5.     Open a new browser tab or window and paste the following in your developer console (CTRL + SHIFT + I)
  6.    
  7.     (You may need to type "allow pasting", as indicated by your browser)
  8. */
  9.  
  10. const text =
  11.   "An obscure body in the S-K System, your majesty. The inhabitants refer to it as the planet Earth.";
  12.  
  13. async function provideSha256ByteArray(message) {
  14.   const msgUint8 = new TextEncoder().encode(message);
  15.   const hashBuffer = await crypto.subtle.digest("SHA-256", msgUint8);
  16.   return new Uint8Array(hashBuffer);
  17.  
  18. }
  19.  
  20. async function provideDoubleSha256(message){
  21.     const sha256ByteArray = await provideSha256ByteArray(message);
  22.     const hashBuffer = await crypto.subtle.digest("SHA-256", sha256ByteArray);
  23.     const hashBufferByteArray = new Uint8Array(hashBuffer);
  24.     const hashArray = Array.from(hashBufferByteArray);
  25.    
  26.     const hashHex = hashArray
  27.     .map((b) => b.toString(16).padStart(2, "0"))
  28.     .join("");
  29.    
  30.     return hashHex;
  31. }
  32.  
  33. /*
  34.     Get the double sha256 hash for any string, in this case the string assigned to the text variable
  35. */
  36.  
  37. const result = await provideDoubleSha256(text);
  38.  
  39. /*
  40.     The result is "255c3a66cd15953821c8c3a04dc22d10a826209a705d13f8072f674c2bf3294c"
  41.    
  42.     Repeat this with any other string
  43. */
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement