Files
web/apps/scandic-web/utils/aes.ts
Anton Gunnarsson 5da3e457cb Merged in chore/migrate-from-next-lint (pull request #3263)
chore: Migrate from next lint to eslint

* Migrate scandic-web

* Migrate partner-sas

* Enable any rule in partner-sas


Approved-by: Joakim Jäderberg
Approved-by: Linus Flood
2025-12-02 10:08:56 +00:00

38 lines
1007 B
TypeScript

function base64ToUint8Array(base64String: string) {
const binaryString = atob(base64String)
const byteArray = new Uint8Array(binaryString.length)
for (let i = 0; i < binaryString.length; i++) {
byteArray[i] = binaryString.charCodeAt(i)
}
return byteArray
}
function uint8ArrayToUtf8(uint8Array: Uint8Array) {
return new TextDecoder().decode(uint8Array)
}
export async function decryptData(
keyBase64: string,
ivBase64: string,
encryptedDataBase64: string
): Promise<string> {
const keyBuffer = await crypto.subtle.importKey(
"raw",
base64ToUint8Array(keyBase64),
"AES-CBC",
false,
["decrypt"]
)
const encryptedDataBuffer = base64ToUint8Array(encryptedDataBase64)
const ivBuffer = base64ToUint8Array(ivBase64)
const decryptedDataBuffer = await crypto.subtle.decrypt(
{ name: "AES-CBC", iv: ivBuffer },
keyBuffer,
encryptedDataBuffer
)
const decryptedData = uint8ArrayToUtf8(new Uint8Array(decryptedDataBuffer))
return decryptedData
}