BlueWallet/blue_modules/encryption.ts

24 lines
888 B
TypeScript
Raw Normal View History

2024-04-09 18:14:14 +02:00
import AES from 'crypto-js/aes';
import Utf8 from 'crypto-js/enc-utf8';
2023-10-24 02:57:41 +02:00
export function encrypt(data: string, password: string): string {
if (data.length < 10) throw new Error('data length cant be < 10');
2024-04-09 18:14:14 +02:00
const ciphertext = AES.encrypt(data, password);
2023-10-24 02:57:41 +02:00
return ciphertext.toString();
}
export function decrypt(data: string, password: string): string | false {
2024-04-09 18:14:14 +02:00
const bytes = AES.decrypt(data, password);
2023-10-24 02:57:41 +02:00
let str: string | false = false;
try {
2024-04-09 18:14:14 +02:00
str = bytes.toString(Utf8);
2023-10-24 02:57:41 +02:00
} catch (e) {}
// For some reason, sometimes decrypt would succeed with an incorrect password and return random characters.
// In this TypeScript version, we are not allowing the encryption of data that is shorter than
// 10 characters. If the decrypted data is less than 10 characters, we assume that the decrypt actually failed.
if (str && str.length < 10) return false;
return str;
}