antelle / argon2-browser

Argon2 library compiled for browser runtime
https://antelle.net/argon2-browser
MIT License
367 stars 79 forks source link

Trouble using verify #25

Closed mymindstorm closed 4 years ago

mymindstorm commented 4 years ago

Hi,

I've been using the example webpack code, and I'm having some trouble getting verify to work.

Broken code ```js const argon2 = require('argon2-browser'); argon2 .hash({ pass: 'p@ssw0rd', salt: 'somesalt' }) .then(hash => { console.log(hash); argon2 .verify({ pass: 'p@ssw0rd', encoded: hash.encoded }) .then(() => console.log('Hash correct')) .catch(e => console.log(e)); }); ```

The above code generates a hash correctly, but it never fulfills or rejects the argon2.verify promise.

Working code ```js argon2 .verify({ pass: 'p@ssw0rd', encoded: '$argon2d$v=19$m=1024,t=1,p=1$c29tZXNhbHQ$JGOjI98eRw4ahgg28250Fz2BKEFl48Xm' }) .then(() => console.log('Hash correct')) .catch(e => console.log(e)); ```

I then manually copied the string into another promise, which works fine and reaches 'Hash correct'.


Additionally, I found that if you try to do more than one thing at once, it will have problems allocating memory.

More bad code ```js const argon2 = require('argon2-browser'); argon2 .hash({ pass: 'p@ssw0rd', salt: 'somesalt' }) .then(hash => { console.log(hash); }); argon2 .hash({ pass: 'p@ssw0rd', salt: 'somesalt' }) .then(hash => { console.log(hash); }); ```

This will fail with

argon2.js:77 Uncaught (in promise) TypeError: Module.allocate is not a function
    at allocateArray (argon2.js:77)
    at eval (argon2.js:106)

Not sure if I'm just doing something wrong. I'm hoping that you could point me in the right direction.

antelle commented 4 years ago

Thanks for reporting this! That's what the bug report should look like. 🎁 There are actually two issues here:

  1. subsequent initialization always threw an error, fixed in cab936d8aae21103a13af98f78e76d8e7cf24997
  2. strings were allocated without a null terminator, fixed in 31bbf70336870852e4fe5be74c0b64aeb9b6b709

Published v1.7.0, updated the usage example, now your code works in webpack and node.js versions.

mymindstorm commented 4 years ago

Thanks for the quick fix!

mymindstorm commented 4 years ago

cab936d confirmed fixed.

31bbf70 does not fix, still getting e.allocate is not a function with the second bad code sample.

antelle commented 4 years ago

Fixed that one too (it passed in node, but not in browser).