Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Web Crypto API to enable async hashing #123

Open
thernstig opened this issue Feb 20, 2023 · 7 comments
Open

Support Web Crypto API to enable async hashing #123

thernstig opened this issue Feb 20, 2023 · 7 comments

Comments

@thernstig
Copy link

thernstig commented Feb 20, 2023

https://nodejs.org/api/webcrypto.html supports async hash functions. Meaning that if this libs implements an async function like:

import hash from 'object-hash';

await hash({foo: 'bar'}) // => '67b69634f9880a282c14a0f0cb7ba20cf5d677e9'

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.

@strogonoff
Copy link

strogonoff commented Mar 21, 2023

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).

@thernstig
Copy link
Author

thernstig commented Mar 22, 2023

@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');

@strogonoff
Copy link

@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.

@strogonoff
Copy link

(Note that Buffer is a Node-only construct, so an isomorphic solution would probably use Uint8Array instead.)

@thernstig
Copy link
Author

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.

@strogonoff
Copy link

strogonoff commented Mar 22, 2023

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.

strogonoff added a commit to paneron/object-hash that referenced this issue Sep 7, 2023
- Exposes subtle crypto as extra `subtle()` export
- Uses subtle crypto behavior by default if no Node crypto is available
  (in which case main `hash()` export becomes an async function)
strogonoff added a commit to paneron/object-hash that referenced this issue Sep 7, 2023
- Exposes subtle crypto as extra `subtle()` export
- Uses subtle crypto behavior by default if no Node crypto is available
  (in which case main `hash()` export becomes an async function)
@ivosabev
Copy link

ivosabev commented Mar 1, 2024

Are there any plans for this to be released?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants