Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crypto: add getIntOption function to reduce dupl #20247

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 23 additions & 32 deletions lib/internal/crypto/sig.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,25 @@ Sign.prototype.update = function update(data, encoding) {
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) {
if (!options)
throw new ERR_CRYPTO_SIGN_KEY_REQUIRED();
Expand All @@ -61,23 +80,9 @@ Sign.prototype.sign = function sign(options, encoding) {
var passphrase = options.passphrase || null;

// Options specific to RSA
var rsaPadding = RSA_PKCS1_PADDING;
if (options.hasOwnProperty('padding')) {
if (options.padding === options.padding >> 0) {
rsaPadding = options.padding;
} else {
throw new ERR_INVALID_OPT_VALUE('padding', options.padding);
}
}
var rsaPadding = getPadding(options);

var pssSaltLength = RSA_PSS_SALTLEN_AUTO;
if (options.hasOwnProperty('saltLength')) {
if (options.saltLength === options.saltLength >> 0) {
pssSaltLength = options.saltLength;
} else {
throw new ERR_INVALID_OPT_VALUE('saltLength', options.saltLength);
}
}
var pssSaltLength = getSaltLength(options);

key = toBuf(key);
if (!isArrayBufferView(key)) {
Expand Down Expand Up @@ -119,23 +124,9 @@ Verify.prototype.verify = function verify(options, signature, sigEncoding) {
sigEncoding = sigEncoding || getDefaultEncoding();

// Options specific to RSA
var rsaPadding = RSA_PKCS1_PADDING;
if (options.hasOwnProperty('padding')) {
if (options.padding === options.padding >> 0) {
rsaPadding = options.padding;
} else {
throw new ERR_INVALID_OPT_VALUE('padding', options.padding);
}
}
var rsaPadding = getPadding(options);

var pssSaltLength = RSA_PSS_SALTLEN_AUTO;
if (options.hasOwnProperty('saltLength')) {
if (options.saltLength === options.saltLength >> 0) {
pssSaltLength = options.saltLength;
} else {
throw new ERR_INVALID_OPT_VALUE('saltLength', options.saltLength);
}
}
var pssSaltLength = getSaltLength(options);

key = toBuf(key);
if (!isArrayBufferView(key)) {
Expand Down