Skip to content

Commit 65a0265

Browse files
authored
test,doc: cover and document multi-byte offset/size in randomFill
Signed-off-by: kyungrae <kyungrae2002@gmail.com> PR-URL: #64834 Reviewed-By: Daeyeon Jeong <daeyeon.dev@gmail.com>
1 parent 84b76c8 commit 65a0265

3 files changed

Lines changed: 62 additions & 7 deletions

File tree

doc/api/crypto.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5660,9 +5660,12 @@ changes:
56605660

56615661
* `buffer` {ArrayBuffer|Buffer|TypedArray|DataView} Must be supplied. The
56625662
size of the provided `buffer` must not be larger than `2**31 - 1`.
5663-
* `offset` {number} **Default:** `0`
5664-
* `size` {number} **Default:** `buffer.length - offset`. The `size` must
5665-
not be larger than `2**31 - 1`.
5663+
* `offset` {number} The start position, in elements for a `TypedArray` and in
5664+
bytes for an `ArrayBuffer` or `DataView`. **Default:** `0`
5665+
* `size` {number} The amount to fill, in the same units as `offset`.
5666+
**Default:** `buffer.length - offset` for a `TypedArray`, or
5667+
`buffer.byteLength - offset` for an `ArrayBuffer` or `DataView`. The `size`
5668+
must not be larger than `2**31 - 1`.
56665669
* `callback` {Function} `function(err, buf) {}`.
56675670

56685671
This function is similar to [`crypto.randomBytes()`][] but requires the first
@@ -5797,9 +5800,12 @@ changes:
57975800

57985801
* `buffer` {ArrayBuffer|Buffer|TypedArray|DataView} Must be supplied. The
57995802
size of the provided `buffer` must not be larger than `2**31 - 1`.
5800-
* `offset` {number} **Default:** `0`
5801-
* `size` {number} **Default:** `buffer.length - offset`. The `size` must
5802-
not be larger than `2**31 - 1`.
5803+
* `offset` {number} The start position, in elements for a `TypedArray` and in
5804+
bytes for an `ArrayBuffer` or `DataView`. **Default:** `0`
5805+
* `size` {number} The amount to fill, in the same units as `offset`.
5806+
**Default:** `buffer.length - offset` for a `TypedArray`, or
5807+
`buffer.byteLength - offset` for an `ArrayBuffer` or `DataView`. The `size`
5808+
must not be larger than `2**31 - 1`.
58035809
* Returns: {ArrayBuffer|Buffer|TypedArray|DataView} The object passed as
58045810
`buffer` argument.
58055811

lib/internal/crypto/random.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ function randomFill(buf, offset, size, callback) {
168168
size = buf.length;
169169
} else if (typeof size === 'function') {
170170
callback = size;
171-
size = buf.length - offset;
171+
size = (buf.length ?? buf.byteLength) - offset;
172172
} else {
173173
validateFunction(callback, 'callback');
174174
}

test/parallel/test-crypto-random.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,55 @@ common.expectWarning('DeprecationWarning',
218218
}));
219219
}
220220

221+
{
222+
const buf = new Uint16Array(10);
223+
const before = Buffer.from(buf.buffer).toString('hex');
224+
crypto.randomFillSync(buf, 1, 8);
225+
const after = Buffer.from(buf.buffer).toString('hex');
226+
assert.notStrictEqual(before, after);
227+
assert.deepStrictEqual(before.slice(0, 4), after.slice(0, 4));
228+
assert.deepStrictEqual(before.slice(-4), after.slice(-4));
229+
}
230+
231+
{
232+
const buf = new Uint32Array(10);
233+
const before = Buffer.from(buf.buffer).toString('hex');
234+
crypto.randomFillSync(buf, 1, 8);
235+
const after = Buffer.from(buf.buffer).toString('hex');
236+
assert.notStrictEqual(before, after);
237+
assert.deepStrictEqual(before.slice(0, 8), after.slice(0, 8));
238+
assert.deepStrictEqual(before.slice(-8), after.slice(-8));
239+
}
240+
241+
{
242+
const buf = new Uint16Array(10);
243+
const before = Buffer.from(buf.buffer).toString('hex');
244+
crypto.randomFill(buf, 1, 8, common.mustSucceed((buf) => {
245+
const after = Buffer.from(buf.buffer).toString('hex');
246+
assert.notStrictEqual(before, after);
247+
assert.deepStrictEqual(before.slice(0, 4), after.slice(0, 4));
248+
assert.deepStrictEqual(before.slice(-4), after.slice(-4));
249+
}));
250+
}
251+
252+
{
253+
const buf = new Uint32Array(10);
254+
const before = Buffer.from(buf.buffer).toString('hex');
255+
crypto.randomFill(buf, 1, 8, common.mustSucceed((buf) => {
256+
const after = Buffer.from(buf.buffer).toString('hex');
257+
assert.notStrictEqual(before, after);
258+
assert.deepStrictEqual(before.slice(0, 8), after.slice(0, 8));
259+
assert.deepStrictEqual(before.slice(-8), after.slice(-8));
260+
}));
261+
}
262+
263+
{
264+
// randomFill() with an offset and no size must not throw for types
265+
// without a .length property, matching randomFillSync().
266+
crypto.randomFill(new ArrayBuffer(10), 2, common.mustSucceed());
267+
crypto.randomFill(new DataView(new ArrayBuffer(10)), 2, common.mustSucceed());
268+
}
269+
221270
{
222271
[
223272
Buffer.alloc(10),

0 commit comments

Comments
 (0)