From dc7ee5b6ea10fcf45eb4761ff694343fe7e3630e Mon Sep 17 00:00:00 2001 From: koalasat Date: Wed, 21 May 2025 15:27:20 +0200 Subject: [PATCH] Fix openpgp --- frontend/src/index.js | 50 ++++++++++++++++++++++++++++++++++++++- frontend/src/pgp/index.ts | 8 ++++--- 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/frontend/src/index.js b/frontend/src/index.js index cf7c14e4..fb10ab9a 100644 --- a/frontend/src/index.js +++ b/frontend/src/index.js @@ -15,7 +15,7 @@ const getWebCrypto = () => { } else if (data instanceof ArrayBuffer) { message = new TextDecoder().decode(new Uint8Array(data)); } else { - message = data; + message = data; // Assume it's a string } const hash = CryptoJS.SHA256(message).toString(); @@ -31,6 +31,54 @@ const getWebCrypto = () => { } }); }, + + encrypt: (algorithm, key, data) => { + return new Promise((resolve, reject) => { + if (algorithm === 'AES-CBC') { + const iv = CryptoJS.lib.WordArray.random(128 / 8); // Generate a random IV + const encrypted = CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse(data), key, { + iv: iv, + mode: CryptoJS.mode.CBC, + padding: CryptoJS.pad.Pkcs7, + }); + + resolve({ + ciphertext: encrypted.toString(), + iv: iv.toString(), + }); + } else { + reject(new Error('Algorithm not supported')); + } + }); + }, + + decrypt: (algorithm, key, data) => { + return new Promise((resolve, reject) => { + if (algorithm === 'AES-CBC') { + const { ciphertext, iv } = data; + const decrypted = CryptoJS.AES.decrypt(ciphertext, key, { + iv: CryptoJS.enc.Hex.parse(iv), + mode: CryptoJS.mode.CBC, + padding: CryptoJS.pad.Pkcs7, + }); + + resolve(decrypted.toString(CryptoJS.enc.Utf8)); + } else { + reject(new Error('Algorithm not supported')); + } + }); + }, + + generateKey: (algorithm, extractable, keyUsages) => { + return new Promise((resolve, reject) => { + if (algorithm.name === 'AES-CBC') { + const key = CryptoJS.lib.WordArray.random(256 / 8); // Generate a random AES key + resolve(key); + } else { + reject(new Error('Algorithm not supported')); + } + }); + }, }, }; }; diff --git a/frontend/src/pgp/index.ts b/frontend/src/pgp/index.ts index 40f36887..3c442329 100644 --- a/frontend/src/pgp/index.ts +++ b/frontend/src/pgp/index.ts @@ -19,14 +19,16 @@ export interface generatedKeyPair { } export async function genKey(highEntropyToken: string): Promise { - const d = new Date(); + const date = new Date(); + date.setDate(date.getDate() - 1); // One day of offset. Helps reducing errors due to client's system time being in the future. + const keyPair = await generateKey({ type: 'ecc', // Type of the key, defaults to ECC - curve: 'curve25519', // ECC curve name, defaults to curve25519 + curve: 'curve25519Legacy', // ECC curve name, defaults to curve25519 userIDs: [{ name: 'RoboSats ID ' + sha256(sha256(highEntropyToken)) }], // Ideally it would be the avatar nickname, but the nickname is generated only after submission. The second SHA256 can be converted into the Nickname using nick_generator package. passphrase: highEntropyToken, format: 'armored', - date: d.setDate(d.getDate() - 1), // One day of offset. Helps reducing errors due to client's system time being in the future. + date, }); return {