randomFill - Node documentation
function randomFill

Usage in Deno

import { randomFill } from "node:crypto";
randomFill<T extends ArrayBufferView>(
buffer: T,
callback: (
err: Error | null,
buf: T,
) => void
,
): void

This function is similar to randomBytes but requires the first argument to be a Buffer that will be filled. It also requires that a callback is passed in.

If the callback function is not provided, an error will be thrown.

import { Buffer } from 'node:buffer';
const { randomFill } = await import('node:crypto');

const buf = Buffer.alloc(10);
randomFill(buf, (err, buf) => {
  if (err) throw err;
  console.log(buf.toString('hex'));
});

randomFill(buf, 5, (err, buf) => {
  if (err) throw err;
  console.log(buf.toString('hex'));
});

// The above is equivalent to the following:
randomFill(buf, 5, 5, (err, buf) => {
  if (err) throw err;
  console.log(buf.toString('hex'));
});

Any ArrayBuffer, TypedArray, or DataView instance may be passed asbuffer.

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.

import { Buffer } from 'node:buffer';
const { randomFill } = await import('node:crypto');

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

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 ArrayBuffer(10);
randomFill(c, (err, buf) => {
  if (err) throw err;
  console.log(Buffer.from(buf).toString('hex'));
});

This API uses libuv's threadpool, which can have surprising and negative performance implications for some applications; see the UV_THREADPOOL_SIZE documentation for more information.

The asynchronous version of crypto.randomFill() is carried out in a single threadpool request. To minimize threadpool task length variation, partition large randomFill requests when doing so as part of fulfilling a client request.

Type Parameters

T extends ArrayBufferView

Parameters

buffer: T

Must be supplied. The size of the provided buffer must not be larger than 2**31 - 1.

callback: (
err: Error | null,
buf: T,
) => void

function(err, buf) {}.

Return Type

void
randomFill<T extends ArrayBufferView>(
buffer: T,
offset: number,
callback: (
err: Error | null,
buf: T,
) => void
,
): void

Type Parameters

T extends ArrayBufferView

Parameters

buffer: T
offset: number
callback: (
err: Error | null,
buf: T,
) => void

Return Type

void
randomFill<T extends ArrayBufferView>(
buffer: T,
offset: number,
size: number,
callback: (
err: Error | null,
buf: T,
) => void
,
): void

Type Parameters

T extends ArrayBufferView

Parameters

buffer: T
offset: number
size: number
callback: (
err: Error | null,
buf: T,
) => void

Return Type

void