@@ -26,6 +26,9 @@ const dsaKeyPem = fs.readFileSync(common.fixturesDir + '/test_dsa_privkey.pem',
2626const dsaKeyPemEncrypted = fs . readFileSync (
2727 common . fixturesDir + '/test_dsa_privkey_encrypted.pem' , 'ascii' ) ;
2828
29+ const decryptError = new RegExp ( '^Error: error:06065064:digital envelope ' +
30+ 'routines:EVP_DecryptFinal_ex:bad decrypt$' ) ;
31+
2932// Test RSA encryption/decryption
3033{
3134 const input = 'I AM THE WALRUS' ;
@@ -34,13 +37,13 @@ const dsaKeyPemEncrypted = fs.readFileSync(
3437 let encryptedBuffer = crypto . publicEncrypt ( rsaPubPem , bufferToEncrypt ) ;
3538
3639 let decryptedBuffer = crypto . privateDecrypt ( rsaKeyPem , encryptedBuffer ) ;
37- assert . strictEqual ( input , decryptedBuffer . toString ( ) ) ;
40+ assert . strictEqual ( decryptedBuffer . toString ( ) , input ) ;
3841
3942 let decryptedBufferWithPassword = crypto . privateDecrypt ( {
4043 key : rsaKeyPemEncrypted ,
4144 passphrase : 'password'
4245 } , encryptedBuffer ) ;
43- assert . strictEqual ( input , decryptedBufferWithPassword . toString ( ) ) ;
46+ assert . strictEqual ( decryptedBufferWithPassword . toString ( ) , input ) ;
4447
4548 encryptedBuffer = crypto . publicEncrypt ( {
4649 key : rsaKeyPemEncrypted ,
@@ -51,7 +54,7 @@ const dsaKeyPemEncrypted = fs.readFileSync(
5154 key : rsaKeyPemEncrypted ,
5255 passphrase : 'password'
5356 } , encryptedBuffer ) ;
54- assert . strictEqual ( input , decryptedBufferWithPassword . toString ( ) ) ;
57+ assert . strictEqual ( decryptedBufferWithPassword . toString ( ) , input ) ;
5558
5659 encryptedBuffer = crypto . privateEncrypt ( {
5760 key : rsaKeyPemEncrypted ,
@@ -62,53 +65,53 @@ const dsaKeyPemEncrypted = fs.readFileSync(
6265 key : rsaKeyPemEncrypted ,
6366 passphrase : Buffer . from ( 'password' )
6467 } , encryptedBuffer ) ;
65- assert . strictEqual ( input , decryptedBufferWithPassword . toString ( ) ) ;
68+ assert . strictEqual ( decryptedBufferWithPassword . toString ( ) , input ) ;
6669
6770 encryptedBuffer = crypto . publicEncrypt ( certPem , bufferToEncrypt ) ;
6871
6972 decryptedBuffer = crypto . privateDecrypt ( keyPem , encryptedBuffer ) ;
70- assert . strictEqual ( input , decryptedBuffer . toString ( ) ) ;
73+ assert . strictEqual ( decryptedBuffer . toString ( ) , input ) ;
7174
7275 encryptedBuffer = crypto . publicEncrypt ( keyPem , bufferToEncrypt ) ;
7376
7477 decryptedBuffer = crypto . privateDecrypt ( keyPem , encryptedBuffer ) ;
75- assert . strictEqual ( input , decryptedBuffer . toString ( ) ) ;
78+ assert . strictEqual ( decryptedBuffer . toString ( ) , input ) ;
7679
7780 encryptedBuffer = crypto . privateEncrypt ( keyPem , bufferToEncrypt ) ;
7881
7982 decryptedBuffer = crypto . publicDecrypt ( keyPem , encryptedBuffer ) ;
80- assert . strictEqual ( input , decryptedBuffer . toString ( ) ) ;
83+ assert . strictEqual ( decryptedBuffer . toString ( ) , input ) ;
8184
82- assert . throws ( function ( ) {
85+ assert . throws ( ( ) => {
8386 crypto . privateDecrypt ( {
8487 key : rsaKeyPemEncrypted ,
8588 passphrase : 'wrong'
8689 } , bufferToEncrypt ) ;
87- } ) ;
90+ } , decryptError ) ;
8891
89- assert . throws ( function ( ) {
92+ assert . throws ( ( ) => {
9093 crypto . publicEncrypt ( {
9194 key : rsaKeyPemEncrypted ,
9295 passphrase : 'wrong'
9396 } , encryptedBuffer ) ;
94- } ) ;
97+ } , decryptError ) ;
9598
9699 encryptedBuffer = crypto . privateEncrypt ( {
97100 key : rsaKeyPemEncrypted ,
98101 passphrase : Buffer . from ( 'password' )
99102 } , bufferToEncrypt ) ;
100103
101- assert . throws ( function ( ) {
104+ assert . throws ( ( ) => {
102105 crypto . publicDecrypt ( {
103106 key : rsaKeyPemEncrypted ,
104107 passphrase : [ ] . concat . apply ( [ ] , Buffer . from ( 'password' ) )
105108 } , encryptedBuffer ) ;
106- } ) ;
109+ } , decryptError ) ;
107110}
108111
109112function test_rsa ( padding ) {
110- const input = Buffer
111- . allocUnsafe ( padding === 'RSA_NO_PADDING' ? 1024 / 8 : 32 ) ;
113+ const size = ( padding === 'RSA_NO_PADDING' ) ? 1024 / 8 : 32 ;
114+ const input = Buffer . allocUnsafe ( size ) ;
112115 for ( let i = 0 ; i < input . length ; i ++ )
113116 input [ i ] = ( i * 7 + 11 ) & 0xff ;
114117 const bufferToEncrypt = Buffer . from ( input ) ;
@@ -124,7 +127,7 @@ function test_rsa(padding) {
124127 key : rsaKeyPem ,
125128 padding : padding
126129 } , encryptedBuffer ) ;
127- assert . strictEqual ( input . toString ( ) , decryptedBuffer . toString ( ) ) ;
130+ assert . deepStrictEqual ( decryptedBuffer , input ) ;
128131}
129132
130133test_rsa ( 'RSA_NO_PADDING' ) ;
@@ -137,42 +140,39 @@ let rsaVerify = crypto.createVerify('RSA-SHA1');
137140assert . ok ( rsaSign ) ;
138141assert . ok ( rsaVerify ) ;
139142
143+ const expectedSignature =
144+ '5c50e3145c4e2497aadb0eabc83b342d0b0021ece0d4c4a064b7c' +
145+ '8f020d7e2688b122bfb54c724ac9ee169f83f66d2fe90abeb95e8' +
146+ 'e1290e7e177152a4de3d944cf7d4883114a20ed0f78e70e25ef0f' +
147+ '60f06b858e6af42a2f276ede95bbc6bc9a9bbdda15bd663186a6f' +
148+ '40819a7af19e577bb2efa5e579a1f5ce8a0d4ca8b8f6' ;
149+
140150rsaSign . update ( rsaPubPem ) ;
141151let rsaSignature = rsaSign . sign ( rsaKeyPem , 'hex' ) ;
142- assert . strictEqual ( rsaSignature ,
143- '5c50e3145c4e2497aadb0eabc83b342d0b0021ece0d4c4a064b7c' +
144- '8f020d7e2688b122bfb54c724ac9ee169f83f66d2fe90abeb95e8' +
145- 'e1290e7e177152a4de3d944cf7d4883114a20ed0f78e70e25ef0f' +
146- '60f06b858e6af42a2f276ede95bbc6bc9a9bbdda15bd663186a6f' +
147- '40819a7af19e577bb2efa5e579a1f5ce8a0d4ca8b8f6' ) ;
152+ assert . strictEqual ( rsaSignature , expectedSignature ) ;
148153
149154rsaVerify . update ( rsaPubPem ) ;
150155assert . strictEqual ( rsaVerify . verify ( rsaPubPem , rsaSignature , 'hex' ) , true ) ;
151156
152157// Test RSA key signing/verification with encrypted key
153158rsaSign = crypto . createSign ( 'RSA-SHA1' ) ;
154159rsaSign . update ( rsaPubPem ) ;
155- assert . doesNotThrow ( function ( ) {
160+ assert . doesNotThrow ( ( ) => {
156161 const signOptions = { key : rsaKeyPemEncrypted , passphrase : 'password' } ;
157162 rsaSignature = rsaSign . sign ( signOptions , 'hex' ) ;
158163} ) ;
159- assert . strictEqual ( rsaSignature ,
160- '5c50e3145c4e2497aadb0eabc83b342d0b0021ece0d4c4a064b7c' +
161- '8f020d7e2688b122bfb54c724ac9ee169f83f66d2fe90abeb95e8' +
162- 'e1290e7e177152a4de3d944cf7d4883114a20ed0f78e70e25ef0f' +
163- '60f06b858e6af42a2f276ede95bbc6bc9a9bbdda15bd663186a6f' +
164- '40819a7af19e577bb2efa5e579a1f5ce8a0d4ca8b8f6' ) ;
164+ assert . strictEqual ( rsaSignature , expectedSignature ) ;
165165
166166rsaVerify = crypto . createVerify ( 'RSA-SHA1' ) ;
167167rsaVerify . update ( rsaPubPem ) ;
168168assert . strictEqual ( rsaVerify . verify ( rsaPubPem , rsaSignature , 'hex' ) , true ) ;
169169
170170rsaSign = crypto . createSign ( 'RSA-SHA1' ) ;
171171rsaSign . update ( rsaPubPem ) ;
172- assert . throws ( function ( ) {
172+ assert . throws ( ( ) => {
173173 const signOptions = { key : rsaKeyPemEncrypted , passphrase : 'wrong' } ;
174174 rsaSign . sign ( signOptions , 'hex' ) ;
175- } ) ;
175+ } , decryptError ) ;
176176
177177//
178178// Test RSA signing and verification
@@ -197,7 +197,7 @@ assert.throws(function() {
197197 sign . update ( input ) ;
198198
199199 const output = sign . sign ( privateKey , 'hex' ) ;
200- assert . strictEqual ( output , signature ) ;
200+ assert . strictEqual ( signature , output ) ;
201201
202202 const verify = crypto . createVerify ( 'RSA-SHA256' ) ;
203203 verify . update ( input ) ;
@@ -233,9 +233,9 @@ const input = 'I AM THE WALRUS';
233233{
234234 const sign = crypto . createSign ( 'DSS1' ) ;
235235 sign . update ( input ) ;
236- assert . throws ( function ( ) {
236+ assert . throws ( ( ) => {
237237 sign . sign ( { key : dsaKeyPemEncrypted , passphrase : 'wrong' } , 'hex' ) ;
238- } ) ;
238+ } , decryptError ) ;
239239}
240240
241241{
@@ -245,7 +245,7 @@ const input = 'I AM THE WALRUS';
245245 sign . update ( input ) ;
246246
247247 let signature ;
248- assert . doesNotThrow ( function ( ) {
248+ assert . doesNotThrow ( ( ) => {
249249 const signOptions = { key : dsaKeyPemEncrypted , passphrase : 'password' } ;
250250 signature = sign . sign ( signOptions , 'hex' ) ;
251251 } ) ;
0 commit comments