Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crypto: add crypto.hash #42233

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions benchmark/crypto/oneshot-hash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';
const common = require('../common.js');
const { hash, createHash } = require('crypto');

const bench = common.createBenchmark(main, {
mode: ['oneshot', 'stateful'],
algo: ['sha1', 'sha256', 'sha512'],
len: [2, 128, 1024, 64 * 1024],
n: [1e5],
});

function testOneshot(algo, data, n) {
bench.start();
for (let i = 0; i < n; i++)
hash(algo, data);
bench.end(n);
}

function testStateful(algo, data, n) {
bench.start();
for (let i = 0; i < n; i++)
createHash(algo).update(data).digest();
bench.end(n);
}

function main({ mode, algo, len, n }) {
const data = Buffer.alloc(len);
if (mode === 'oneshot') {
testOneshot(algo, data, n);
} else {
testStateful(algo, data, n);
}
}
4 changes: 3 additions & 1 deletion lib/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ const {
} = require('internal/crypto/sig');
const {
Hash,
Hmac
Hmac,
hash
} = require('internal/crypto/hash');
const {
X509Certificate
Expand Down Expand Up @@ -199,6 +200,7 @@ module.exports = {
getCurves,
getDiffieHellman: createDiffieHellmanGroup,
getHashes,
hash,
hkdf,
hkdfSync,
pbkdf2,
Expand Down
19 changes: 19 additions & 0 deletions lib/internal/crypto/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const {
HashJob,
Hmac: _Hmac,
kCryptoJobAsync,
kCryptoJobSync,
} = internalBinding('crypto');

const {
Expand Down Expand Up @@ -161,6 +162,23 @@ Hmac.prototype.digest = function digest(outputEncoding) {
Hmac.prototype._flush = Hash.prototype._flush;
Hmac.prototype._transform = Hash.prototype._transform;

function hash(algorithm, data, options) {
validateString(algorithm, 'algorithm');
if (!isArrayBufferView(data)) {
throw new ERR_INVALID_ARG_TYPE(
'data', ['Buffer', 'TypedArray', 'DataView'], data);
}
const xofLen = typeof options === 'object' && options !== null ?
options.outputLength : undefined;
if (xofLen !== undefined)
validateUint32(xofLen, 'options.outputLength');
const { 0: err, 1: digest } =
new HashJob(kCryptoJobSync, algorithm, data, xofLen).run();
if (err !== undefined)
throw err;
return digest;
}

// Implementation for WebCrypto subtle.digest()

async function asyncDigest(algorithm, data) {
Expand All @@ -181,5 +199,6 @@ async function asyncDigest(algorithm, data) {
module.exports = {
Hash,
Hmac,
hash,
asyncDigest,
};