( input: string, key: string, algorithmName = 'AES-CBC', algorithmLength = 256, subtle = globalThis.crypto.subtle, )
| 4 | Uint8Array.from(atob(b), (c) => c.charCodeAt(0)); |
| 5 | |
| 6 | export const decrypt = async ( |
| 7 | input: string, |
| 8 | key: string, |
| 9 | algorithmName = 'AES-CBC', |
| 10 | algorithmLength = 256, |
| 11 | subtle = globalThis.crypto.subtle, |
| 12 | ): Promise<string> => { |
| 13 | if (!subtle || (isDevelopment && !isProductionAPI)) { |
| 14 | return input; |
| 15 | } |
| 16 | const keyObject = await subtle.importKey( |
| 17 | 'raw', |
| 18 | new TextEncoder().encode(key), |
| 19 | { name: algorithmName, length: algorithmLength }, |
| 20 | true, |
| 21 | ['encrypt', 'decrypt'], |
| 22 | ); |
| 23 | const [iv, cipherText] = input.split(':'); |
| 24 | const plainTextBuffer = await subtle.decrypt( |
| 25 | { name: algorithmName, iv: base64ToBuf(iv) }, |
| 26 | keyObject, |
| 27 | base64ToBuf(cipherText), |
| 28 | ); |
| 29 | |
| 30 | return new TextDecoder().decode(plainTextBuffer); |
| 31 | }; |
no test coverage detected