Open thernstig opened 1 year ago
AFAICT this library relies on Node’s .createHash()
exclusively, which is not supported even by browsers supporting globalThis.crypto
(nor is it in the Web Crypto spec).
@strogonoff I am not sure I follow, would something like this not work? The intention is to replace .createHash()
with the Web Crypto API.
const str = "sdomaosdjasjdklasdkjhsdfjkhsdi23894u89weuriuhsduih8i9y893eyu8923hduhdjik3d";
const encode = new TextEncoder();
const digest = await webcrypto.subtle.digest('SHA-512', encode.encode(str));
return Buffer.from(digest).toString('hex');
@thernstig Ah yes. I was actually thinking along the same lines, that it’s possible to implement it in a way that works in both Node and browser.
// web version
new Uint8Array(await crypto.subtle.digest('SHA-1', (new TextEncoder()).encode('…'))).toString()
// Node version
new Uint8Array(await require('crypto').webcrypto.subtle.digest('SHA-1', (new TextEncoder()).encode('…'))).toString()
I saw that currently the library relies on Node’s flow, and I guess browserify
fills in the gaps so that it works in browser (including browsers that may not support web crypto), that’s what I meant. But yes, it doesn’t have to be that way I guess.
(Note that Buffer
is a Node-only construct, so an isomorphic solution would probably use Uint8Array
instead.)
Yes. So the main gist of using the Web Crypto API is that it supports await
making it async. Second thing is of course that the Web Crypto API is probably the future for Node.js as well.
To me the reason for looking into this was not async but clearer and isomorphic logic. Currently there is browserify adding some unnecessary magic and increasing bundle size. Async is a nice bonus though.
Are there any plans for this to be released?
https://nodejs.org/api/webcrypto.html supports async hash functions. Meaning that if this libs implements an async function like:
Then users could use async functionality for this.
So the async version should be made clear to depend on Node.js and browsers supporting
globalThis.crypto
.