mirror of
https://github.com/nodejs/node.git
synced 2026-10-14 10:26:26 -04:00
crypto: refactoring internals, add WebCrypto
Fixes: https://github.com/nodejs/node/issues/678 Refs: https://github.com/nodejs/node/issues/26854 Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: https://github.com/nodejs/node/pull/35093 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
This commit is contained in:
149 files changed
+28395
-8580
No files matched your search
@@ -0,0 +1,44 @@
|
||||
'use strict';
|
||||
|
||||
const common = require('../common.js');
|
||||
const assert = require('assert');
|
||||
const {
|
||||
hkdf,
|
||||
hkdfSync
|
||||
} = require('crypto');
|
||||
|
||||
const bench = common.createBenchmark(main, {
|
||||
sync: [0, 1],
|
||||
size: [10, 64, 1024],
|
||||
key: ['a', 'secret', 'this-is-a-much-longer-secret'],
|
||||
salt: ['', 'salt'],
|
||||
info: ['', 'info'],
|
||||
hash: ['sha256', 'sha512'],
|
||||
n: [1e3],
|
||||
});
|
||||
|
||||
function measureSync(n, size, salt, info, hash, key) {
|
||||
bench.start();
|
||||
for (let i = 0; i < n; ++i)
|
||||
hkdfSync(hash, key, salt, info, size);
|
||||
bench.end(n);
|
||||
}
|
||||
|
||||
function measureAsync(n, size, salt, info, hash, key) {
|
||||
let remaining = n;
|
||||
function done(err) {
|
||||
assert.ifError(err);
|
||||
if (--remaining === 0)
|
||||
bench.end(n);
|
||||
}
|
||||
bench.start();
|
||||
for (let i = 0; i < n; ++i)
|
||||
hkdf(hash, key, salt, info, size, done);
|
||||
}
|
||||
|
||||
function main({ n, sync, size, salt, info, hash, key }) {
|
||||
if (sync)
|
||||
measureSync(n, size, salt, info, hash, key);
|
||||
else
|
||||
measureAsync(n, size, salt, info, hash, key);
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
'use strict';
|
||||
|
||||
const common = require('../common.js');
|
||||
const assert = require('assert');
|
||||
const {
|
||||
generateKeyPair,
|
||||
generateKeyPairSync
|
||||
} = require('crypto');
|
||||
|
||||
const bench = common.createBenchmark(main, {
|
||||
method: ['rsaSync', 'rsaAsync', 'dsaSync', 'dsaAsync'],
|
||||
n: [1e2],
|
||||
});
|
||||
|
||||
const methods = {
|
||||
rsaSync(n) {
|
||||
bench.start();
|
||||
for (let i = 0; i < n; ++i) {
|
||||
generateKeyPairSync('rsa', {
|
||||
modulusLength: 1024,
|
||||
publicExponent: 0x10001
|
||||
});
|
||||
}
|
||||
bench.end(n);
|
||||
},
|
||||
|
||||
rsaAsync(n) {
|
||||
let remaining = n;
|
||||
function done(err) {
|
||||
assert.ifError(err);
|
||||
if (--remaining === 0)
|
||||
bench.end(n);
|
||||
}
|
||||
bench.start();
|
||||
for (let i = 0; i < n; ++i)
|
||||
generateKeyPair('rsa', {
|
||||
modulusLength: 512,
|
||||
publicExponent: 0x10001
|
||||
}, done);
|
||||
},
|
||||
|
||||
dsaSync(n) {
|
||||
bench.start();
|
||||
for (let i = 0; i < n; ++i) {
|
||||
generateKeyPairSync('dsa', {
|
||||
modulusLength: 512,
|
||||
divisorLength: 256,
|
||||
});
|
||||
}
|
||||
bench.end(n);
|
||||
},
|
||||
|
||||
dsaAsync(n) {
|
||||
let remaining = n;
|
||||
function done(err) {
|
||||
assert.ifError(err);
|
||||
if (--remaining === 0)
|
||||
bench.end(n);
|
||||
}
|
||||
bench.start();
|
||||
for (let i = 0; i < n; ++i)
|
||||
generateKeyPair('dsa', {
|
||||
modulusLength: 512,
|
||||
divisorLength: 256,
|
||||
}, done);
|
||||
},
|
||||
};
|
||||
|
||||
function main({ n, method }) {
|
||||
methods[method](n);
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
'use strict';
|
||||
|
||||
const common = require('../common.js');
|
||||
const {
|
||||
createHash,
|
||||
webcrypto: {
|
||||
subtle,
|
||||
getRandomValues
|
||||
}
|
||||
} = require('crypto');
|
||||
|
||||
const bench = common.createBenchmark(main, {
|
||||
sync: ['createHash', 'subtle'],
|
||||
data: [10, 20, 50, 100],
|
||||
method: ['SHA-1', 'SHA-256', 'SHA-384', 'SHA-512'],
|
||||
n: [1e3],
|
||||
});
|
||||
|
||||
const kMethods = {
|
||||
'SHA-1': 'sha1',
|
||||
'SHA-256': 'sha256',
|
||||
'SHA-384': 'sha384',
|
||||
'SHA-512': 'sha512'
|
||||
};
|
||||
|
||||
// This benchmark only looks at clock time and ignores factors
|
||||
// such as event loop delay, event loop utilization, and memory.
|
||||
// As such, it is expected that the synchronous legacy method
|
||||
// will always be faster in clock time.
|
||||
|
||||
function measureLegacy(n, data, method) {
|
||||
method = kMethods[method];
|
||||
bench.start();
|
||||
for (let i = 0; i < n; ++i) {
|
||||
createHash(method).update(data).digest();
|
||||
}
|
||||
bench.end(n);
|
||||
}
|
||||
|
||||
function measureSubtle(n, data, method) {
|
||||
const ec = new TextEncoder();
|
||||
data = ec.encode(data);
|
||||
const jobs = new Array(n);
|
||||
bench.start();
|
||||
for (let i = 0; i < n; i++)
|
||||
jobs[i] = subtle.digest(method, data);
|
||||
Promise.all(jobs).then(() => bench.end(n)).catch((err) => {
|
||||
process.nextTick(() => { throw err; });
|
||||
});
|
||||
}
|
||||
|
||||
function main({ n, sync, data, method }) {
|
||||
data = getRandomValues(Buffer.alloc(data));
|
||||
switch (sync) {
|
||||
case 'createHash': return measureLegacy(n, data, method);
|
||||
case 'subtle': return measureSubtle(n, data, method);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user