node/lib/internal/crypto/random.js
Ruben Bridgewater c6b6c92185
lib: always show ERR_INVALID_ARG_TYPE received part
This makes a effort to make sure all of these errors will actually
also show the received input.
On top of that it refactors a few tests for better maintainability.
It will also change the returned type to always be a simple typeof
instead of special handling null.

PR-URL: https://github.com/nodejs/node/pull/19445
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
2018-03-25 01:45:37 +01:00

108 lines
2.5 KiB
JavaScript

'use strict';
const {
ERR_INVALID_ARG_TYPE,
ERR_INVALID_CALLBACK,
ERR_OUT_OF_RANGE
} = require('internal/errors').codes;
const { isArrayBufferView } = require('internal/util/types');
const {
randomBytes: _randomBytes,
randomFill: _randomFill
} = process.binding('crypto');
const { kMaxLength } = require('buffer');
const kMaxUint32 = Math.pow(2, 32) - 1;
function assertOffset(offset, length) {
if (typeof offset !== 'number' || Number.isNaN(offset)) {
throw new ERR_INVALID_ARG_TYPE('offset', 'number');
}
if (offset > kMaxUint32 || offset < 0) {
throw new ERR_INVALID_ARG_TYPE('offset', 'uint32');
}
if (offset > kMaxLength || offset > length) {
throw new ERR_OUT_OF_RANGE('offset');
}
}
function assertSize(size, offset = 0, length = Infinity) {
if (typeof size !== 'number' || Number.isNaN(size)) {
throw new ERR_INVALID_ARG_TYPE('size', 'number');
}
if (size > kMaxUint32 || size < 0) {
throw new ERR_INVALID_ARG_TYPE('size', 'uint32');
}
if (size + offset > length || size > kMaxLength) {
throw new ERR_OUT_OF_RANGE('size');
}
}
function randomBytes(size, cb) {
assertSize(size);
if (cb !== undefined && typeof cb !== 'function')
throw new ERR_INVALID_CALLBACK();
return _randomBytes(size, cb);
}
function randomFillSync(buf, offset = 0, size) {
if (!isArrayBufferView(buf)) {
throw new ERR_INVALID_ARG_TYPE('buf', 'ArrayBufferView', buf);
}
const elementSize = buf.BYTES_PER_ELEMENT || 1;
offset *= elementSize;
assertOffset(offset, buf.byteLength);
if (size === undefined) {
size = buf.byteLength - offset;
} else {
size *= elementSize;
}
assertSize(size, offset, buf.byteLength);
return _randomFill(buf, offset, size);
}
function randomFill(buf, offset, size, cb) {
if (!isArrayBufferView(buf)) {
throw new ERR_INVALID_ARG_TYPE('buf', 'ArrayBufferView', buf);
}
const elementSize = buf.BYTES_PER_ELEMENT || 1;
if (typeof offset === 'function') {
cb = offset;
offset = 0;
size = buf.bytesLength;
} else if (typeof size === 'function') {
cb = size;
offset *= elementSize;
size = buf.byteLength - offset;
} else if (typeof cb !== 'function') {
throw new ERR_INVALID_CALLBACK();
}
if (size === undefined) {
size = buf.byteLength - offset;
} else {
size *= elementSize;
}
assertOffset(offset, buf.byteLength);
assertSize(size, offset, buf.byteLength);
return _randomFill(buf, offset, size, cb);
}
module.exports = {
randomBytes,
randomFill,
randomFillSync
};