Last active
November 7, 2023 04:20
-
-
Save nichoth/b63e0853b32af3ba24ccd24adc99461e to your computer and use it in GitHub Desktop.
Create a sha256 hash in the browser
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
const webcrypto = window.crypto | |
import * as uint8arrays from 'uint8arrays' | |
// data should be Uint8Array | |
function getHash (data) { | |
let _data = data | |
if (typeof data === 'string') { | |
const te = new TextEncoder() | |
_data = te.encode(data) | |
} | |
return webcrypto.subtle.digest('SHA-256', _data).then(buf => { | |
return uint8arrays.toString(new Uint8Array(buf), 'base64url') | |
// this is hex version | |
// return Array.from(new Uint8Array(buf), b => { | |
// return b.toString(16).padStart(2, '0') | |
// }).join('') | |
} | |
} |
export async function sha256(bytes: Uint8Array): Promise<Uint8Array> {
return new Uint8Array(await webcrypto.subtle.digest("sha-256", bytes))
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://doc.replicache.dev/howto/blobs#content-addressed-data