From f44d0b90442d9a1dc7457924f827722fc3a7c440 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Wed, 30 Nov 2011 14:27:12 +0100 Subject: [PATCH] crypto: throw exception on unknown digest method Fixes #2227. --- src/node_crypto.cc | 16 ++++++++-------- test/simple/test-crypto.js | 5 +++++ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/node_crypto.cc b/src/node_crypto.cc index 5aaa7ba432..bea31607ed 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -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; @@ -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(); } diff --git a/test/simple/test-crypto.js b/test/simple/test-crypto.js index 028ca46298..1469b11166 100644 --- a/test/simple/test-crypto.js +++ b/test/simple/test-crypto.js @@ -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')