Skip to content

Commit 8fc10c8

Browse files
committed
test,doc: cover and document multi-byte offset/size in randomFill
Signed-off-by: kyungrae <kyungrae2002@gmail.com>
1 parent ed63b19 commit 8fc10c8

2 files changed

Lines changed: 54 additions & 6 deletions

File tree

doc/api/crypto.md

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

55755575
* `buffer` {ArrayBuffer|Buffer|TypedArray|DataView} Must be supplied. The
55765576
size of the provided `buffer` must not be larger than `2**31 - 1`.
5577-
* `offset` {number} **Default:** `0`
5578-
* `size` {number} **Default:** `buffer.length - offset`. The `size` must
5579-
not be larger than `2**31 - 1`.
5577+
* `offset` {number} The start position, in elements for a `TypedArray` and in
5578+
bytes for an `ArrayBuffer` or `DataView`. **Default:** `0`
5579+
* `size` {number} The amount to fill, in the same units as `offset`.
5580+
**Default:** `buffer.length - offset` for a `TypedArray`, or
5581+
`buffer.byteLength - offset` for an `ArrayBuffer` or `DataView`. The `size`
5582+
must not be larger than `2**31 - 1`.
55805583
* `callback` {Function} `function(err, buf) {}`.
55815584

55825585
This function is similar to [`crypto.randomBytes()`][] but requires the first
@@ -5711,9 +5714,12 @@ changes:
57115714

57125715
* `buffer` {ArrayBuffer|Buffer|TypedArray|DataView} Must be supplied. The
57135716
size of the provided `buffer` must not be larger than `2**31 - 1`.
5714-
* `offset` {number} **Default:** `0`
5715-
* `size` {number} **Default:** `buffer.length - offset`. The `size` must
5716-
not be larger than `2**31 - 1`.
5717+
* `offset` {number} The start position, in elements for a `TypedArray` and in
5718+
bytes for an `ArrayBuffer` or `DataView`. **Default:** `0`
5719+
* `size` {number} The amount to fill, in the same units as `offset`.
5720+
**Default:** `buffer.length - offset` for a `TypedArray`, or
5721+
`buffer.byteLength - offset` for an `ArrayBuffer` or `DataView`. The `size`
5722+
must not be larger than `2**31 - 1`.
57175723
* Returns: {ArrayBuffer|Buffer|TypedArray|DataView} The object passed as
57185724
`buffer` argument.
57195725

test/parallel/test-crypto-random.js

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

221+
{
222+
const buf = new Uint16Array(4);
223+
const before = Buffer.from(buf.buffer).toString('hex');
224+
crypto.randomFillSync(buf, 1, 1);
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(8), after.slice(8));
229+
}
230+
231+
{
232+
const buf = new Uint32Array(4);
233+
const before = Buffer.from(buf.buffer).toString('hex');
234+
crypto.randomFillSync(buf, 1, 1);
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(16), after.slice(16));
239+
}
240+
241+
{
242+
const buf = new Uint16Array(4);
243+
const before = Buffer.from(buf.buffer).toString('hex');
244+
crypto.randomFill(buf, 1, 1, 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(8), after.slice(8));
249+
}));
250+
}
251+
252+
{
253+
const buf = new Uint32Array(4);
254+
const before = Buffer.from(buf.buffer).toString('hex');
255+
crypto.randomFill(buf, 1, 1, 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(16), after.slice(16));
260+
}));
261+
}
262+
221263
{
222264
[
223265
Buffer.alloc(10),

0 commit comments

Comments
 (0)