Skip to content

Commit 56a7e0a

Browse files
targosnodejs-github-bot
authored andcommitted
crypto: support Big(U)Int64Array in getRandomValues
Refs: w3c/webcrypto#255 Fixes: #39442 PR-URL: #39443 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Zeyu Yang <himself65@outlook.com>
1 parent ab73d95 commit 56a7e0a

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

lib/internal/crypto/random.js

-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ const {
5050
const {
5151
isArrayBufferView,
5252
isAnyArrayBuffer,
53-
isBigInt64Array,
5453
isFloat32Array,
5554
isFloat64Array,
5655
} = require('internal/util/types');
@@ -309,7 +308,6 @@ function onJobDone(buf, callback, error) {
309308
// be an integer-type TypedArray.
310309
function getRandomValues(data) {
311310
if (!isArrayBufferView(data) ||
312-
isBigInt64Array(data) ||
313311
isFloat32Array(data) ||
314312
isFloat64Array(data)) {
315313
// Ordinarily this would be an ERR_INVALID_ARG_TYPE. However,

test/parallel/test-webcrypto-random.js

+24-22
Original file line numberDiff line numberDiff line change
@@ -9,41 +9,43 @@ const { Buffer } = require('buffer');
99
const assert = require('assert');
1010
const { getRandomValues } = require('crypto').webcrypto;
1111

12-
[undefined, null, '', 1, {}, []].forEach((i) => {
13-
assert.throws(() => getRandomValues(i), { code: 17 });
12+
[
13+
undefined, null, '', 1, {}, [],
14+
new Float32Array(1),
15+
new Float64Array(1),
16+
].forEach((i) => {
17+
assert.throws(
18+
() => getRandomValues(i),
19+
{ name: 'TypeMismatchError', code: 17 },
20+
);
1421
});
1522

1623
{
1724
const buf = new Uint8Array(0);
1825
getRandomValues(buf);
1926
}
2027

21-
{
22-
const buf = new Uint8Array(new Array(10).fill(0));
23-
const before = Buffer.from(buf).toString('hex');
24-
getRandomValues(buf);
25-
const after = Buffer.from(buf).toString('hex');
26-
assert.notStrictEqual(before, after);
27-
}
28-
29-
{
30-
const buf = new Uint16Array(new Array(10).fill(0));
31-
const before = Buffer.from(buf).toString('hex');
32-
getRandomValues(buf);
33-
const after = Buffer.from(buf).toString('hex');
34-
assert.notStrictEqual(before, after);
35-
}
28+
const intTypedConstructors = [
29+
Int8Array,
30+
Int16Array,
31+
Int32Array,
32+
Uint8Array,
33+
Uint16Array,
34+
Uint32Array,
35+
BigInt64Array,
36+
BigUint64Array,
37+
];
3638

37-
{
38-
const buf = new Uint32Array(new Array(10).fill(0));
39-
const before = Buffer.from(buf).toString('hex');
39+
for (const ctor of intTypedConstructors) {
40+
const buf = new ctor(10);
41+
const before = Buffer.from(buf.buffer).toString('hex');
4042
getRandomValues(buf);
41-
const after = Buffer.from(buf).toString('hex');
43+
const after = Buffer.from(buf.buffer).toString('hex');
4244
assert.notStrictEqual(before, after);
4345
}
4446

4547
{
46-
const buf = new Uint16Array(new Array(10).fill(0));
48+
const buf = new Uint16Array(10);
4749
const before = Buffer.from(buf).toString('hex');
4850
getRandomValues(new DataView(buf.buffer));
4951
const after = Buffer.from(buf).toString('hex');

0 commit comments

Comments
 (0)