decide.nolog.cz/app/utils/encryption.ts
Jeldrik Hanschke f0cff27e99
Convert to TypeScript (#713)
* setup typescript

* covert to TypeScript
2023-10-29 19:16:33 +01:00

29 lines
807 B
TypeScript

import { decrypt as sjclDecrypt, encrypt as sjclEncrypt } from 'sjcl';
function decrypt(encryptedValue: string, passphrase: string): unknown {
return JSON.parse(sjclDecrypt(passphrase, encryptedValue));
}
function encrypt(plainValue: unknown, passphrase: string) {
return sjclEncrypt(passphrase, JSON.stringify(plainValue));
}
function generatePassphrase(): string {
const length = 40;
const possible =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const randomArray = new Uint32Array(length);
window.crypto.getRandomValues(randomArray);
let passphrase = '';
for (let j = length; j--; ) {
passphrase += possible.charAt(
Math.floor(randomArray[j]! % possible.length),
);
}
return passphrase;
}
export { decrypt, encrypt, generatePassphrase };