Skip to content

Commit eb23b1e

Browse files
committedJun 15, 2019
linting fix
1 parent 66194c7 commit eb23b1e

File tree

3 files changed

+216
-135
lines changed

3 files changed

+216
-135
lines changed
 

‎package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
},
4040
"xo": {
4141
"space": 2,
42-
"semicolon": false
42+
"semicolon": false,
43+
"prettier": true
4344
}
4445
}

‎src/crypto.js

+52-29
Original file line numberDiff line numberDiff line change
@@ -14,49 +14,69 @@ class Crypto {
1414

1515
set(plaintext) {
1616
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,
2535
ct: ct.ct,
2636
at: ct.at,
27-
aad: aad,
28-
iv: iv
37+
aad,
38+
iv,
2939
})
3040

3141
return obj
3242
}
33-
43+
3444
get(ciphertext) {
3545
let ct, hmac, pt, sid, session
3646

37-
if (ciphertext)
47+
if (ciphertext) {
3848
try {
3949
ct = JSON.parse(ciphertext)
40-
} catch(err) {
50+
} catch (err) {
4151
ct = ciphertext
4252
}
53+
}
4354

4455
hmac = this._digest(this.secret, ct.ct, this.hashing, this.encodeas)
4556

46-
if (hmac != ct.hmac)
57+
if (hmac != ct.hmac) {
4758
throw 'Encrypted session was tampered with!'
59+
}
4860

49-
if (ct.at)
61+
if (ct.at) {
5062
ct.at = Buffer.from(ct.at)
63+
}
5164

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+
)
5474

5575
return pt
5676
}
5777

5878
_digest(key, obj, hashing, encodeas) {
59-
let hmac = this.crypto.createHmac(this.hashing, key)
79+
const hmac = this.crypto.createHmac(this.hashing, key)
6080
hmac.setEncoding(encodeas)
6181
hmac.write(obj)
6282
hmac.end()
@@ -65,15 +85,17 @@ class Crypto {
6585

6686
_encrypt(key, pt, algo, encodeas, iv, aad) {
6787
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
7092

7193
if (aad) {
7294
try {
7395
cipher.setAAD(Buffer.from(aad), {
74-
plaintextLength: Buffer.byteLength(pt)
96+
plaintextLength: Buffer.byteLength(pt),
7597
})
76-
} catch(err) {
98+
} catch (err) {
7799
throw err
78100
}
79101
}
@@ -83,30 +105,31 @@ class Crypto {
83105

84106
try {
85107
at = cipher.getAuthTag()
86-
} catch(err) {
108+
} catch (err) {
87109
throw err
88110
}
89111

90-
return (at) ? {'ct': ct, 'at': at} : {'ct': ct}
112+
return at ? {ct, at} : {ct}
91113
}
92114

93115
_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
95118

96119
if (at) {
97120
try {
98121
cipher.setAuthTag(Buffer.from(at))
99-
} catch(err) {
122+
} catch (err) {
100123
throw err
101124
}
102125
}
103126

104127
if (aad) {
105128
try {
106129
cipher.setAAD(Buffer.from(aad), {
107-
plaintextLength: Buffer.byteLength(ct)
130+
plaintextLength: Buffer.byteLength(ct),
108131
})
109-
} catch(err) {
132+
} catch (err) {
110133
throw err
111134
}
112135
}
@@ -130,4 +153,4 @@ class Crypto {
130153
}
131154
}
132155

133-
module.exports = new Crypto
156+
module.exports = new Crypto()

0 commit comments

Comments
 (0)
Please sign in to comment.