-
Notifications
You must be signed in to change notification settings - Fork 3
/
bso.js
59 lines (49 loc) · 1.49 KB
/
bso.js
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
const crypto = require('crypto')
/**
* @param {import('./types').ParsedKeyBundle} keyBundle
* @param {import('./types').BSO} bso
* @returns {Object}
*/
function decryptBSO (keyBundle, bso) {
const payload = JSON.parse(bso.payload)
const hmac = crypto.createHmac('sha256', keyBundle.hmacKey)
.update(payload.ciphertext)
.digest('hex')
if (hmac !== payload.hmac) {
throw new Error('HMAC mismatch')
}
const iv = Buffer.from(payload.IV, 'base64')
const decipher = crypto.createDecipheriv('aes-256-cbc', keyBundle.encryptionKey, iv)
const plaintext = decipher.update(payload.ciphertext, 'base64', 'utf8') + decipher.final('utf8')
const result = JSON.parse(plaintext)
if (result.id !== bso.id) {
throw new Error('Record ID mismatch')
}
return result
}
/**
* @param {import('./types').ParsedKeyBundle} keyBundle
* @param {Object} payload
* @returns {import('./types').BSO}
*/
function encryptBSO (keyBundle, payload) {
const plaintext = JSON.stringify(payload)
const iv = crypto.randomBytes(16)
const cipher = crypto.createCipheriv('aes-256-cbc', keyBundle.encryptionKey, iv)
const ciphertext = cipher.update(plaintext, 'utf8', 'base64') + cipher.final('base64')
const hmac = crypto.createHmac('sha256', keyBundle.hmacKey)
.update(ciphertext)
.digest('hex')
return {
id: payload.id,
payload: JSON.stringify({
ciphertext: ciphertext,
IV: iv.toString('base64'),
hmac
})
}
}
module.exports = {
decryptBSO,
encryptBSO
}