mirror of
https://github.com/RoboSats/robosats.git
synced 2025-09-05 12:24:10 +00:00
Fix openpgp
This commit is contained in:
@ -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'));
|
||||
}
|
||||
});
|
||||
},
|
||||
},
|
||||
};
|
||||
};
|
||||
|
@ -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 {
|
||||
|
Reference in New Issue
Block a user