( ciphertext: string, key: string )
| 50 | * Returns the original compressed base64url string. |
| 51 | */ |
| 52 | export async function decrypt( |
| 53 | ciphertext: string, |
| 54 | key: string |
| 55 | ): Promise<string> { |
| 56 | const combined = base64urlToBytes(ciphertext); |
| 57 | const rawKey = base64urlToBytes(key); |
| 58 | |
| 59 | const iv = combined.slice(0, 12); |
| 60 | const encrypted = combined.slice(12); |
| 61 | |
| 62 | const cryptoKey = await crypto.subtle.importKey( |
| 63 | 'raw', |
| 64 | rawKey.buffer as ArrayBuffer, |
| 65 | { name: 'AES-GCM', length: 256 }, |
| 66 | false, |
| 67 | ['decrypt'] |
| 68 | ); |
| 69 | |
| 70 | const decrypted = await crypto.subtle.decrypt( |
| 71 | { name: 'AES-GCM', iv }, |
| 72 | cryptoKey, |
| 73 | encrypted |
| 74 | ); |
| 75 | |
| 76 | return new TextDecoder().decode(decrypted); |
| 77 | } |
| 78 | |
| 79 | // --- Helpers --- |
| 80 |
no test coverage detected