crypto.js
3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
'use strict'
class Crypto {
init(options) {
this.crypto = require('crypto')
this.algorithm = options.algorithm || 'aes-256-gcm'
this.hashing = options.hashing || 'sha512'
this.encodeas = options.encodeas || 'hex'
this.iv_size = options.iv_size || 16
this.at_size = options.at_size || 16
this.key_size = options.key_size || 32
this.secret = this._deriveKey(options.secret) || false
}
set(plaintext) {
const iv = this.crypto.randomBytes(this.iv_size).toString(this.encodeas)
const aad = this._digest(
iv + this.secret,
JSON.stringify(plaintext),
this.hashing,
this.encodeas
)
const ct = this._encrypt(
this.secret,
JSON.stringify(plaintext),
this.algorithm,
this.encodeas,
iv,
aad
)
const hmac = this._digest(this.secret, ct.ct, this.hashing, this.encodeas)
const obj = JSON.stringify({
hmac,
ct: ct.ct,
at: ct.at,
aad,
iv,
})
return obj
}
get(ciphertext) {
let ct
if (ciphertext) {
try {
ct = JSON.parse(ciphertext)
} catch (err) {
ct = ciphertext
}
}
const hmac = this._digest(this.secret, ct.ct, this.hashing, this.encodeas)
if (hmac !== ct.hmac) {
throw new Error('Encrypted session was tampered with!')
}
if (ct.at) {
ct.at = Buffer.from(ct.at)
}
const pt = this._decrypt(
this.secret,
ct.ct,
this.algorithm,
this.encodeas,
ct.iv,
ct.at,
ct.aad
)
return pt
}
_digest(key, obj, hashing, encodeas) {
const hmac = this.crypto.createHmac(this.hashing, key)
hmac.setEncoding(encodeas)
hmac.write(obj)
hmac.end()
return hmac.read().toString(encodeas)
}
_encrypt(key, pt, algo, encodeas, iv, aad) {
const cipher = this.crypto.createCipheriv(algo, key, iv, {
authTagLength: this.at_size,
})
let ct
let at
if (aad) {
try {
cipher.setAAD(Buffer.from(aad), {
plaintextLength: Buffer.byteLength(pt),
})
} catch (err) {
throw err
}
}
ct = cipher.update(pt, 'utf8', encodeas)
ct += cipher.final(encodeas)
try {
at = cipher.getAuthTag()
} catch (err) {
throw err
}
return at ? { ct, at } : { ct }
}
_decrypt(key, ct, algo, encodeas, iv, at, aad) {
const cipher = this.crypto.createDecipheriv(algo, key, iv)
let pt
if (at) {
try {
cipher.setAuthTag(Buffer.from(at))
} catch (err) {
throw err
}
}
if (aad) {
try {
cipher.setAAD(Buffer.from(aad), {
plaintextLength: Buffer.byteLength(ct),
})
} catch (err) {
throw err
}
}
pt = cipher.update(ct, encodeas, 'utf8')
pt += cipher.final('utf8')
return pt
}
_deriveKey(secret) {
const hash = this.crypto.createHash(this.hashing)
hash.update(secret)
const salt = hash.digest(this.encodeas).substr(0, 16)
const key = this.crypto.pbkdf2Sync(secret, salt, 10000, 64, this.hashing)
return key.toString(this.encodeas).substr(0, this.key_size)
}
}
module.exports = new Crypto()