Last active
July 12, 2022 10:39
-
-
Save eligrey/8335f09276492e69b747fb4017e9570e to your computer and use it in GitHub Desktop.
Simple cryptographic hashing function for ArrayBuffers in browsers
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
/** | |
* Get the cryptographic hash of an ArrayBuffer | |
* | |
* @param ab - ArrayBuffer to digest | |
* @param algorithm - Cryptographic hash digest algorithm | |
* @returns Hexadecimal hash digest string | |
*/ | |
export const hash = async ( | |
algorithm: string, | |
ab: ArrayBuffer | Promise<ArrayBuffer>, | |
): Promise<string> => | |
new Uint8Array(await crypto.subtle.digest(algorithm, await ab)).reduce( | |
(memo, i) => memo + i.toString(16).padStart(2, '0'), | |
'', | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment