crypto: accept decimal Number in randomBytes
This change adds the ability to pass a double into randomBytes. PR-URL: https://github.com/nodejs/node/pull/15130 Fixes: https://github.com/nodejs/node/issues/15118 Reviewed-By: Sam Roberts <vieuxtech@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Evan Lucas <evanlucas@me.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
Родитель
8d5b0130da
Коммит
484bfa2e37
|
@ -5616,13 +5616,13 @@ void RandomBytesProcessSync(Environment* env,
|
|||
void RandomBytes(const FunctionCallbackInfo<Value>& args) {
|
||||
Environment* env = Environment::GetCurrent(args);
|
||||
|
||||
if (!args[0]->IsUint32()) {
|
||||
if (!args[0]->IsNumber() || args[0].As<v8::Number>()->Value() < 0) {
|
||||
return env->ThrowTypeError("size must be a number >= 0");
|
||||
}
|
||||
|
||||
const int64_t size = args[0]->IntegerValue();
|
||||
if (size < 0 || size > Buffer::kMaxLength)
|
||||
return env->ThrowRangeError("size is not a valid Smi");
|
||||
if (size > Buffer::kMaxLength)
|
||||
return env->ThrowTypeError("size must be a uint32");
|
||||
|
||||
Local<Object> obj = env->randombytes_constructor_template()->
|
||||
NewInstance(env->context()).ToLocalChecked();
|
||||
|
|
|
@ -33,6 +33,15 @@ crypto.DEFAULT_ENCODING = 'buffer';
|
|||
// bump, we register a lot of exit listeners
|
||||
process.setMaxListeners(256);
|
||||
|
||||
const errMessages = {
|
||||
offsetNotNumber: /^TypeError: offset must be a number$/,
|
||||
offsetOutOfRange: /^RangeError: offset out of range$/,
|
||||
offsetNotUInt32: /^TypeError: offset must be a uint32$/,
|
||||
sizeNotNumber: /^TypeError: size must be a number$/,
|
||||
sizeNotUInt32: /^TypeError: size must be a uint32$/,
|
||||
bufferTooSmall: /^RangeError: buffer too small$/,
|
||||
};
|
||||
|
||||
const expectedErrorRegexp = /^TypeError: size must be a number >= 0$/;
|
||||
[crypto.randomBytes, crypto.pseudoRandomBytes].forEach(function(f) {
|
||||
[-1, undefined, null, false, true, {}, []].forEach(function(value) {
|
||||
|
@ -41,10 +50,10 @@ const expectedErrorRegexp = /^TypeError: size must be a number >= 0$/;
|
|||
expectedErrorRegexp);
|
||||
});
|
||||
|
||||
[0, 1, 2, 4, 16, 256, 1024].forEach(function(len) {
|
||||
[0, 1, 2, 4, 16, 256, 1024, 101.2].forEach(function(len) {
|
||||
f(len, common.mustCall(function(ex, buf) {
|
||||
assert.strictEqual(ex, null);
|
||||
assert.strictEqual(buf.length, len);
|
||||
assert.strictEqual(buf.length, Math.floor(len));
|
||||
assert.ok(Buffer.isBuffer(buf));
|
||||
}));
|
||||
});
|
||||
|
@ -139,14 +148,6 @@ const expectedErrorRegexp = /^TypeError: size must be a number >= 0$/;
|
|||
Buffer.alloc(10),
|
||||
new Uint8Array(new Array(10).fill(0))
|
||||
];
|
||||
const errMessages = {
|
||||
offsetNotNumber: /^TypeError: offset must be a number$/,
|
||||
offsetOutOfRange: /^RangeError: offset out of range$/,
|
||||
offsetNotUInt32: /^TypeError: offset must be a uint32$/,
|
||||
sizeNotNumber: /^TypeError: size must be a number$/,
|
||||
sizeNotUInt32: /^TypeError: size must be a uint32$/,
|
||||
bufferTooSmall: /^RangeError: buffer too small$/,
|
||||
};
|
||||
|
||||
const max = require('buffer').kMaxLength + 1;
|
||||
|
||||
|
@ -264,4 +265,4 @@ const expectedErrorRegexp = /^TypeError: size must be a number >= 0$/;
|
|||
// length exceeds max acceptable value"
|
||||
assert.throws(function() {
|
||||
crypto.randomBytes((-1 >>> 0) + 1);
|
||||
}, /^TypeError: size must be a number >= 0$/);
|
||||
}, errMessages.sizeNotUInt32);
|
||||
|
|
Загрузка…
Ссылка в новой задаче