/ src / rlpx / mac.js
mac.js
 1  const { createCipheriv } = require('crypto')
 2  const createKeccakHash = require('keccak')
 3  const { xor } = require('../util')
 4  
 5  class MAC {
 6    constructor (secret) {
 7      this._hash = createKeccakHash('keccak256')
 8      this._secret = secret
 9    }
10  
11    update (data) {
12      this._hash.update(data)
13    }
14  
15    updateHeader (data) {
16      const aes = createCipheriv('aes-256-ecb', this._secret, '')
17      const encrypted = aes.update(this.digest())
18      this._hash.update(xor(encrypted, data))
19    }
20  
21    updateBody (data) {
22      this._hash.update(data)
23      const prev = this.digest()
24      const aes = createCipheriv('aes-256-ecb', this._secret, '')
25      const encrypted = aes.update(prev)
26      this._hash.update(xor(encrypted, prev))
27    }
28  
29    digest () {
30      return this._hash._clone().digest().slice(0, 16)
31    }
32  }
33  
34  module.exports = MAC