1
1
'use strict' ;
2
- var common = require ( '../common' ) ;
3
- var assert = require ( 'assert' ) ;
2
+ const common = require ( '../common' ) ;
4
3
5
4
if ( ! common . hasCrypto ) {
6
5
common . skip ( 'missing crypto' ) ;
@@ -10,21 +9,22 @@ if (common.hasFipsCrypto) {
10
9
common . skip ( 'not supported in FIPS mode' ) ;
11
10
return ;
12
11
}
13
- var crypto = require ( 'crypto' ) ;
12
+ const crypto = require ( 'crypto' ) ;
13
+ const assert = require ( 'assert' ) ;
14
14
15
15
function testCipher1 ( key ) {
16
16
// Test encryption and decryption
17
- var plaintext = 'Keep this a secret? No! Tell everyone about node.js!' ;
18
- var cipher = crypto . createCipher ( 'aes192' , key ) ;
17
+ const plaintext = 'Keep this a secret? No! Tell everyone about node.js!' ;
18
+ const cipher = crypto . createCipher ( 'aes192' , key ) ;
19
19
20
20
// encrypt plaintext which is in utf8 format
21
21
// to a ciphertext which will be in hex
22
- var ciph = cipher . update ( plaintext , 'utf8' , 'hex' ) ;
22
+ let ciph = cipher . update ( plaintext , 'utf8' , 'hex' ) ;
23
23
// Only use binary or hex, not base64.
24
24
ciph += cipher . final ( 'hex' ) ;
25
25
26
- var decipher = crypto . createDecipher ( 'aes192' , key ) ;
27
- var txt = decipher . update ( ciph , 'hex' , 'utf8' ) ;
26
+ const decipher = crypto . createDecipher ( 'aes192' , key ) ;
27
+ let txt = decipher . update ( ciph , 'hex' , 'utf8' ) ;
28
28
txt += decipher . final ( 'utf8' ) ;
29
29
30
30
assert . strictEqual ( txt , plaintext , 'encryption and decryption' ) ;
@@ -33,11 +33,11 @@ function testCipher1(key) {
33
33
// NB: In real life, it's not guaranteed that you can get all of it
34
34
// in a single read() like this. But in this case, we know it's
35
35
// quite small, so there's no harm.
36
- var cStream = crypto . createCipher ( 'aes192' , key ) ;
36
+ const cStream = crypto . createCipher ( 'aes192' , key ) ;
37
37
cStream . end ( plaintext ) ;
38
38
ciph = cStream . read ( ) ;
39
39
40
- var dStream = crypto . createDecipher ( 'aes192' , key ) ;
40
+ const dStream = crypto . createDecipher ( 'aes192' , key ) ;
41
41
dStream . end ( ciph ) ;
42
42
txt = dStream . read ( ) . toString ( 'utf8' ) ;
43
43
@@ -48,19 +48,19 @@ function testCipher1(key) {
48
48
function testCipher2 ( key ) {
49
49
// encryption and decryption with Base64
50
50
// reported in https://github.com/joyent/node/issues/738
51
- var plaintext =
51
+ const plaintext =
52
52
'32|RmVZZkFUVmpRRkp0TmJaUm56ZU9qcnJkaXNNWVNpTTU*|iXmckfRWZBGWWELw' +
53
53
'eCBsThSsfUHLeRe0KCsK8ooHgxie0zOINpXxfZi/oNG7uq9JWFVCk70gfzQH8ZUJ' +
54
54
'jAfaFg**' ;
55
- var cipher = crypto . createCipher ( 'aes256' , key ) ;
55
+ const cipher = crypto . createCipher ( 'aes256' , key ) ;
56
56
57
57
// encrypt plaintext which is in utf8 format
58
58
// to a ciphertext which will be in Base64
59
- var ciph = cipher . update ( plaintext , 'utf8' , 'base64' ) ;
59
+ let ciph = cipher . update ( plaintext , 'utf8' , 'base64' ) ;
60
60
ciph += cipher . final ( 'base64' ) ;
61
61
62
- var decipher = crypto . createDecipher ( 'aes256' , key ) ;
63
- var txt = decipher . update ( ciph , 'base64' , 'utf8' ) ;
62
+ const decipher = crypto . createDecipher ( 'aes256' , key ) ;
63
+ let txt = decipher . update ( ciph , 'base64' , 'utf8' ) ;
64
64
txt += decipher . final ( 'utf8' ) ;
65
65
66
66
assert . strictEqual ( txt , plaintext , 'encryption and decryption with Base64' ) ;
@@ -119,12 +119,12 @@ testCipher2(Buffer.from('0123456789abcdef'));
119
119
const key = '0123456789abcdef' ;
120
120
const plaintext = 'Top secret!!!' ;
121
121
const c = crypto . createCipher ( 'aes192' , key ) ;
122
- var ciph = c . update ( plaintext , 'utf16le' , 'base64' ) ;
122
+ let ciph = c . update ( plaintext , 'utf16le' , 'base64' ) ;
123
123
ciph += c . final ( 'base64' ) ;
124
124
125
- var decipher = crypto . createDecipher ( 'aes192' , key ) ;
125
+ let decipher = crypto . createDecipher ( 'aes192' , key ) ;
126
126
127
- var txt ;
127
+ let txt ;
128
128
assert . doesNotThrow ( ( ) => txt = decipher . update ( ciph , 'base64' , 'ucs2' ) ) ;
129
129
assert . doesNotThrow ( ( ) => txt += decipher . final ( 'ucs2' ) ) ;
130
130
assert . strictEqual ( txt , plaintext , 'decrypted result in ucs2' ) ;
0 commit comments