forked from ethereumjs/ethereumjs-devp2p
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmac.ts
37 lines (32 loc) · 879 Bytes
/
mac.ts
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
import { createCipheriv } from 'crypto'
import createKeccakHash from 'keccak'
import { xor } from '../util'
export class MAC {
_hash: any
_secret: Buffer
constructor(secret: Buffer) {
this._hash = createKeccakHash('keccak256')
this._secret = secret
}
update(data: Buffer | string) {
this._hash.update(data)
}
updateHeader(data: Buffer | string) {
const aes = createCipheriv('aes-256-ecb', this._secret, '')
const encrypted = aes.update(this.digest())
this._hash.update(xor(encrypted, data))
}
updateBody(data: Buffer | string) {
this._hash.update(data)
const prev = this.digest()
const aes = createCipheriv('aes-256-ecb', this._secret, '')
const encrypted = aes.update(prev)
this._hash.update(xor(encrypted, prev))
}
digest() {
return this._hash
._clone()
.digest()
.slice(0, 16)
}
}