react-native-cryptopp icon indicating copy to clipboard operation
react-native-cryptopp copied to clipboard

Hashs & MAC chunk by chunk

Open tex0l opened this issue 3 years ago • 3 comments

Hi!

Thank you for your amazing reactivity :)!

I was wondering if you were planning to expose a way to calculate MACs and hashes in multiple stages:

  • creation to set up the mac or hash object;
  • update to add the chunks to and update its internal state;
  • digest to finalize and return the mac or hash value.

We use it to calculate MACs of very large files without passing an ArrayBuffer of the file: https://github.com/seald/sscrypto/blob/c54c0fdb9f9cd4b311f031a9db1967bfd330b6f7/src/forge/aes.ts#L73

We can trick it for AES CBC without having to remember an internal state, but for hashes and MACs it is impossible (as far as I know).

Would you consider implementing such a feature? May we help on this?

tex0l avatar Apr 13 '22 09:04 tex0l

Right, that would be the ideal solution and how other libraries do it as well. The issue is that my current knowledge of the JSI Host Objects is not the greatest. I just didn't have enough time to look into it for now.

Cryptopp also does it this way, example from the [wiki]:(https://www.cryptopp.com/wiki/HMAC)

HMAC<SHA1> hmac(k, sizeof(k));
hmac.Update(m, sizeof(m));

byte d[HMAC<SHA1>::DIGESTSIZE];
hmac.Final(d);

If you want to help, I would be more than happy to include it in the code.

JiriHoffmann avatar Apr 13 '22 16:04 JiriHoffmann

Hey @tex0l! Progressive hashing is up on master. Note that I changed hashFunctions to just hash. HMAC & CMAC will be next.

Snippet from example: https://github.com/JiriHoffmann/react-native-cryptopp/blob/19353af049b5fb7f50762c172fd182fa64130353/example/src/App.tsx#L120-L122

JiriHoffmann avatar Jun 04 '22 15:06 JiriHoffmann

On this, I managed to implement an HMAC in JS with the progressive SHA256 ;) it works perfectly!

tex0l avatar Jul 26 '22 08:07 tex0l