Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: advise against using randomFill on floats #38150

Closed
Closed
Changes from all 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
48 changes: 16 additions & 32 deletions doc/api/crypto.md
Original file line number Diff line number Diff line change
Expand Up @@ -4454,16 +4454,12 @@ const a = new Uint32Array(10);
console.log(Buffer.from(randomFillSync(a).buffer,
a.byteOffset, a.byteLength).toString('hex'));

const b = new Float64Array(10);
const b = new DataView(new ArrayBuffer(10));
console.log(Buffer.from(randomFillSync(b).buffer,
b.byteOffset, b.byteLength).toString('hex'));

const c = new DataView(new ArrayBuffer(10));
console.log(Buffer.from(randomFillSync(c).buffer,
c.byteOffset, c.byteLength).toString('hex'));

const d = new ArrayBuffer(10);
console.log(Buffer.from(randomFillSync(d)).toString('hex'));
const c = new ArrayBuffer(10);
console.log(Buffer.from(randomFillSync(c)).toString('hex'));
```

```cjs
Expand All @@ -4475,16 +4471,12 @@ const a = new Uint32Array(10);
console.log(Buffer.from(randomFillSync(a).buffer,
a.byteOffset, a.byteLength).toString('hex'));

const b = new Float64Array(10);
const b = new DataView(new ArrayBuffer(10));
console.log(Buffer.from(randomFillSync(b).buffer,
b.byteOffset, b.byteLength).toString('hex'));

const c = new DataView(new ArrayBuffer(10));
console.log(Buffer.from(randomFillSync(c).buffer,
c.byteOffset, c.byteLength).toString('hex'));

const d = new ArrayBuffer(10);
console.log(Buffer.from(randomFillSync(d)).toString('hex'));
const c = new ArrayBuffer(10);
console.log(Buffer.from(randomFillSync(c)).toString('hex'));
```

### `crypto.randomFill(buffer[, offset][, size], callback)`
Expand Down Expand Up @@ -4560,6 +4552,12 @@ randomFill(buf, 5, 5, (err, buf) => {
Any `ArrayBuffer`, `TypedArray`, or `DataView` instance may be passed as
`buffer`.

While this includes instances of `Float32Array` and `Float64Array`, this
function should not be used to generate random floating-point numbers. The
result may contain `+Infinity`, `-Infinity`, and `NaN`, and even if the array
contains finite numbers only, they are not drawn from a uniform random
distribution and have no meaningful lower or upper bounds.

```mjs
const {
randomFill,
Expand All @@ -4572,22 +4570,15 @@ randomFill(a, (err, buf) => {
.toString('hex'));
});

const b = new Float64Array(10);
const b = new DataView(new ArrayBuffer(10));
randomFill(b, (err, buf) => {
if (err) throw err;
console.log(Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength)
.toString('hex'));
});

const c = new DataView(new ArrayBuffer(10));
const c = new ArrayBuffer(10);
randomFill(c, (err, buf) => {
if (err) throw err;
console.log(Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength)
.toString('hex'));
});

const d = new ArrayBuffer(10);
randomFill(d, (err, buf) => {
if (err) throw err;
console.log(Buffer.from(buf).toString('hex'));
});
Expand All @@ -4605,22 +4596,15 @@ randomFill(a, (err, buf) => {
.toString('hex'));
});

const b = new Float64Array(10);
const b = new DataView(new ArrayBuffer(10));
randomFill(b, (err, buf) => {
if (err) throw err;
console.log(Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength)
.toString('hex'));
});

const c = new DataView(new ArrayBuffer(10));
const c = new ArrayBuffer(10);
randomFill(c, (err, buf) => {
if (err) throw err;
console.log(Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength)
.toString('hex'));
});

const d = new ArrayBuffer(10);
randomFill(d, (err, buf) => {
if (err) throw err;
console.log(Buffer.from(buf).toString('hex'));
});
Expand Down