@@ -24,7 +24,7 @@ assert.strictEqual(dh1.verifyError, 0);
24
24
assert . strictEqual ( dh2 . verifyError , 0 ) ;
25
25
26
26
const argumentsError =
27
- / ^ T y p e E r r o r : F i r s t a r g u m e n t s h o u l d b e n u m b e r , s t r i n g o r B u f f e r $ / ;
27
+ / ^ T y p e E r r o r : F i r s t a r g u m e n t s h o u l d b e n u m b e r , s t r i n g , U i n t 8 A r r a y o r B u f f e r $ / ;
28
28
29
29
assert . throws ( ( ) => {
30
30
crypto . createDiffieHellman ( [ 0x1 , 0x2 ] ) ;
@@ -112,45 +112,69 @@ const modp2buf = Buffer.from([
112
112
0x1f , 0xe6 , 0x49 , 0x28 , 0x66 , 0x51 , 0xec , 0xe6 , 0x53 , 0x81 ,
113
113
0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff
114
114
] ) ;
115
- const exmodp2 = crypto . createDiffieHellman ( modp2buf , Buffer . from ( [ 2 ] ) ) ;
116
- modp2 . generateKeys ( ) ;
117
- exmodp2 . generateKeys ( ) ;
118
- let modp2Secret = modp2 . computeSecret ( exmodp2 . getPublicKey ( ) ) . toString ( 'hex' ) ;
119
- const exmodp2Secret = exmodp2 . computeSecret ( modp2 . getPublicKey ( ) )
120
- . toString ( 'hex' ) ;
121
- assert . strictEqual ( modp2Secret , exmodp2Secret ) ;
122
- assert . strictEqual ( modp2 . verifyError , DH_NOT_SUITABLE_GENERATOR ) ;
123
- assert . strictEqual ( exmodp2 . verifyError , DH_NOT_SUITABLE_GENERATOR ) ;
124
-
125
-
126
- // Ensure specific generator (string with encoding) works as expected.
127
- const exmodp2_2 = crypto . createDiffieHellman ( modp2buf , '02' , 'hex' ) ;
128
- exmodp2_2 . generateKeys ( ) ;
129
- modp2Secret = modp2 . computeSecret ( exmodp2_2 . getPublicKey ( ) ) . toString ( 'hex' ) ;
130
- const exmodp2_2Secret = exmodp2_2 . computeSecret ( modp2 . getPublicKey ( ) )
131
- . toString ( 'hex' ) ;
132
- assert . strictEqual ( modp2Secret , exmodp2_2Secret ) ;
133
- assert . strictEqual ( exmodp2_2 . verifyError , DH_NOT_SUITABLE_GENERATOR ) ;
134
-
135
-
136
- // Ensure specific generator (string without encoding) works as expected.
137
- const exmodp2_3 = crypto . createDiffieHellman ( modp2buf , '\x02' ) ;
138
- exmodp2_3 . generateKeys ( ) ;
139
- modp2Secret = modp2 . computeSecret ( exmodp2_3 . getPublicKey ( ) ) . toString ( 'hex' ) ;
140
- const exmodp2_3Secret = exmodp2_3 . computeSecret ( modp2 . getPublicKey ( ) )
141
- . toString ( 'hex' ) ;
142
- assert . strictEqual ( modp2Secret , exmodp2_3Secret ) ;
143
- assert . strictEqual ( exmodp2_3 . verifyError , DH_NOT_SUITABLE_GENERATOR ) ;
144
-
145
-
146
- // Ensure specific generator (numeric) works as expected.
147
- const exmodp2_4 = crypto . createDiffieHellman ( modp2buf , 2 ) ;
148
- exmodp2_4 . generateKeys ( ) ;
149
- modp2Secret = modp2 . computeSecret ( exmodp2_4 . getPublicKey ( ) ) . toString ( 'hex' ) ;
150
- const exmodp2_4Secret = exmodp2_4 . computeSecret ( modp2 . getPublicKey ( ) )
151
- . toString ( 'hex' ) ;
152
- assert . strictEqual ( modp2Secret , exmodp2_4Secret ) ;
153
- assert . strictEqual ( exmodp2_4 . verifyError , DH_NOT_SUITABLE_GENERATOR ) ;
115
+
116
+ {
117
+ const exmodp2 = crypto . createDiffieHellman ( modp2buf , Buffer . from ( [ 2 ] ) ) ;
118
+ modp2 . generateKeys ( ) ;
119
+ exmodp2 . generateKeys ( ) ;
120
+ const modp2Secret = modp2 . computeSecret ( exmodp2 . getPublicKey ( ) )
121
+ . toString ( 'hex' ) ;
122
+ const exmodp2Secret = exmodp2 . computeSecret ( modp2 . getPublicKey ( ) )
123
+ . toString ( 'hex' ) ;
124
+ assert . strictEqual ( modp2Secret , exmodp2Secret ) ;
125
+ assert . strictEqual ( modp2 . verifyError , DH_NOT_SUITABLE_GENERATOR ) ;
126
+ assert . strictEqual ( exmodp2 . verifyError , DH_NOT_SUITABLE_GENERATOR ) ;
127
+ }
128
+
129
+ {
130
+ // Ensure specific generator (string with encoding) works as expected.
131
+ const exmodp2 = crypto . createDiffieHellman ( modp2buf , '02' , 'hex' ) ;
132
+ exmodp2 . generateKeys ( ) ;
133
+ const modp2Secret = modp2 . computeSecret ( exmodp2 . getPublicKey ( ) )
134
+ . toString ( 'hex' ) ;
135
+ const exmodp2Secret = exmodp2 . computeSecret ( modp2 . getPublicKey ( ) )
136
+ . toString ( 'hex' ) ;
137
+ assert . strictEqual ( modp2Secret , exmodp2Secret ) ;
138
+ assert . strictEqual ( exmodp2 . verifyError , DH_NOT_SUITABLE_GENERATOR ) ;
139
+ }
140
+
141
+ {
142
+ // Ensure specific generator (string with encoding) works as expected,
143
+ // with a Uint8Array as the first argument to createDiffieHellman().
144
+ const exmodp2 = crypto . createDiffieHellman ( new Uint8Array ( modp2buf ) ,
145
+ '02' , 'hex' ) ;
146
+ exmodp2 . generateKeys ( ) ;
147
+ const modp2Secret = modp2 . computeSecret ( exmodp2 . getPublicKey ( ) )
148
+ . toString ( 'hex' ) ;
149
+ const exmodp2Secret = exmodp2 . computeSecret ( modp2 . getPublicKey ( ) )
150
+ . toString ( 'hex' ) ;
151
+ assert . strictEqual ( modp2Secret , exmodp2Secret ) ;
152
+ assert . strictEqual ( exmodp2 . verifyError , DH_NOT_SUITABLE_GENERATOR ) ;
153
+ }
154
+
155
+ {
156
+ // Ensure specific generator (string without encoding) works as expected.
157
+ const exmodp2 = crypto . createDiffieHellman ( modp2buf , '\x02' ) ;
158
+ exmodp2 . generateKeys ( ) ;
159
+ const modp2Secret = modp2 . computeSecret ( exmodp2 . getPublicKey ( ) )
160
+ . toString ( 'hex' ) ;
161
+ const exmodp2Secret = exmodp2 . computeSecret ( modp2 . getPublicKey ( ) )
162
+ . toString ( 'hex' ) ;
163
+ assert . strictEqual ( modp2Secret , exmodp2Secret ) ;
164
+ assert . strictEqual ( exmodp2 . verifyError , DH_NOT_SUITABLE_GENERATOR ) ;
165
+ }
166
+
167
+ {
168
+ // Ensure specific generator (numeric) works as expected.
169
+ const exmodp2 = crypto . createDiffieHellman ( modp2buf , 2 ) ;
170
+ exmodp2 . generateKeys ( ) ;
171
+ const modp2Secret = modp2 . computeSecret ( exmodp2 . getPublicKey ( ) )
172
+ . toString ( 'hex' ) ;
173
+ const exmodp2Secret = exmodp2 . computeSecret ( modp2 . getPublicKey ( ) )
174
+ . toString ( 'hex' ) ;
175
+ assert . strictEqual ( modp2Secret , exmodp2Secret ) ;
176
+ assert . strictEqual ( exmodp2 . verifyError , DH_NOT_SUITABLE_GENERATOR ) ;
177
+ }
154
178
155
179
156
180
const p = 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74' +
0 commit comments