Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e81bb9f

Browse filesBrowse 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 <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>
1 parent 391d2f8 commit e81bb9f
Copy full SHA for e81bb9f

File tree

Expand file treeCollapse file tree

1 file changed

+23
-32
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+23
-32
lines changed
Open diff view settings
Collapse file

‎lib/internal/crypto/sig.js‎

Copy file name to clipboardExpand all lines: lib/internal/crypto/sig.js
+23-32Lines changed: 23 additions & 32 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.