( encryptedBase64: string, password: string, )
| 83 | * Returns the parsed JSON data. |
| 84 | */ |
| 85 | export async function decryptData( |
| 86 | encryptedBase64: string, |
| 87 | password: string, |
| 88 | ): Promise<any> { |
| 89 | try { |
| 90 | const binary = atob(encryptedBase64); |
| 91 | const combined = new Uint8Array(binary.length); |
| 92 | for (let i = 0; i < binary.length; i++) { |
| 93 | combined[i] = binary.charCodeAt(i); |
| 94 | } |
| 95 | |
| 96 | const salt = combined.subarray(0, SALT_LENGTH); |
| 97 | const iv = combined.subarray(SALT_LENGTH, SALT_LENGTH + IV_LENGTH); |
| 98 | const encryptedBytes = combined.subarray(SALT_LENGTH + IV_LENGTH); |
| 99 | |
| 100 | const passwordKey = await getPasswordKey(password); |
| 101 | const aesKey = await deriveKey(passwordKey, salt); |
| 102 | |
| 103 | const decryptedContent = await crypto.subtle.decrypt( |
| 104 | { name: "AES-GCM", iv: iv }, |
| 105 | aesKey, |
| 106 | encryptedBytes, |
| 107 | ); |
| 108 | |
| 109 | const dec = new TextDecoder(); |
| 110 | const jsonStr = dec.decode(decryptedContent); |
| 111 | return JSON.parse(jsonStr); |
| 112 | } catch (e) { |
| 113 | throw new Error("Decryption failed. Incorrect password or corrupted data."); |
| 114 | } |
| 115 | } |
no test coverage detected