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

Commit

Permalink
crypto: throw exception on unknown digest method
Browse files Browse the repository at this point in the history
Fixes #2227.
  • Loading branch information
bnoordhuis committed Nov 30, 2011
1 parent bbd976b commit f44d0b9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/node_crypto.cc
Expand Up @@ -2890,10 +2890,7 @@ class Hash : public ObjectWrap {

bool HashInit (const char* hashType) {
md = EVP_get_digestbyname(hashType);
if(!md) {
fprintf(stderr, "node-crypto : Unknown message digest %s\n", hashType);
return false;
}
if(!md) return false;
EVP_MD_CTX_init(&mdctx);
EVP_DigestInit_ex(&mdctx, md, NULL);
initialised_ = true;
Expand All @@ -2917,13 +2914,16 @@ class Hash : public ObjectWrap {
"Must give hashtype string as argument")));
}

Hash *hash = new Hash();
hash->Wrap(args.This());

String::Utf8Value hashType(args[0]->ToString());

hash->HashInit(*hashType);
Hash *hash = new Hash();
if (!hash->HashInit(*hashType)) {
delete hash;
return ThrowException(Exception::Error(String::New(
"Digest method not supported")));
}

hash->Wrap(args.This());
return args.This();
}

Expand Down
5 changes: 5 additions & 0 deletions test/simple/test-crypto.js
Expand Up @@ -278,6 +278,11 @@ fileStream.on('close', function() {
'Test SHA1 of sample.png');
});

// Issue #2227: unknown digest method should throw an error.
assert.throws(function() {
crypto.createHash('xyzzy');
});

// Test signing and verifying
var s1 = crypto.createSign('RSA-SHA1')
.update('Test123')
Expand Down

0 comments on commit f44d0b9

Please sign in to comment.