Fix openpgp

This commit is contained in:
koalasat
2025-05-21 15:27:20 +02:00
parent 44e88bb891
commit dc7ee5b6ea
2 changed files with 54 additions and 4 deletions

View File

@ -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'));
}
});
},
},
};
};

View File

@ -19,14 +19,16 @@ export interface generatedKeyPair {
}
export async function genKey(highEntropyToken: string): Promise<generatedKeyPair> {
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 {