crypto.createHash doesn't allow multiple digests — cryptic error message "HashUpdate fail" #749

Closed
marcello3d opened this Issue Mar 6, 2011 · 3 comments

Comments

Projects
None yet
3 participants

The following code will fail on the second call to hash.update:
var hash = require("crypto").createHash('md5')
hash.update(new Buffer(10))
console.log("hash 1 = "+hash.digest('hex'))
hash.update(new Buffer(10))
console.log("hash 2 = "+hash.digest('hex'))

It prints out the following:
hash 1 = 544a69e67a2fccbfc49e6cd6114f7f3e

node.js:116
        throw e; // process.nextTick error, or 'error' event on first tick
        ^
TypeError: HashUpdate fail
    at Object.<anonymous> (/path/to/my/test.js:4:6)

dekz commented Apr 28, 2011

This is due to .digest() putting the Digest object into an uninitialised state (initialised_ = false), and when an update is called, it returns 0 when checking if it has been initialised. Therefor currently .digest() should be used as a final method call on that object, it should not be used afterward.

To solve this issue or add additional functionality, .digestFinal() can be added which will do the same as digest does now (digest everything and set itself to uninitialised and return the digest). Digest can then be used to only return the digest and still be in an initialised state. The problem with continuing from here is, OpenSSL call to EVP_DigestFinal_ex causes the context to be unusable, so a copy needs to be made before this method call. Copying a digest context on every .digest call in hope that it may be used again might not be the most efficient way to do this, as most uses would just end with digest called once.

Thoughts?

If it's not the norm, perhaps add a digestIntermediate() method with the caveats you described?

Owner

bnoordhuis commented Jul 25, 2011

This has been fixed in de6e88c, the second call to digest() now throws a 'Not initialized' error.

bnoordhuis closed this Jul 25, 2011

@ITGuy9401 ITGuy9401 added a commit to ITGuy9401/extremelot-gildaladri-mercato that referenced this issue Apr 12, 2017

@ITGuy9401 ITGuy9401 BUGFIX 694fbc3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment