@@ -6,19 +6,26 @@ if (!common.hasCrypto)
6
6
const assert = require ( 'assert' ) ;
7
7
const crypto = require ( 'crypto' ) ;
8
8
9
- //
10
- // Test PBKDF2 with RFC 6070 test vectors (except #4)
11
- //
12
- function testPBKDF2 ( password , salt , iterations , keylen , expected ) {
13
- const actual =
14
- crypto . pbkdf2Sync ( password , salt , iterations , keylen , 'sha256' ) ;
15
- assert . strictEqual ( actual . toString ( 'latin1' ) , expected ) ;
9
+ function runPBKDF2 ( password , salt , iterations , keylen , hash ) {
10
+ const syncResult =
11
+ crypto . pbkdf2Sync ( password , salt , iterations , keylen , hash ) ;
16
12
17
- crypto . pbkdf2 ( password , salt , iterations , keylen , 'sha256' , ( err , actual ) => {
18
- assert . strictEqual ( actual . toString ( 'latin1' ) , expected ) ;
19
- } ) ;
13
+ crypto . pbkdf2 ( password , salt , iterations , keylen , hash ,
14
+ common . mustSucceed ( ( asyncResult ) => {
15
+ assert . deepStrictEqual ( asyncResult , syncResult ) ;
16
+ } ) ) ;
17
+
18
+ return syncResult ;
19
+ }
20
+
21
+ function testPBKDF2 ( password , salt , iterations , keylen , expected , encoding ) {
22
+ const actual = runPBKDF2 ( password , salt , iterations , keylen , 'sha256' ) ;
23
+ assert . strictEqual ( actual . toString ( encoding || 'latin1' ) , expected ) ;
20
24
}
21
25
26
+ //
27
+ // Test PBKDF2 with RFC 6070 test vectors (except #4)
28
+ //
22
29
23
30
testPBKDF2 ( 'password' , 'salt' , 1 , 20 ,
24
31
'\x12\x0f\xb6\xcf\xfc\xf8\xb3\x2c\x43\xe7\x22\x52' +
@@ -43,15 +50,9 @@ testPBKDF2('pass\0word', 'sa\0lt', 4096, 16,
43
50
'\x89\xb6\x9d\x05\x16\xf8\x29\x89\x3c\x69\x62\x26\x65' +
44
51
'\x0a\x86\x87' ) ;
45
52
46
- const expected =
47
- '64c486c55d30d4c5a079b8823b7d7cb37ff0556f537da8410233bcec330ed956' ;
48
- const key = crypto . pbkdf2Sync ( 'password' , 'salt' , 32 , 32 , 'sha256' ) ;
49
- assert . strictEqual ( key . toString ( 'hex' ) , expected ) ;
50
-
51
- crypto . pbkdf2 ( 'password' , 'salt' , 32 , 32 , 'sha256' , common . mustSucceed ( ondone ) ) ;
52
- function ondone ( key ) {
53
- assert . strictEqual ( key . toString ( 'hex' ) , expected ) ;
54
- }
53
+ testPBKDF2 ( 'password' , 'salt' , 32 , 32 ,
54
+ '64c486c55d30d4c5a079b8823b7d7cb37ff0556f537da8410233bcec330ed956' ,
55
+ 'hex' ) ;
55
56
56
57
// Error path should not leak memory (check with valgrind).
57
58
assert . throws (
@@ -197,38 +198,12 @@ assert.throws(
197
198
) ;
198
199
} ) ;
199
200
200
- // Any TypedArray should work for password and salt
201
- crypto . pbkdf2 ( new Uint8Array ( 10 ) , 'salt' , 8 , 8 , 'sha256' , common . mustSucceed ( ) ) ;
202
- crypto . pbkdf2 ( 'pass' , new Uint8Array ( 10 ) , 8 , 8 , 'sha256' , common . mustSucceed ( ) ) ;
203
- crypto . pbkdf2 ( new Uint16Array ( 10 ) , 'salt' , 8 , 8 , 'sha1' , common . mustSucceed ( ) ) ;
204
- crypto . pbkdf2 ( 'pass' , new Uint16Array ( 10 ) , 8 , 8 , 'sha1' , common . mustSucceed ( ) ) ;
205
- crypto . pbkdf2 ( new Uint32Array ( 10 ) , 'salt' , 8 , 8 , 'sha1' , common . mustSucceed ( ) ) ;
206
- crypto . pbkdf2 ( 'pass' , new Uint32Array ( 10 ) , 8 , 8 , 'sha1' , common . mustSucceed ( ) ) ;
207
- crypto . pbkdf2 ( new Float32Array ( 10 ) , 'salt' , 8 , 8 , 'sha1' , common . mustSucceed ( ) ) ;
208
- crypto . pbkdf2 ( 'pass' , new Float32Array ( 10 ) , 8 , 8 , 'sha1' , common . mustSucceed ( ) ) ;
209
- crypto . pbkdf2 ( new Float64Array ( 10 ) , 'salt' , 8 , 8 , 'sha1' , common . mustSucceed ( ) ) ;
210
- crypto . pbkdf2 ( 'pass' , new Float64Array ( 10 ) , 8 , 8 , 'sha1' , common . mustSucceed ( ) ) ;
211
- crypto . pbkdf2 ( new ArrayBuffer ( 10 ) , 'salt' , 8 , 8 , 'sha1' , common . mustSucceed ( ) ) ;
212
- crypto . pbkdf2 ( 'pass' , new ArrayBuffer ( 10 ) , 8 , 8 , 'sha1' , common . mustSucceed ( ) ) ;
213
- crypto . pbkdf2 ( new SharedArrayBuffer ( 10 ) , 'salt' , 8 , 8 , 'sha256' ,
214
- common . mustSucceed ( ) ) ;
215
- crypto . pbkdf2 ( 'pass' , new SharedArrayBuffer ( 10 ) , 8 , 8 , 'sha256' ,
216
- common . mustSucceed ( ) ) ;
217
-
218
- crypto . pbkdf2Sync ( new Uint8Array ( 10 ) , 'salt' , 8 , 8 , 'sha256' ) ;
219
- crypto . pbkdf2Sync ( 'pass' , new Uint8Array ( 10 ) , 8 , 8 , 'sha256' ) ;
220
- crypto . pbkdf2Sync ( new Uint16Array ( 10 ) , 'salt' , 8 , 8 , 'sha256' ) ;
221
- crypto . pbkdf2Sync ( 'pass' , new Uint16Array ( 10 ) , 8 , 8 , 'sha256' ) ;
222
- crypto . pbkdf2Sync ( new Uint32Array ( 10 ) , 'salt' , 8 , 8 , 'sha256' ) ;
223
- crypto . pbkdf2Sync ( 'pass' , new Uint32Array ( 10 ) , 8 , 8 , 'sha256' ) ;
224
- crypto . pbkdf2Sync ( new Float32Array ( 10 ) , 'salt' , 8 , 8 , 'sha256' ) ;
225
- crypto . pbkdf2Sync ( 'pass' , new Float32Array ( 10 ) , 8 , 8 , 'sha256' ) ;
226
- crypto . pbkdf2Sync ( new Float64Array ( 10 ) , 'salt' , 8 , 8 , 'sha256' ) ;
227
- crypto . pbkdf2Sync ( 'pass' , new Float64Array ( 10 ) , 8 , 8 , 'sha256' ) ;
228
- crypto . pbkdf2Sync ( new ArrayBuffer ( 10 ) , 'salt' , 8 , 8 , 'sha256' ) ;
229
- crypto . pbkdf2Sync ( 'pass' , new ArrayBuffer ( 10 ) , 8 , 8 , 'sha256' ) ;
230
- crypto . pbkdf2Sync ( new SharedArrayBuffer ( 10 ) , 'salt' , 8 , 8 , 'sha256' ) ;
231
- crypto . pbkdf2Sync ( 'pass' , new SharedArrayBuffer ( 10 ) , 8 , 8 , 'sha256' ) ;
201
+ // Any TypedArray should work for password and salt.
202
+ for ( const SomeArray of [ Uint8Array , Uint16Array , Uint32Array , Float32Array ,
203
+ Float64Array , ArrayBuffer , SharedArrayBuffer ] ) {
204
+ runPBKDF2 ( new SomeArray ( 10 ) , 'salt' , 8 , 8 , 'sha256' ) ;
205
+ runPBKDF2 ( 'pass' , new SomeArray ( 10 ) , 8 , 8 , 'sha256' ) ;
206
+ }
232
207
233
208
assert . throws (
234
209
( ) => crypto . pbkdf2 ( 'pass' , 'salt' , 8 , 8 , 'md55' , common . mustNotCall ( ) ) ,
@@ -252,6 +227,5 @@ const kNotPBKDF2Supported = ['shake128', 'shake256'];
252
227
crypto . getHashes ( )
253
228
. filter ( ( hash ) => ! kNotPBKDF2Supported . includes ( hash ) )
254
229
. forEach ( ( hash ) => {
255
- crypto . pbkdf2Sync ( new Uint8Array ( 10 ) , 'salt' , 8 , 8 , hash ) ;
256
- crypto . pbkdf2 ( new Uint8Array ( 10 ) , 'salt' , 8 , 8 , hash , common . mustCall ( ) ) ;
230
+ runPBKDF2 ( new Uint8Array ( 10 ) , 'salt' , 8 , 8 , hash ) ;
257
231
} ) ;
0 commit comments