@@ -26,6 +26,9 @@ const dsaKeyPem = fs.readFileSync(common.fixturesDir + '/test_dsa_privkey.pem',
26
26
const dsaKeyPemEncrypted = fs . readFileSync (
27
27
common . fixturesDir + '/test_dsa_privkey_encrypted.pem' , 'ascii' ) ;
28
28
29
+ const decryptError = new RegExp ( '^Error: error:06065064:digital envelope ' +
30
+ 'routines:EVP_DecryptFinal_ex:bad decrypt$' ) ;
31
+
29
32
// Test RSA encryption/decryption
30
33
{
31
34
const input = 'I AM THE WALRUS' ;
@@ -34,13 +37,13 @@ const dsaKeyPemEncrypted = fs.readFileSync(
34
37
let encryptedBuffer = crypto . publicEncrypt ( rsaPubPem , bufferToEncrypt ) ;
35
38
36
39
let decryptedBuffer = crypto . privateDecrypt ( rsaKeyPem , encryptedBuffer ) ;
37
- assert . strictEqual ( input , decryptedBuffer . toString ( ) ) ;
40
+ assert . strictEqual ( decryptedBuffer . toString ( ) , input ) ;
38
41
39
42
let decryptedBufferWithPassword = crypto . privateDecrypt ( {
40
43
key : rsaKeyPemEncrypted ,
41
44
passphrase : 'password'
42
45
} , encryptedBuffer ) ;
43
- assert . strictEqual ( input , decryptedBufferWithPassword . toString ( ) ) ;
46
+ assert . strictEqual ( decryptedBufferWithPassword . toString ( ) , input ) ;
44
47
45
48
encryptedBuffer = crypto . publicEncrypt ( {
46
49
key : rsaKeyPemEncrypted ,
@@ -51,7 +54,7 @@ const dsaKeyPemEncrypted = fs.readFileSync(
51
54
key : rsaKeyPemEncrypted ,
52
55
passphrase : 'password'
53
56
} , encryptedBuffer ) ;
54
- assert . strictEqual ( input , decryptedBufferWithPassword . toString ( ) ) ;
57
+ assert . strictEqual ( decryptedBufferWithPassword . toString ( ) , input ) ;
55
58
56
59
encryptedBuffer = crypto . privateEncrypt ( {
57
60
key : rsaKeyPemEncrypted ,
@@ -62,53 +65,53 @@ const dsaKeyPemEncrypted = fs.readFileSync(
62
65
key : rsaKeyPemEncrypted ,
63
66
passphrase : Buffer . from ( 'password' )
64
67
} , encryptedBuffer ) ;
65
- assert . strictEqual ( input , decryptedBufferWithPassword . toString ( ) ) ;
68
+ assert . strictEqual ( decryptedBufferWithPassword . toString ( ) , input ) ;
66
69
67
70
encryptedBuffer = crypto . publicEncrypt ( certPem , bufferToEncrypt ) ;
68
71
69
72
decryptedBuffer = crypto . privateDecrypt ( keyPem , encryptedBuffer ) ;
70
- assert . strictEqual ( input , decryptedBuffer . toString ( ) ) ;
73
+ assert . strictEqual ( decryptedBuffer . toString ( ) , input ) ;
71
74
72
75
encryptedBuffer = crypto . publicEncrypt ( keyPem , bufferToEncrypt ) ;
73
76
74
77
decryptedBuffer = crypto . privateDecrypt ( keyPem , encryptedBuffer ) ;
75
- assert . strictEqual ( input , decryptedBuffer . toString ( ) ) ;
78
+ assert . strictEqual ( decryptedBuffer . toString ( ) , input ) ;
76
79
77
80
encryptedBuffer = crypto . privateEncrypt ( keyPem , bufferToEncrypt ) ;
78
81
79
82
decryptedBuffer = crypto . publicDecrypt ( keyPem , encryptedBuffer ) ;
80
- assert . strictEqual ( input , decryptedBuffer . toString ( ) ) ;
83
+ assert . strictEqual ( decryptedBuffer . toString ( ) , input ) ;
81
84
82
- assert . throws ( function ( ) {
85
+ assert . throws ( ( ) => {
83
86
crypto . privateDecrypt ( {
84
87
key : rsaKeyPemEncrypted ,
85
88
passphrase : 'wrong'
86
89
} , bufferToEncrypt ) ;
87
- } ) ;
90
+ } , decryptError ) ;
88
91
89
- assert . throws ( function ( ) {
92
+ assert . throws ( ( ) => {
90
93
crypto . publicEncrypt ( {
91
94
key : rsaKeyPemEncrypted ,
92
95
passphrase : 'wrong'
93
96
} , encryptedBuffer ) ;
94
- } ) ;
97
+ } , decryptError ) ;
95
98
96
99
encryptedBuffer = crypto . privateEncrypt ( {
97
100
key : rsaKeyPemEncrypted ,
98
101
passphrase : Buffer . from ( 'password' )
99
102
} , bufferToEncrypt ) ;
100
103
101
- assert . throws ( function ( ) {
104
+ assert . throws ( ( ) => {
102
105
crypto . publicDecrypt ( {
103
106
key : rsaKeyPemEncrypted ,
104
107
passphrase : [ ] . concat . apply ( [ ] , Buffer . from ( 'password' ) )
105
108
} , encryptedBuffer ) ;
106
- } ) ;
109
+ } , decryptError ) ;
107
110
}
108
111
109
112
function 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 ) ;
112
115
for ( let i = 0 ; i < input . length ; i ++ )
113
116
input [ i ] = ( i * 7 + 11 ) & 0xff ;
114
117
const bufferToEncrypt = Buffer . from ( input ) ;
@@ -124,7 +127,7 @@ function test_rsa(padding) {
124
127
key : rsaKeyPem ,
125
128
padding : padding
126
129
} , encryptedBuffer ) ;
127
- assert . strictEqual ( input . toString ( ) , decryptedBuffer . toString ( ) ) ;
130
+ assert . deepStrictEqual ( decryptedBuffer , input ) ;
128
131
}
129
132
130
133
test_rsa ( 'RSA_NO_PADDING' ) ;
@@ -137,42 +140,39 @@ let rsaVerify = crypto.createVerify('RSA-SHA1');
137
140
assert . ok ( rsaSign ) ;
138
141
assert . ok ( rsaVerify ) ;
139
142
143
+ const expectedSignature =
144
+ '5c50e3145c4e2497aadb0eabc83b342d0b0021ece0d4c4a064b7c' +
145
+ '8f020d7e2688b122bfb54c724ac9ee169f83f66d2fe90abeb95e8' +
146
+ 'e1290e7e177152a4de3d944cf7d4883114a20ed0f78e70e25ef0f' +
147
+ '60f06b858e6af42a2f276ede95bbc6bc9a9bbdda15bd663186a6f' +
148
+ '40819a7af19e577bb2efa5e579a1f5ce8a0d4ca8b8f6' ;
149
+
140
150
rsaSign . update ( rsaPubPem ) ;
141
151
let rsaSignature = rsaSign . sign ( rsaKeyPem , 'hex' ) ;
142
- assert . strictEqual ( rsaSignature ,
143
- '5c50e3145c4e2497aadb0eabc83b342d0b0021ece0d4c4a064b7c' +
144
- '8f020d7e2688b122bfb54c724ac9ee169f83f66d2fe90abeb95e8' +
145
- 'e1290e7e177152a4de3d944cf7d4883114a20ed0f78e70e25ef0f' +
146
- '60f06b858e6af42a2f276ede95bbc6bc9a9bbdda15bd663186a6f' +
147
- '40819a7af19e577bb2efa5e579a1f5ce8a0d4ca8b8f6' ) ;
152
+ assert . strictEqual ( rsaSignature , expectedSignature ) ;
148
153
149
154
rsaVerify . update ( rsaPubPem ) ;
150
155
assert . strictEqual ( rsaVerify . verify ( rsaPubPem , rsaSignature , 'hex' ) , true ) ;
151
156
152
157
// Test RSA key signing/verification with encrypted key
153
158
rsaSign = crypto . createSign ( 'RSA-SHA1' ) ;
154
159
rsaSign . update ( rsaPubPem ) ;
155
- assert . doesNotThrow ( function ( ) {
160
+ assert . doesNotThrow ( ( ) => {
156
161
const signOptions = { key : rsaKeyPemEncrypted , passphrase : 'password' } ;
157
162
rsaSignature = rsaSign . sign ( signOptions , 'hex' ) ;
158
163
} ) ;
159
- assert . strictEqual ( rsaSignature ,
160
- '5c50e3145c4e2497aadb0eabc83b342d0b0021ece0d4c4a064b7c' +
161
- '8f020d7e2688b122bfb54c724ac9ee169f83f66d2fe90abeb95e8' +
162
- 'e1290e7e177152a4de3d944cf7d4883114a20ed0f78e70e25ef0f' +
163
- '60f06b858e6af42a2f276ede95bbc6bc9a9bbdda15bd663186a6f' +
164
- '40819a7af19e577bb2efa5e579a1f5ce8a0d4ca8b8f6' ) ;
164
+ assert . strictEqual ( rsaSignature , expectedSignature ) ;
165
165
166
166
rsaVerify = crypto . createVerify ( 'RSA-SHA1' ) ;
167
167
rsaVerify . update ( rsaPubPem ) ;
168
168
assert . strictEqual ( rsaVerify . verify ( rsaPubPem , rsaSignature , 'hex' ) , true ) ;
169
169
170
170
rsaSign = crypto . createSign ( 'RSA-SHA1' ) ;
171
171
rsaSign . update ( rsaPubPem ) ;
172
- assert . throws ( function ( ) {
172
+ assert . throws ( ( ) => {
173
173
const signOptions = { key : rsaKeyPemEncrypted , passphrase : 'wrong' } ;
174
174
rsaSign . sign ( signOptions , 'hex' ) ;
175
- } ) ;
175
+ } , decryptError ) ;
176
176
177
177
//
178
178
// Test RSA signing and verification
@@ -197,7 +197,7 @@ assert.throws(function() {
197
197
sign . update ( input ) ;
198
198
199
199
const output = sign . sign ( privateKey , 'hex' ) ;
200
- assert . strictEqual ( output , signature ) ;
200
+ assert . strictEqual ( signature , output ) ;
201
201
202
202
const verify = crypto . createVerify ( 'RSA-SHA256' ) ;
203
203
verify . update ( input ) ;
@@ -233,9 +233,9 @@ const input = 'I AM THE WALRUS';
233
233
{
234
234
const sign = crypto . createSign ( 'DSS1' ) ;
235
235
sign . update ( input ) ;
236
- assert . throws ( function ( ) {
236
+ assert . throws ( ( ) => {
237
237
sign . sign ( { key : dsaKeyPemEncrypted , passphrase : 'wrong' } , 'hex' ) ;
238
- } ) ;
238
+ } , decryptError ) ;
239
239
}
240
240
241
241
{
@@ -245,7 +245,7 @@ const input = 'I AM THE WALRUS';
245
245
sign . update ( input ) ;
246
246
247
247
let signature ;
248
- assert . doesNotThrow ( function ( ) {
248
+ assert . doesNotThrow ( ( ) => {
249
249
const signOptions = { key : dsaKeyPemEncrypted , passphrase : 'password' } ;
250
250
signature = sign . sign ( signOptions , 'hex' ) ;
251
251
} ) ;
0 commit comments