Skip to content

Instantly share code, notes, and snippets.

@eligrey
Last active July 12, 2022 10:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eligrey/8335f09276492e69b747fb4017e9570e to your computer and use it in GitHub Desktop.
Save eligrey/8335f09276492e69b747fb4017e9570e to your computer and use it in GitHub Desktop.
Simple cryptographic hashing function for ArrayBuffers in browsers
/**
* 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