This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

crypto: add crypto.getHashes()

  • Loading branch information...
bnoordhuis committed Oct 13, 2012
1 parent f53441a commit 14a6c4efb853fe853698b9c8d6dc619fbe8a206d
Showing with 51 additions and 0 deletions.
  1. +10 −0 doc/api/crypto.markdown
  2. +16 −0 lib/crypto.js
  3. +18 −0 src/node_crypto.cc
  4. +7 −0 test/simple/test-crypto.js
View
@@ -21,6 +21,16 @@ Example:
console.log(ciphers); // ['AES128-SHA', 'AES256-SHA', ...]
+## crypto.getHashes()
+
+Returns an array with the names of the supported hash algorithms.
+
+Example:
+
+ var hashes = crypto.getHashes();
+ console.log(hashes); // ['sha', 'sha1', 'sha1WithRSAEncryption', ...]

This comment has been minimized.

Show comment Hide comment
@mscdex

mscdex Oct 13, 2012

If we're lowercasing everything, the third example item is not expected...

@mscdex

mscdex Oct 13, 2012

If we're lowercasing everything, the third example item is not expected...

+
+
## crypto.createCredentials(details)
Creates a credentials object, with the optional details being a dictionary with keys:
View
@@ -35,6 +35,7 @@ try {
var randomBytes = binding.randomBytes;
var pseudoRandomBytes = binding.pseudoRandomBytes;
var getCiphers = binding.getCiphers;
+ var getHashes = binding.getHashes;
var crypto = true;
} catch (e) {
@@ -196,3 +197,18 @@ exports.rng = randomBytes;
exports.prng = pseudoRandomBytes;
exports.getCiphers = getCiphers;
+
+exports.getHashes = function() {
+ var names = getHashes.call(null, arguments);
+
+ // Drop all-caps names in favor of their lowercase aliases,
+ // for example, 'sha1' instead of 'SHA1'.

This comment has been minimized.

Show comment Hide comment
@mscdex

mscdex Oct 13, 2012

Won't this be kind of confusing when someone goes to use these lowercased names, as OpenSSL's names are case-sensitive right?

@mscdex

mscdex Oct 13, 2012

Won't this be kind of confusing when someone goes to use these lowercased names, as OpenSSL's names are case-sensitive right?

This comment has been minimized.

Show comment Hide comment
@bnoordhuis

bnoordhuis Oct 13, 2012

Member

Yes, but the strings "sha1" and "SHA1" are aliases for the same algorithm. This code removes the ALL CAPS duplicates and only those, e.g. "sha1WithRSAEncryption" is not filtered out.

@bnoordhuis

bnoordhuis Oct 13, 2012

Member

Yes, but the strings "sha1" and "SHA1" are aliases for the same algorithm. This code removes the ALL CAPS duplicates and only those, e.g. "sha1WithRSAEncryption" is not filtered out.

+ var ctx = {};
+ names = names.forEach(function(name) {
+ if (/^[0-9A-Z\-]+$/.test(name)) name = name.toLowerCase();
+ ctx[name] = true;
+ });
+ names = Object.getOwnPropertyNames(ctx);
+
+ return names;
+};
View
@@ -4646,6 +4646,23 @@ Handle<Value> GetCiphers(const Arguments& args) {
}
+static void add_hash_to_array(const EVP_MD* md,
+ const char* from,
+ const char* to,
+ void* arg) {
+ Local<Array>& arr = *static_cast<Local<Array>*>(arg);
+ arr->Set(arr->Length(), String::New(from));
+}
+
+
+Handle<Value> GetHashes(const Arguments& args) {
+ HandleScope scope;
+ Local<Array> arr = Array::New();
+ EVP_MD_do_all_sorted(add_hash_to_array, &arr);
+ return scope.Close(arr);
+}
+
+
void InitCrypto(Handle<Object> target) {
HandleScope scope;
@@ -4686,6 +4703,7 @@ void InitCrypto(Handle<Object> target) {
NODE_SET_METHOD(target, "randomBytes", RandomBytes<RAND_bytes>);
NODE_SET_METHOD(target, "pseudoRandomBytes", RandomBytes<RAND_pseudo_bytes>);
NODE_SET_METHOD(target, "getCiphers", GetCiphers);
+ NODE_SET_METHOD(target, "getHashes", GetHashes);
subject_symbol = NODE_PSYMBOL("subject");
issuer_symbol = NODE_PSYMBOL("issuer");
@@ -687,3 +687,10 @@ testPBKDF2('pass\0word', 'sa\0lt', 4096, 16,
// Assume that we have at least AES256-SHA.
assert.notEqual(0, crypto.getCiphers());
assert.notEqual(-1, crypto.getCiphers().indexOf('AES256-SHA'));
+
+// Assert that we have sha and sha1 but not SHA and SHA1.
+assert.notEqual(0, crypto.getHashes());
+assert.notEqual(-1, crypto.getHashes().indexOf('sha1'));
+assert.notEqual(-1, crypto.getHashes().indexOf('sha'));
+assert.equal(-1, crypto.getHashes().indexOf('SHA1'));
+assert.equal(-1, crypto.getHashes().indexOf('SHA'));

0 comments on commit 14a6c4e

Please sign in to comment.