Skip to content

Commit 47cca06

Browse files
calvinmetcalfjasnell
authored andcommitted
crypto: better error message for createHash
calling digest or update on a hash object after digest has been called now gives a topical error message instead of an error message saying that the hash failed to initialize. PR-URL: #6042 Reviewed-By: Brian White <mscdex@mscdex.net> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 67eed63 commit 47cca06

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

src/node_crypto.cc

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3676,6 +3676,7 @@ bool Hash::HashInit(const char* hash_type) {
36763676
return false;
36773677
}
36783678
initialised_ = true;
3679+
finalized_ = false;
36793680
return true;
36803681
}
36813682

@@ -3695,6 +3696,13 @@ void Hash::HashUpdate(const FunctionCallbackInfo<Value>& args) {
36953696

36963697
THROW_AND_RETURN_IF_NOT_STRING_OR_BUFFER(args[0], "Data");
36973698

3699+
if (!hash->initialised_) {
3700+
return env->ThrowError("Not initialized");
3701+
}
3702+
if (hash->finalized_) {
3703+
return env->ThrowError("Digest already called");
3704+
}
3705+
36983706
// Only copy the data if we have to, because it's a string
36993707
bool r;
37003708
if (args[0]->IsString()) {
@@ -3722,6 +3730,9 @@ void Hash::HashDigest(const FunctionCallbackInfo<Value>& args) {
37223730
if (!hash->initialised_) {
37233731
return env->ThrowError("Not initialized");
37243732
}
3733+
if (hash->finalized_) {
3734+
return env->ThrowError("Digest already called");
3735+
}
37253736

37263737
enum encoding encoding = BUFFER;
37273738
if (args.Length() >= 1) {
@@ -3735,7 +3746,7 @@ void Hash::HashDigest(const FunctionCallbackInfo<Value>& args) {
37353746

37363747
EVP_DigestFinal_ex(&hash->mdctx_, md_value, &md_len);
37373748
EVP_MD_CTX_cleanup(&hash->mdctx_);
3738-
hash->initialised_ = false;
3749+
hash->finalized_ = true;
37393750

37403751
Local<Value> rc = StringBytes::Encode(env->isolate(),
37413752
reinterpret_cast<const char*>(md_value),

src/node_crypto.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,7 @@ class Hash : public BaseObject {
538538
EVP_MD_CTX mdctx_; /* coverity[member_decl] */
539539
const EVP_MD* md_; /* coverity[member_decl] */
540540
bool initialised_;
541+
bool finalized_;
541542
};
542543

543544
class SignBase : public BaseObject {

test/parallel/test-crypto-hash.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,15 @@ assert.equal(
9898
assert.notEqual(
9999
hutf8,
100100
crypto.createHash('sha512').update('УТФ-8 text', 'binary').digest('hex'));
101+
102+
var h3 = crypto.createHash('sha256');
103+
h3.digest();
104+
assert.throws(function() {
105+
h3.digest();
106+
},
107+
/Digest already called/);
108+
109+
assert.throws(function() {
110+
h3.update('foo');
111+
},
112+
/Digest already called/);

0 commit comments

Comments
 (0)