• buffer {Buffer|TypedArray|DataView} Must be supplied.
    • offset {number} Default: 0
    • size {number} Default: buffer.length - offset
    • callback {Function} function(err, buf) {}.

    This function is similar to [crypto.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.

    1. const buf = Buffer.alloc(10);
    2. crypto.randomFill(buf, (err, buf) => {
    3. if (err) throw err;
    4. console.log(buf.toString('hex'));
    5. });
    6. crypto.randomFill(buf, 5, (err, buf) => {
    7. if (err) throw err;
    8. console.log(buf.toString('hex'));
    9. });
    10. // The above is equivalent to the following:
    11. crypto.randomFill(buf, 5, 5, (err, buf) => {
    12. if (err) throw err;
    13. console.log(buf.toString('hex'));
    14. });

    Any TypedArray or DataView instance may be passed as buffer.

    1. const a = new Uint32Array(10);
    2. crypto.randomFill(a, (err, buf) => {
    3. if (err) throw err;
    4. console.log(Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength)
    5. .toString('hex'));
    6. });
    7. const b = new Float64Array(10);
    8. crypto.randomFill(b, (err, buf) => {
    9. if (err) throw err;
    10. console.log(Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength)
    11. .toString('hex'));
    12. });
    13. const c = new DataView(new ArrayBuffer(10));
    14. crypto.randomFill(c, (err, buf) => {
    15. if (err) throw err;
    16. console.log(Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength)
    17. .toString('hex'));
    18. });

    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.