@@ -41,13 +41,14 @@ const {
41
41
const { customPromisifyArgs } = require ( 'internal/util' ) ;
42
42
43
43
const {
44
- isInt32,
45
- isUint32,
46
44
validateFunction,
45
+ validateBuffer,
47
46
validateString,
48
47
validateInteger,
49
48
validateObject,
50
49
validateOneOf,
50
+ validateInt32,
51
+ validateUint32,
51
52
} = require ( 'internal/validators' ) ;
52
53
53
54
const {
@@ -174,16 +175,13 @@ function createJob(mode, type, options) {
174
175
{
175
176
validateObject ( options , 'options' ) ;
176
177
const { modulusLength } = options ;
177
- if ( ! isUint32 ( modulusLength ) )
178
- throw new ERR_INVALID_ARG_VALUE ( 'options.modulusLength' , modulusLength ) ;
178
+ validateUint32 ( modulusLength , 'options.modulusLength' ) ;
179
179
180
180
let { publicExponent } = options ;
181
181
if ( publicExponent == null ) {
182
182
publicExponent = 0x10001 ;
183
- } else if ( ! isUint32 ( publicExponent ) ) {
184
- throw new ERR_INVALID_ARG_VALUE (
185
- 'options.publicExponent' ,
186
- publicExponent ) ;
183
+ } else {
184
+ validateUint32 ( publicExponent , 'options.publicExponent' ) ;
187
185
}
188
186
189
187
if ( type === 'rsa' ) {
@@ -201,22 +199,20 @@ function createJob(mode, type, options) {
201
199
202
200
const pendingDeprecation = getOptionValue ( '--pending-deprecation' ) ;
203
201
204
- if ( saltLength !== undefined && ( ! isInt32 ( saltLength ) || saltLength < 0 ) )
205
- throw new ERR_INVALID_ARG_VALUE ( 'options.saltLength' , saltLength ) ;
206
- if ( hashAlgorithm !== undefined && typeof hashAlgorithm !== 'string' )
207
- throw new ERR_INVALID_ARG_VALUE ( 'options.hashAlgorithm' , hashAlgorithm ) ;
208
- if ( mgf1HashAlgorithm !== undefined &&
209
- typeof mgf1HashAlgorithm !== 'string' )
210
- throw new ERR_INVALID_ARG_VALUE ( 'options.mgf1HashAlgorithm' ,
211
- mgf1HashAlgorithm ) ;
202
+ if ( saltLength !== undefined )
203
+ validateInt32 ( saltLength , 'options.saltLength' , 0 ) ;
204
+ if ( hashAlgorithm !== undefined )
205
+ validateString ( hashAlgorithm , 'options.hashAlgorithm' ) ;
206
+ if ( mgf1HashAlgorithm !== undefined )
207
+ validateString ( mgf1HashAlgorithm , 'options.mgf1HashAlgorithm' ) ;
212
208
if ( hash !== undefined ) {
213
209
pendingDeprecation && process . emitWarning (
214
210
'"options.hash" is deprecated, ' +
215
211
'use "options.hashAlgorithm" instead.' ,
216
212
'DeprecationWarning' ,
217
213
'DEP0154' ) ;
218
- if ( typeof hash !== 'string' ||
219
- ( hashAlgorithm && hash !== hashAlgorithm ) ) {
214
+ validateString ( hash , 'options.hash' ) ;
215
+ if ( hashAlgorithm && hash !== hashAlgorithm ) {
220
216
throw new ERR_INVALID_ARG_VALUE ( 'options.hash' , hash ) ;
221
217
}
222
218
}
@@ -226,8 +222,8 @@ function createJob(mode, type, options) {
226
222
'use "options.mgf1HashAlgorithm" instead.' ,
227
223
'DeprecationWarning' ,
228
224
'DEP0154' ) ;
229
- if ( typeof mgf1Hash !== 'string' ||
230
- ( mgf1HashAlgorithm && mgf1Hash !== mgf1HashAlgorithm ) ) {
225
+ validateString ( mgf1Hash , 'options.mgf1Hash' ) ;
226
+ if ( mgf1HashAlgorithm && mgf1Hash !== mgf1HashAlgorithm ) {
231
227
throw new ERR_INVALID_ARG_VALUE ( 'options.mgf1Hash' , mgf1Hash ) ;
232
228
}
233
229
}
@@ -246,15 +242,13 @@ function createJob(mode, type, options) {
246
242
{
247
243
validateObject ( options , 'options' ) ;
248
244
const { modulusLength } = options ;
249
- if ( ! isUint32 ( modulusLength ) )
250
- throw new ERR_INVALID_ARG_VALUE ( 'options.modulusLength' , modulusLength ) ;
245
+ validateUint32 ( modulusLength , 'options.modulusLength' ) ;
251
246
252
247
let { divisorLength } = options ;
253
248
if ( divisorLength == null ) {
254
249
divisorLength = - 1 ;
255
- } else if ( ! isInt32 ( divisorLength ) || divisorLength < 0 ) {
256
- throw new ERR_INVALID_ARG_VALUE ( 'options.divisorLength' , divisorLength ) ;
257
- }
250
+ } else
251
+ validateInt32 ( divisorLength , 'options.divisorLength' , 0 ) ;
258
252
259
253
return new DsaKeyPairGenJob (
260
254
mode ,
@@ -266,8 +260,7 @@ function createJob(mode, type, options) {
266
260
{
267
261
validateObject ( options , 'options' ) ;
268
262
const { namedCurve } = options ;
269
- if ( typeof namedCurve !== 'string' )
270
- throw new ERR_INVALID_ARG_VALUE ( 'options.namedCurve' , namedCurve ) ;
263
+ validateString ( namedCurve , 'options.namedCurve' ) ;
271
264
let { paramEncoding } = options ;
272
265
if ( paramEncoding == null || paramEncoding === 'named' )
273
266
paramEncoding = OPENSSL_EC_NAMED_CURVE ;
@@ -315,28 +308,26 @@ function createJob(mode, type, options) {
315
308
throw new ERR_INCOMPATIBLE_OPTION_PAIR ( 'group' , 'primeLength' ) ;
316
309
if ( generator != null )
317
310
throw new ERR_INCOMPATIBLE_OPTION_PAIR ( 'group' , 'generator' ) ;
318
- if ( typeof group !== 'string' )
319
- throw new ERR_INVALID_ARG_VALUE ( 'options.group' , group ) ;
311
+
312
+ validateString ( group , 'options.group' ) ;
320
313
321
314
return new DhKeyPairGenJob ( mode , group , ...encoding ) ;
322
315
}
323
316
324
317
if ( prime != null ) {
325
318
if ( primeLength != null )
326
319
throw new ERR_INCOMPATIBLE_OPTION_PAIR ( 'prime' , 'primeLength' ) ;
327
- if ( ! isArrayBufferView ( prime ) )
328
- throw new ERR_INVALID_ARG_VALUE ( 'options.prime' , prime ) ;
320
+
321
+ validateBuffer ( prime , 'options.prime' ) ;
329
322
} else if ( primeLength != null ) {
330
- if ( ! isInt32 ( primeLength ) || primeLength < 0 )
331
- throw new ERR_INVALID_ARG_VALUE ( 'options.primeLength' , primeLength ) ;
323
+ validateInt32 ( primeLength , 'options.primeLength' , 0 ) ;
332
324
} else {
333
325
throw new ERR_MISSING_OPTION (
334
326
'At least one of the group, prime, or primeLength options' ) ;
335
327
}
336
328
337
329
if ( generator != null ) {
338
- if ( ! isInt32 ( generator ) || generator < 0 )
339
- throw new ERR_INVALID_ARG_VALUE ( 'options.generator' , generator ) ;
330
+ validateInt32 ( generator , 'options.generator' , 0 ) ;
340
331
}
341
332
return new DhKeyPairGenJob (
342
333
mode ,
0 commit comments