Skip to content

Commit

Permalink
crypto: support Big(U)Int64Array in getRandomValues
Browse files Browse the repository at this point in the history
  • Loading branch information
targos committed Jul 19, 2021
1 parent e579acb commit a3eba4a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
1 change: 0 additions & 1 deletion lib/internal/crypto/random.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,6 @@ function onJobDone(buf, callback, error) {
// be an integer-type TypedArray.
function getRandomValues(data) {
if (!isArrayBufferView(data) ||
isBigInt64Array(data) ||
isFloat32Array(data) ||
isFloat64Array(data)) {
// Ordinarily this would be an ERR_INVALID_ARG_TYPE. However,
Expand Down
46 changes: 24 additions & 22 deletions test/parallel/test-webcrypto-random.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,43 @@ const { Buffer } = require('buffer');
const assert = require('assert');
const { getRandomValues } = require('crypto').webcrypto;

[undefined, null, '', 1, {}, []].forEach((i) => {
assert.throws(() => getRandomValues(i), { code: 17 });
[
undefined, null, '', 1, {}, [],
new Float32Array(1),
new Float64Array(1),
].forEach((i) => {
assert.throws(
() => getRandomValues(i),
{ name: 'TypeMismatchError', code: 17 },
);
});

{
const buf = new Uint8Array(0);
getRandomValues(buf);
}

{
const buf = new Uint8Array(new Array(10).fill(0));
const before = Buffer.from(buf).toString('hex');
getRandomValues(buf);
const after = Buffer.from(buf).toString('hex');
assert.notStrictEqual(before, after);
}

{
const buf = new Uint16Array(new Array(10).fill(0));
const before = Buffer.from(buf).toString('hex');
getRandomValues(buf);
const after = Buffer.from(buf).toString('hex');
assert.notStrictEqual(before, after);
}
const intTypedConstructors = [
Int8Array,
Int16Array,
Int32Array,
Uint8Array,
Uint16Array,
Uint32Array,
BigInt64Array,
BigUint64Array,
];

{
const buf = new Uint32Array(new Array(10).fill(0));
const before = Buffer.from(buf).toString('hex');
for (const ctor of intTypedConstructors) {
const buf = new ctor(10);
const before = Buffer.from(buf.buffer).toString('hex');
getRandomValues(buf);
const after = Buffer.from(buf).toString('hex');
const after = Buffer.from(buf.buffer).toString('hex');
assert.notStrictEqual(before, after);
}

{
const buf = new Uint16Array(new Array(10).fill(0));
const buf = new Uint16Array(10);
const before = Buffer.from(buf).toString('hex');
getRandomValues(new DataView(buf.buffer));
const after = Buffer.from(buf).toString('hex');
Expand Down

0 comments on commit a3eba4a

Please sign in to comment.