(data)
| 35 | } |
| 36 | |
| 37 | encrypt(data) { |
| 38 | const counter = this.encryptCounter++; |
| 39 | const iv = this._makeIV(counter); |
| 40 | const cipher = crypto.createCipheriv('aes-128-gcm', this.key, iv); |
| 41 | const encrypted = Buffer.concat([cipher.update(data), cipher.final()]); |
| 42 | const tag = cipher.getAuthTag(); |
| 43 | |
| 44 | const result = Buffer.alloc(4 + encrypted.length + tag.length); |
| 45 | result.writeUInt32LE(counter, 0); |
| 46 | encrypted.copy(result, 4); |
| 47 | tag.copy(result, 4 + encrypted.length); |
| 48 | return result; |
| 49 | } |
| 50 | |
| 51 | decrypt(data) { |
| 52 | if (data.length < 20) throw new Error('Data too short'); |
no test coverage detected