@@ -14,49 +14,69 @@ class Crypto {
14
14
15
15
set ( plaintext ) {
16
16
let iv = this . crypto . randomBytes ( this . iv_size ) . toString ( this . encodeas ) ,
17
- aad = this . _digest ( iv + this . secret , JSON . stringify ( plaintext ) ,
18
- this . hashing , this . encodeas ) ,
19
- ct = this . _encrypt ( this . secret , JSON . stringify ( plaintext ) ,
20
- this . algorithm , this . encodeas , iv , aad ) ,
21
- hmac = this . _digest ( this . secret , ct . ct , this . hashing , this . encodeas )
22
-
23
- let obj = JSON . stringify ( {
24
- hmac : hmac ,
17
+ aad = this . _digest (
18
+ iv + this . secret ,
19
+ JSON . stringify ( plaintext ) ,
20
+ this . hashing ,
21
+ this . encodeas
22
+ ) ,
23
+ ct = this . _encrypt (
24
+ this . secret ,
25
+ JSON . stringify ( plaintext ) ,
26
+ this . algorithm ,
27
+ this . encodeas ,
28
+ iv ,
29
+ aad
30
+ ) ,
31
+ hmac = this . _digest ( this . secret , ct . ct , this . hashing , this . encodeas )
32
+
33
+ const obj = JSON . stringify ( {
34
+ hmac,
25
35
ct : ct . ct ,
26
36
at : ct . at ,
27
- aad : aad ,
28
- iv : iv
37
+ aad,
38
+ iv,
29
39
} )
30
40
31
41
return obj
32
42
}
33
-
43
+
34
44
get ( ciphertext ) {
35
45
let ct , hmac , pt , sid , session
36
46
37
- if ( ciphertext )
47
+ if ( ciphertext ) {
38
48
try {
39
49
ct = JSON . parse ( ciphertext )
40
- } catch ( err ) {
50
+ } catch ( err ) {
41
51
ct = ciphertext
42
52
}
53
+ }
43
54
44
55
hmac = this . _digest ( this . secret , ct . ct , this . hashing , this . encodeas )
45
56
46
- if ( hmac != ct . hmac )
57
+ if ( hmac != ct . hmac ) {
47
58
throw 'Encrypted session was tampered with!'
59
+ }
48
60
49
- if ( ct . at )
61
+ if ( ct . at ) {
50
62
ct . at = Buffer . from ( ct . at )
63
+ }
51
64
52
- pt = this . _decrypt ( this . secret , ct . ct , this . algorithm , this . encodeas ,
53
- ct . iv , ct . at , ct . aad )
65
+ pt = this . _decrypt (
66
+ this . secret ,
67
+ ct . ct ,
68
+ this . algorithm ,
69
+ this . encodeas ,
70
+ ct . iv ,
71
+ ct . at ,
72
+ ct . aad
73
+ )
54
74
55
75
return pt
56
76
}
57
77
58
78
_digest ( key , obj , hashing , encodeas ) {
59
- let hmac = this . crypto . createHmac ( this . hashing , key )
79
+ const hmac = this . crypto . createHmac ( this . hashing , key )
60
80
hmac . setEncoding ( encodeas )
61
81
hmac . write ( obj )
62
82
hmac . end ( )
@@ -65,15 +85,17 @@ class Crypto {
65
85
66
86
_encrypt ( key , pt , algo , encodeas , iv , aad ) {
67
87
let cipher = this . crypto . createCipheriv ( algo , key , iv , {
68
- authTagLength : this . at_size
69
- } ) , ct , at
88
+ authTagLength : this . at_size ,
89
+ } ) ,
90
+ ct ,
91
+ at
70
92
71
93
if ( aad ) {
72
94
try {
73
95
cipher . setAAD ( Buffer . from ( aad ) , {
74
- plaintextLength : Buffer . byteLength ( pt )
96
+ plaintextLength : Buffer . byteLength ( pt ) ,
75
97
} )
76
- } catch ( err ) {
98
+ } catch ( err ) {
77
99
throw err
78
100
}
79
101
}
@@ -83,30 +105,31 @@ class Crypto {
83
105
84
106
try {
85
107
at = cipher . getAuthTag ( )
86
- } catch ( err ) {
108
+ } catch ( err ) {
87
109
throw err
88
110
}
89
111
90
- return ( at ) ? { 'ct' : ct , 'at' : at } : { 'ct' : ct }
112
+ return at ? { ct , at } : { ct}
91
113
}
92
114
93
115
_decrypt ( key , ct , algo , encodeas , iv , at , aad ) {
94
- let cipher = this . crypto . createDecipheriv ( algo , key , iv ) , pt
116
+ let cipher = this . crypto . createDecipheriv ( algo , key , iv ) ,
117
+ pt
95
118
96
119
if ( at ) {
97
120
try {
98
121
cipher . setAuthTag ( Buffer . from ( at ) )
99
- } catch ( err ) {
122
+ } catch ( err ) {
100
123
throw err
101
124
}
102
125
}
103
126
104
127
if ( aad ) {
105
128
try {
106
129
cipher . setAAD ( Buffer . from ( aad ) , {
107
- plaintextLength : Buffer . byteLength ( ct )
130
+ plaintextLength : Buffer . byteLength ( ct ) ,
108
131
} )
109
- } catch ( err ) {
132
+ } catch ( err ) {
110
133
throw err
111
134
}
112
135
}
@@ -130,4 +153,4 @@ class Crypto {
130
153
}
131
154
}
132
155
133
- module . exports = new Crypto
156
+ module . exports = new Crypto ( )
0 commit comments