crypto: add getIntOption function to reduce dupl
This commit adds a getIntOption function to reduce the code duplicated for getting the padding, and saltLength options. PR-URL: https://github.com/nodejs/node/pull/20247 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
parent
3b8ab2ac7f
commit
d4726d2f3f
@ -53,6 +53,25 @@ Sign.prototype.update = function update(data, encoding) {
|
|||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function getPadding(options) {
|
||||||
|
return getIntOption('padding', RSA_PKCS1_PADDING, options);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getSaltLength(options) {
|
||||||
|
return getIntOption('saltLength', RSA_PSS_SALTLEN_AUTO, options);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getIntOption(name, defaultValue, options) {
|
||||||
|
if (options.hasOwnProperty(name)) {
|
||||||
|
if (options[name] === options[name] >> 0) {
|
||||||
|
return options[name];
|
||||||
|
} else {
|
||||||
|
throw new ERR_INVALID_OPT_VALUE(name, options[name]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
Sign.prototype.sign = function sign(options, encoding) {
|
Sign.prototype.sign = function sign(options, encoding) {
|
||||||
if (!options)
|
if (!options)
|
||||||
throw new ERR_CRYPTO_SIGN_KEY_REQUIRED();
|
throw new ERR_CRYPTO_SIGN_KEY_REQUIRED();
|
||||||
@ -61,23 +80,9 @@ Sign.prototype.sign = function sign(options, encoding) {
|
|||||||
var passphrase = options.passphrase || null;
|
var passphrase = options.passphrase || null;
|
||||||
|
|
||||||
// Options specific to RSA
|
// Options specific to RSA
|
||||||
var rsaPadding = RSA_PKCS1_PADDING;
|
var rsaPadding = getPadding(options);
|
||||||
if (options.hasOwnProperty('padding')) {
|
|
||||||
if (options.padding === options.padding >> 0) {
|
|
||||||
rsaPadding = options.padding;
|
|
||||||
} else {
|
|
||||||
throw new ERR_INVALID_OPT_VALUE('padding', options.padding);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var pssSaltLength = RSA_PSS_SALTLEN_AUTO;
|
var pssSaltLength = getSaltLength(options);
|
||||||
if (options.hasOwnProperty('saltLength')) {
|
|
||||||
if (options.saltLength === options.saltLength >> 0) {
|
|
||||||
pssSaltLength = options.saltLength;
|
|
||||||
} else {
|
|
||||||
throw new ERR_INVALID_OPT_VALUE('saltLength', options.saltLength);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
key = toBuf(key);
|
key = toBuf(key);
|
||||||
if (!isArrayBufferView(key)) {
|
if (!isArrayBufferView(key)) {
|
||||||
@ -119,23 +124,9 @@ Verify.prototype.verify = function verify(options, signature, sigEncoding) {
|
|||||||
sigEncoding = sigEncoding || getDefaultEncoding();
|
sigEncoding = sigEncoding || getDefaultEncoding();
|
||||||
|
|
||||||
// Options specific to RSA
|
// Options specific to RSA
|
||||||
var rsaPadding = RSA_PKCS1_PADDING;
|
var rsaPadding = getPadding(options);
|
||||||
if (options.hasOwnProperty('padding')) {
|
|
||||||
if (options.padding === options.padding >> 0) {
|
|
||||||
rsaPadding = options.padding;
|
|
||||||
} else {
|
|
||||||
throw new ERR_INVALID_OPT_VALUE('padding', options.padding);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var pssSaltLength = RSA_PSS_SALTLEN_AUTO;
|
var pssSaltLength = getSaltLength(options);
|
||||||
if (options.hasOwnProperty('saltLength')) {
|
|
||||||
if (options.saltLength === options.saltLength >> 0) {
|
|
||||||
pssSaltLength = options.saltLength;
|
|
||||||
} else {
|
|
||||||
throw new ERR_INVALID_OPT_VALUE('saltLength', options.saltLength);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
key = toBuf(key);
|
key = toBuf(key);
|
||||||
if (!isArrayBufferView(key)) {
|
if (!isArrayBufferView(key)) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user