Skip to content

Commit e81bb9f

Browse files
danbevMylesBorins
authored andcommitted
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: #20247 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 391d2f8 commit e81bb9f

File tree

1 file changed

+23
-32
lines changed

1 file changed

+23
-32
lines changed

lib/internal/crypto/sig.js

+23-32
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,25 @@ Sign.prototype.update = function update(data, encoding) {
5353
return this;
5454
};
5555

56+
function getPadding(options) {
57+
return getIntOption('padding', RSA_PKCS1_PADDING, options);
58+
}
59+
60+
function getSaltLength(options) {
61+
return getIntOption('saltLength', RSA_PSS_SALTLEN_AUTO, options);
62+
}
63+
64+
function getIntOption(name, defaultValue, options) {
65+
if (options.hasOwnProperty(name)) {
66+
if (options[name] === options[name] >> 0) {
67+
return options[name];
68+
} else {
69+
throw new ERR_INVALID_OPT_VALUE(name, options[name]);
70+
}
71+
}
72+
return defaultValue;
73+
}
74+
5675
Sign.prototype.sign = function sign(options, encoding) {
5776
if (!options)
5877
throw new ERR_CRYPTO_SIGN_KEY_REQUIRED();
@@ -61,23 +80,9 @@ Sign.prototype.sign = function sign(options, encoding) {
6180
var passphrase = options.passphrase || null;
6281

6382
// Options specific to RSA
64-
var rsaPadding = RSA_PKCS1_PADDING;
65-
if (options.hasOwnProperty('padding')) {
66-
if (options.padding === options.padding >> 0) {
67-
rsaPadding = options.padding;
68-
} else {
69-
throw new ERR_INVALID_OPT_VALUE('padding', options.padding);
70-
}
71-
}
83+
var rsaPadding = getPadding(options);
7284

73-
var pssSaltLength = RSA_PSS_SALTLEN_AUTO;
74-
if (options.hasOwnProperty('saltLength')) {
75-
if (options.saltLength === options.saltLength >> 0) {
76-
pssSaltLength = options.saltLength;
77-
} else {
78-
throw new ERR_INVALID_OPT_VALUE('saltLength', options.saltLength);
79-
}
80-
}
85+
var pssSaltLength = getSaltLength(options);
8186

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

121126
// Options specific to RSA
122-
var rsaPadding = RSA_PKCS1_PADDING;
123-
if (options.hasOwnProperty('padding')) {
124-
if (options.padding === options.padding >> 0) {
125-
rsaPadding = options.padding;
126-
} else {
127-
throw new ERR_INVALID_OPT_VALUE('padding', options.padding);
128-
}
129-
}
127+
var rsaPadding = getPadding(options);
130128

131-
var pssSaltLength = RSA_PSS_SALTLEN_AUTO;
132-
if (options.hasOwnProperty('saltLength')) {
133-
if (options.saltLength === options.saltLength >> 0) {
134-
pssSaltLength = options.saltLength;
135-
} else {
136-
throw new ERR_INVALID_OPT_VALUE('saltLength', options.saltLength);
137-
}
138-
}
129+
var pssSaltLength = getSaltLength(options);
139130

140131
key = toBuf(key);
141132
if (!isArrayBufferView(key)) {

0 commit comments

Comments
 (0)