Decrypts a value produced by encrypt. Returns null on failure.
(encoded: string)
| 196 | |
| 197 | /** Decrypts a value produced by {@link encrypt}. Returns null on failure. */ |
| 198 | decrypt(encoded: string): string | null { |
| 199 | try { |
| 200 | const buf = Buffer.from(encoded, "base64url"); |
| 201 | const iv = buf.subarray(0, 12); |
| 202 | const tag = buf.subarray(12, 28); |
| 203 | const ciphertext = buf.subarray(28); |
| 204 | const decipher = createDecipheriv("aes-256-gcm", this.key, iv); |
| 205 | decipher.setAuthTag(tag); |
| 206 | return decipher.update(ciphertext).toString("utf8") + decipher.final("utf8"); |
| 207 | } catch { |
| 208 | return null; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | // ── Internals ───────────────────────────────────────────────────────────── |
| 213 |
no test coverage detected