Work to improve encryption and decryption
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
let log_lvl = 1; // 0 = no logging, 1 = some logging, 2 = all logging
|
||||
|
||||
async function generate_iv() {
|
||||
const data = new Uint8Array(16);
|
||||
crypto.getRandomValues(data);
|
||||
@@ -6,7 +8,8 @@ async function generate_iv() {
|
||||
|
||||
|
||||
export let encrypt_content = async function encrypt_content(
|
||||
content: string, keyData: string
|
||||
content: string,
|
||||
keyData: string
|
||||
) {
|
||||
const iv = await generate_iv();
|
||||
const keyBytes = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(keyData));
|
||||
@@ -17,10 +20,36 @@ export let encrypt_content = async function encrypt_content(
|
||||
return { base64, iv };
|
||||
}
|
||||
|
||||
export let combine_iv_and_base64 = function combine_iv_and_base64(
|
||||
base64: string,
|
||||
iv: Uint8Array
|
||||
) {
|
||||
console.log(`IV: ${iv}; Encrypted: ${base64}`);
|
||||
|
||||
// Combine the IV and encrypted content
|
||||
const combined = Array.from(iv).map(byte => byte.toString(16).padStart(2, '0')).join('') + ':' + base64;
|
||||
console.log('Combined Data v1:', combined);
|
||||
|
||||
// const ivBase64 = btoa(String.fromCharCode(...iv));
|
||||
// const combined = `${ivBase64}:${base64}`;
|
||||
// console.log('Combined IV and Base64 v2:', combined);
|
||||
return combined;
|
||||
}
|
||||
|
||||
export let encrypt_wrapper = async function encrypt_wrapper(
|
||||
content: string,
|
||||
keyData: string
|
||||
) {
|
||||
const { base64, iv } = await encrypt_content(content, keyData);
|
||||
const combined = combine_iv_and_base64(base64, iv);
|
||||
return combined;
|
||||
}
|
||||
|
||||
// This does not handle errors (invalid key/password) well.
|
||||
export let decrypt_content = async function decrypt_content(
|
||||
base64Content: string, iv: Uint8Array, keyData: string
|
||||
base64Content: string,
|
||||
iv: Uint8Array,
|
||||
keyData: string
|
||||
) {
|
||||
const keyBytes = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(keyData));
|
||||
const key = await crypto.subtle.importKey('raw', keyBytes, { name: 'AES-CBC' }, false, ['decrypt']);
|
||||
@@ -29,4 +58,34 @@ export let decrypt_content = async function decrypt_content(
|
||||
const decodedContent = new TextDecoder().decode(decryptedContent);
|
||||
// console.log('Decrypted Content:', decodedContent);
|
||||
return decodedContent;
|
||||
}
|
||||
|
||||
export let split_iv_and_base64 = function split_iv_and_base64(
|
||||
combined: string
|
||||
) {
|
||||
let [iv_hex, encrypted_base64_string] = combined.split(':');
|
||||
let base64 = encrypted_base64_string
|
||||
let iv = new Uint8Array(iv_hex.match(/.{1,2}/g).map(byte => parseInt(byte, 16)));
|
||||
if (log_lvl) {
|
||||
console.log(`IV: ${iv}; Encrypted: ${base64}`);
|
||||
}
|
||||
|
||||
// const [ivBase64, base64] = combined.split(':');
|
||||
// const iv = Uint8Array.from(atob(ivBase64), c => c.charCodeAt(0));
|
||||
// if (log_lvl) {
|
||||
// console.log(`IV: ${iv}; Encrypted: ${base64}`);
|
||||
// }
|
||||
return { iv, base64 };
|
||||
}
|
||||
|
||||
export let decrypt_wrapper = async function decrypt_wrapper(
|
||||
combined: string,
|
||||
keyData: string
|
||||
) {
|
||||
const { iv, base64 } = split_iv_and_base64(combined);
|
||||
const decrypted = await decrypt_content(base64, iv, keyData);
|
||||
if (log_lvl) {
|
||||
console.log(`IV: ${iv}; Decrypted:`, decrypted);
|
||||
}
|
||||
return decrypted;
|
||||
}
|
||||
Reference in New Issue
Block a user