From 1d9451bb5aef0733f6fb5c5b95d33a31e04f5462 Mon Sep 17 00:00:00 2001 From: Calvin Metcalf Date: Mon, 4 Apr 2016 15:35:29 -0400 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/6042 Reviewed-By: Brian White Reviewed-By: James M Snell --- src/node_crypto.cc | 13 ++++++++++++- src/node_crypto.h | 1 + test/parallel/test-crypto-hash.js | 12 ++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/node_crypto.cc b/src/node_crypto.cc index 2b0f6e6fbb..1e9226c925 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -3676,6 +3676,7 @@ bool Hash::HashInit(const char* hash_type) { return false; } initialised_ = true; + finalized_ = false; return true; } @@ -3695,6 +3696,13 @@ void Hash::HashUpdate(const FunctionCallbackInfo& args) { THROW_AND_RETURN_IF_NOT_STRING_OR_BUFFER(args[0], "Data"); + if (!hash->initialised_) { + return env->ThrowError("Not initialized"); + } + if (hash->finalized_) { + return env->ThrowError("Digest already called"); + } + // Only copy the data if we have to, because it's a string bool r; if (args[0]->IsString()) { @@ -3722,6 +3730,9 @@ void Hash::HashDigest(const FunctionCallbackInfo& args) { if (!hash->initialised_) { return env->ThrowError("Not initialized"); } + if (hash->finalized_) { + return env->ThrowError("Digest already called"); + } enum encoding encoding = BUFFER; if (args.Length() >= 1) { @@ -3735,7 +3746,7 @@ void Hash::HashDigest(const FunctionCallbackInfo& args) { EVP_DigestFinal_ex(&hash->mdctx_, md_value, &md_len); EVP_MD_CTX_cleanup(&hash->mdctx_); - hash->initialised_ = false; + hash->finalized_ = true; Local rc = StringBytes::Encode(env->isolate(), reinterpret_cast(md_value), diff --git a/src/node_crypto.h b/src/node_crypto.h index 66c50efa2c..01c16b39b5 100644 --- a/src/node_crypto.h +++ b/src/node_crypto.h @@ -538,6 +538,7 @@ class Hash : public BaseObject { EVP_MD_CTX mdctx_; /* coverity[member_decl] */ const EVP_MD* md_; /* coverity[member_decl] */ bool initialised_; + bool finalized_; }; class SignBase : public BaseObject { diff --git a/test/parallel/test-crypto-hash.js b/test/parallel/test-crypto-hash.js index fce11eca04..b5fc7e881c 100644 --- a/test/parallel/test-crypto-hash.js +++ b/test/parallel/test-crypto-hash.js @@ -98,3 +98,15 @@ assert.equal( assert.notEqual( hutf8, crypto.createHash('sha512').update('УТФ-8 text', 'binary').digest('hex')); + +var h3 = crypto.createHash('sha256'); +h3.digest(); +assert.throws(function() { + h3.digest(); +}, + /Digest already called/); + +assert.throws(function() { + h3.update('foo'); +}, + /Digest already called/);