Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

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

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

Comments

@marcello3d
Copy link

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
Copy link

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?

@marcello3d
Copy link
Author

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

@bnoordhuis
Copy link
Member

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

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

No branches or pull requests

3 participants