Permalink
Browse files

crypto: throw exception on unknown digest method

Fixes #2227.
  • Loading branch information...
1 parent bbd976b commit f44d0b90442d9a1dc7457924f827722fc3a7c440 @bnoordhuis bnoordhuis committed Nov 30, 2011
Showing with 13 additions and 8 deletions.
  1. +8 −8 src/node_crypto.cc
  2. +5 −0 test/simple/test-crypto.js
View
@@ -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();
}
@@ -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')

0 comments on commit f44d0b9

Please sign in to comment.