mirror of
https://github.com/nodejs/node.git
synced 2025-05-03 15:17:17 +00:00
crypto: add crypto.getHashes()
This commit is contained in:
parent
f53441ab1f
commit
14a6c4efb8
@ -21,6 +21,16 @@ Example:
|
|||||||
console.log(ciphers); // ['AES128-SHA', 'AES256-SHA', ...]
|
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', ...]
|
||||||
|
|
||||||
|
|
||||||
## crypto.createCredentials(details)
|
## crypto.createCredentials(details)
|
||||||
|
|
||||||
Creates a credentials object, with the optional details being a dictionary with keys:
|
Creates a credentials object, with the optional details being a dictionary with keys:
|
||||||
|
@ -35,6 +35,7 @@ try {
|
|||||||
var randomBytes = binding.randomBytes;
|
var randomBytes = binding.randomBytes;
|
||||||
var pseudoRandomBytes = binding.pseudoRandomBytes;
|
var pseudoRandomBytes = binding.pseudoRandomBytes;
|
||||||
var getCiphers = binding.getCiphers;
|
var getCiphers = binding.getCiphers;
|
||||||
|
var getHashes = binding.getHashes;
|
||||||
var crypto = true;
|
var crypto = true;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
||||||
@ -196,3 +197,18 @@ exports.rng = randomBytes;
|
|||||||
exports.prng = pseudoRandomBytes;
|
exports.prng = pseudoRandomBytes;
|
||||||
|
|
||||||
exports.getCiphers = getCiphers;
|
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'.
|
||||||
|
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;
|
||||||
|
};
|
||||||
|
@ -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) {
|
void InitCrypto(Handle<Object> target) {
|
||||||
HandleScope scope;
|
HandleScope scope;
|
||||||
|
|
||||||
@ -4686,6 +4703,7 @@ void InitCrypto(Handle<Object> target) {
|
|||||||
NODE_SET_METHOD(target, "randomBytes", RandomBytes<RAND_bytes>);
|
NODE_SET_METHOD(target, "randomBytes", RandomBytes<RAND_bytes>);
|
||||||
NODE_SET_METHOD(target, "pseudoRandomBytes", RandomBytes<RAND_pseudo_bytes>);
|
NODE_SET_METHOD(target, "pseudoRandomBytes", RandomBytes<RAND_pseudo_bytes>);
|
||||||
NODE_SET_METHOD(target, "getCiphers", GetCiphers);
|
NODE_SET_METHOD(target, "getCiphers", GetCiphers);
|
||||||
|
NODE_SET_METHOD(target, "getHashes", GetHashes);
|
||||||
|
|
||||||
subject_symbol = NODE_PSYMBOL("subject");
|
subject_symbol = NODE_PSYMBOL("subject");
|
||||||
issuer_symbol = NODE_PSYMBOL("issuer");
|
issuer_symbol = NODE_PSYMBOL("issuer");
|
||||||
|
@ -687,3 +687,10 @@ testPBKDF2('pass\0word', 'sa\0lt', 4096, 16,
|
|||||||
// Assume that we have at least AES256-SHA.
|
// Assume that we have at least AES256-SHA.
|
||||||
assert.notEqual(0, crypto.getCiphers());
|
assert.notEqual(0, crypto.getCiphers());
|
||||||
assert.notEqual(-1, crypto.getCiphers().indexOf('AES256-SHA'));
|
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'));
|
||||||
|
Loading…
Reference in New Issue
Block a user