From 89e8a3b80e427b2a4791e9d69768f2e27065199c Mon Sep 17 00:00:00 2001 From: Kavin <20838718+FireMasterK@users.noreply.github.com> Date: Thu, 9 Feb 2023 01:05:54 +0000 Subject: [PATCH] Add key generation and base64 encoding and decoding utils. --- src/utils/encryptionUtils.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/utils/encryptionUtils.js b/src/utils/encryptionUtils.js index 20ebd3a5..a75cbb41 100644 --- a/src/utils/encryptionUtils.js +++ b/src/utils/encryptionUtils.js @@ -17,3 +17,20 @@ export async function decryptAESGCM(ciphertextArray, key) { return decrypted; } + +export async function generateKey() { + const algorithm = { name: "AES-GCM", length: 256 }; + const key = await crypto.subtle.generateKey(algorithm, true, ["encrypt", "decrypt"]); + + return await crypto.subtle.exportKey("raw", key); +} + +export function encodeArrayToBase64(array) { + const chars = String.fromCharCode.apply(null, array); + return btoa(chars); +} + +export function decodeBase64ToArray(base64) { + const chars = atob(base64); + return new Uint8Array(chars.split("").map(c => c.charCodeAt(0))); +}