crypto.randomFillSync(buffer[, offset][, size])
buffer
<Buffer> | <TypedArray> | <DataView> 必须提供。offset
<number> 默认值:0
size
<number> 默认值:buffer.length - offset
- 返回: <Buffer> | <TypedArray> | <DataView> 对象作为
buffer
参数传入。
crypto.randomFill()
的同步版本。
const buf = Buffer.alloc(10);
console.log(crypto.randomFillSync(buf).toString('hex'));
crypto.randomFillSync(buf, 5);
console.log(buf.toString('hex'));
// 以上等价于以下内容:
crypto.randomFillSync(buf, 5, 5);
console.log(buf.toString('hex'));
任何 TypedArray
或 DataView
实例都可以作为 buffer
传入。
const a = new Uint32Array(10);
console.log(Buffer.from(crypto.randomFillSync(a).buffer,
a.byteOffset, a.byteLength).toString('hex'));
const b = new Float64Array(10);
console.log(Buffer.from(crypto.randomFillSync(b).buffer,
b.byteOffset, b.byteLength).toString('hex'));
const c = new DataView(new ArrayBuffer(10));
console.log(Buffer.from(crypto.randomFillSync(c).buffer,
c.byteOffset, c.byteLength).toString('hex'));
buffer
<Buffer> | <TypedArray> | <DataView> Must be supplied.offset
<number> Default:0
size
<number> Default:buffer.length - offset
- Returns: <Buffer> | <TypedArray> | <DataView> The object passed as
buffer
argument.
Synchronous version of crypto.randomFill()
.
const buf = Buffer.alloc(10);
console.log(crypto.randomFillSync(buf).toString('hex'));
crypto.randomFillSync(buf, 5);
console.log(buf.toString('hex'));
// The above is equivalent to the following:
crypto.randomFillSync(buf, 5, 5);
console.log(buf.toString('hex'));
Any TypedArray
or DataView
instance may be passed as buffer
.
const a = new Uint32Array(10);
console.log(Buffer.from(crypto.randomFillSync(a).buffer,
a.byteOffset, a.byteLength).toString('hex'));
const b = new Float64Array(10);
console.log(Buffer.from(crypto.randomFillSync(b).buffer,
b.byteOffset, b.byteLength).toString('hex'));
const c = new DataView(new ArrayBuffer(10));
console.log(Buffer.from(crypto.randomFillSync(c).buffer,
c.byteOffset, c.byteLength).toString('hex'));