Deno: Implement HMAC variants of std/hash algorithms

Created on 1 Sep 2020  路  9Comments  路  Source: denoland/deno

The WebAssembly "createHash" implementation of std/hash is missing HMAC variants.
Should be able to provide this quite easily by taking in the hmac crate and exposing hmac<sha256> etc.

feat std

All 9 comments

Interface-wise, do we take from Node here?

createHmac(algorithm, key, ...);

I think that would be the most aligned to the rest of the implementation, and I don't see any good reason to not do that.

That being said, neither the legacy TS implementation nor the WebAssembly implementations align well to SubtleCrypto .digest() and .sign().

Is this issue still up? Can I take it up??

Feel free to go for it @mustafapc19

Since Hmac crate is not dynamic dispatch compatible. So to have nodejs kinda interface for hmac
Like this

const hash = crypto.createHmac('sha256', secretkey)
                    .update('I love cupcakes')
                   .digest('hex');

I could only think of two solution.
1) Either in createHmac just return secretkey and algo method represented in string i.e 'sha256' into some custom JSValue and and in update method we have to re-initialize Hmac in the side of rust with secretkeyand algo method from the JSValue.
2) In createHmac we initialize Hmac in the rust side and serialize into some JSValue with the algo method as string and in update,on the rust side we deserialize Hmac instance(rust) stored in the JSValue, according to the algo method stored in the JSValue, and then we apply the relevant method in the Hmac crate.

In method 1 it will initialize hmac instance(rust) everytime update is called. So createHmac will be kinda useless and would be there just to have the same api.

In method 2 we will be serializing and deserializing everytime when we do anything .

If there is any better solution plz tell, in an easy manner of course :). This issue was tagged as "good first issue" remember? :) @caspervonb

In the middle of a different pull request I don't want to stash right now, I'll get back to you in a couple of hours.

So.. first of all ignore what the structure of create_hash etc as it makes very little sense to have a class wrapper over a generated class wrapper over a dyn boxed trait. It works, it is fast so we landed it but not necessarily a sound design or an example of good usage of wasm_bindgen.

There's really no need to do dynamic dispatching in Rust at all, we're generating code as a module here not an executable and it should be up to the caller how it wants it's dispatch.

Each hash and hmac/hash pair can be exported as a class explicitly then created in JavaScript in a switch statement.

Should be able to do something like this

extern crate hmac;
extern crate sha2;

use sha2::Sha256;
use hmac::Hmac;

#[wasm_bindgen]
pub type HmacSha256(Hmac<Sha256>);

#[wasm_bindgen]
impl HmacSha256 {
  #[wasm_bindgen(constructor)]
  fn new() -> Self {
    // ...
  }
}

Let me know how it goes, no shame in passing on it if you feel it's too difficult 馃憤

I think it is pretty achievable. I am just a little busy sometimes(Final year :( ). Only issue I will run into is actually wasm_bindgen's infrastucture. I'll ping u when I do. Don't know why I thought of such lame solutions tho :D

Was this page helpful?
0 / 5 - 0 ratings

Related issues

watilde picture watilde  路  3Comments

sh7dm picture sh7dm  路  3Comments

doutchnugget picture doutchnugget  路  3Comments

ry picture ry  路  3Comments

kitsonk picture kitsonk  路  3Comments