Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 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
7 changes: 5 additions & 2 deletions doc/api/webcrypto.md
Original file line number Diff line number Diff line change
Expand Up @@ -354,12 +354,15 @@ Provides access to the `SubtleCrypto` API.
added: v15.0.0
-->

* `typedArray` {Buffer|TypedArray|DataView|ArrayBuffer}
* Returns: {Buffer|TypedArray|DataView|ArrayBuffer} Returns `typedArray`.
* `typedArray` {Buffer|TypedArray}
* Returns: {Buffer|TypedArray}

Generates cryptographically strong random values. The given `typedArray` is
filled with random values, and a reference to `typedArray` is returned.

The given `typedArray` must be an integer-based instance of {TypedArray},
i.e. `Float32Array` and `Float64Array` are not accepted.

An error will be thrown if the given `typedArray` is larger than 65,536 bytes.

### `crypto.randomUUID()`
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/crypto/random.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const {
const {
isArrayBufferView,
isAnyArrayBuffer,
isTypedArray,
isFloat32Array,
isFloat64Array,
} = require('internal/util/types');
Expand Down Expand Up @@ -307,7 +308,7 @@ function onJobDone(buf, callback, error) {
// not allowed to exceed 65536 bytes, and can only
// be an integer-type TypedArray.
function getRandomValues(data) {
if (!isArrayBufferView(data) ||
if (!isTypedArray(data) ||
isFloat32Array(data) ||
isFloat64Array(data)) {
// Ordinarily this would be an ERR_INVALID_ARG_TYPE. However,
Expand Down
4 changes: 3 additions & 1 deletion test/parallel/test-webcrypto-random.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const { getRandomValues } = require('crypto').webcrypto;
undefined, null, '', 1, {}, [],
new Float32Array(1),
new Float64Array(1),
new DataView(new ArrayBuffer(1)),
].forEach((i) => {
assert.throws(
() => getRandomValues(i),
Expand All @@ -32,6 +33,7 @@ const intTypedConstructors = [
Uint8Array,
Uint16Array,
Uint32Array,
Uint8ClampedArray,
BigInt64Array,
BigUint64Array,
];
Expand All @@ -47,7 +49,7 @@ for (const ctor of intTypedConstructors) {
{
const buf = new Uint16Array(10);
const before = Buffer.from(buf).toString('hex');
getRandomValues(new DataView(buf.buffer));
getRandomValues(buf);
const after = Buffer.from(buf).toString('hex');
assert.notStrictEqual(before, after);
}
Expand Down
5 changes: 4 additions & 1 deletion tools/doc/type-parser.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ const jsGlobalTypes = [
'AggregateError', 'Array', 'ArrayBuffer', 'DataView', 'Date', 'Error',
'EvalError', 'Function', 'Map', 'Object', 'Promise', 'RangeError',
'ReferenceError', 'RegExp', 'Set', 'SharedArrayBuffer', 'SyntaxError',
'TypeError', 'TypedArray', 'URIError', 'Uint8Array',
'TypeError', 'TypedArray', 'URIError', 'Int8Array', 'Uint8Array',
'Uint8ClampedArray', 'Int16Array', 'Uint16Array', 'Int32Array',
'Uint32Array', 'Float32Array', 'Float64Array', 'BigInt64Array',
'BigUint64Array',
];

const customTypesMap = {
Expand Down