(data)
| 49 | } |
| 50 | |
| 51 | decrypt(data) { |
| 52 | if (data.length < 20) throw new Error('Data too short'); |
| 53 | const counter = data.readUInt32LE(0); |
| 54 | if (counter <= this.decryptCounter) throw new Error('Replay attack'); |
| 55 | this.decryptCounter = counter; |
| 56 | |
| 57 | const ciphertext = data.slice(4, data.length - 16); |
| 58 | const tag = data.slice(data.length - 16); |
| 59 | const iv = this._makeIV(counter); |
| 60 | |
| 61 | const decipher = crypto.createDecipheriv('aes-128-gcm', this.key, iv); |
| 62 | decipher.setAuthTag(tag); |
| 63 | return Buffer.concat([decipher.update(ciphertext), decipher.final()]); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | // ==================== 压缩/解压 ==================== |
no test coverage detected