-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaead.mjs
More file actions
73 lines (62 loc) · 1.53 KB
/
Copy pathaead.mjs
File metadata and controls
73 lines (62 loc) · 1.53 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
import { createDecipheriv, createCipheriv } from 'node:crypto'
function increase(nonce) {
let i = 0
do {
nonce[i]++
} while (nonce[i] === 0 && ++i < nonce.length)
}
export class AEAD {
static keySizeMap = {
'aes-256-gcm': 32,
'chacha20-poly1305': 32
}
static saltSizeMap = {
'aes-256-gcm': 32,
'chacha20-poly1305': 32
}
static nonceSizeMap = {
'aes-256-gcm': 12,
'chacha20-poly1305': 12
}
static tagSizeMap = {
'aes-256-gcm': 16,
'chacha20-poly1305': 16
}
static optionsMap = {
'aes-256-gcm': {},
'chacha20-poly1305': { authTagLength: 16 }
}
static getSize(method) {
const keySize = this.keySizeMap[method]
const saltSize = this.saltSizeMap[method]
const tagSize = this.tagSizeMap[method]
return { keySize, saltSize, tagSize }
}
constructor(algorithm, key) {
this.algorithm = algorithm
this.key = key
this.nonce = Buffer.alloc(AEAD.nonceSizeMap[algorithm])
this.options = AEAD.optionsMap[algorithm]
}
decrypt(c, tag) {
const d = createDecipheriv(this.algorithm, this.key, this.nonce, this.options)
const m = []
m.push(d.setAuthTag(tag).update(c))
try {
m.push(d.final())
increase(this.nonce)
return Buffer.concat(m)
} catch (e) {
return null
}
}
encrypt(m) {
const e = createCipheriv(this.algorithm, this.key, this.nonce, this.options)
const c = []
c.push(e.update(m))
c.push(e.final())
c.push(e.getAuthTag())
increase(this.nonce)
return Buffer.concat(c)
}
}