Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3676,6 +3676,7 @@ bool Hash::HashInit(const char* hash_type) {
return false;
}
initialised_ = true;
finalized_ = false;
return true;
}

Expand All @@ -3695,6 +3696,13 @@ void Hash::HashUpdate(const FunctionCallbackInfo<Value>& 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()) {
Expand Down Expand Up @@ -3722,6 +3730,9 @@ void Hash::HashDigest(const FunctionCallbackInfo<Value>& 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) {
Expand All @@ -3735,7 +3746,7 @@ void Hash::HashDigest(const FunctionCallbackInfo<Value>& args) {

EVP_DigestFinal_ex(&hash->mdctx_, md_value, &md_len);
EVP_MD_CTX_cleanup(&hash->mdctx_);
hash->initialised_ = false;
hash->finalized_ = true;

Local<Value> rc = StringBytes::Encode(env->isolate(),
reinterpret_cast<const char*>(md_value),
Expand Down
1 change: 1 addition & 0 deletions src/node_crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions test/parallel/test-crypto-hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/);