-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencryption.ts
45 lines (40 loc) · 1.23 KB
/
encryption.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import AES from "crypto-js/aes";
import SHA512 from "crypto-js/sha512";
import core from "crypto-js/core";
export const hash512 = (message) => SHA512(message).toString();
export const fakeNumString = (length = 5) => {
const inner = (length) => {
let temp = "";
for (let x = 0; x <= length; x++) {
temp += Math.floor(Math.random() * 10);
}
return temp;
};
return inner(length);
};
export const encryptAES = (obj: any, key: string) => {
try {
return AES.encrypt(JSON.stringify(obj), key).toString();
} catch (error) {
return -1;
}
};
export const decryptAES = (encrypted: string, key: string) => {
try {
const decrypted = AES.decrypt(encrypted, key);
return JSON.parse(decrypted.toString(core.enc.Utf8));
} catch (error) {
return -1;
}
};
if (require.main === module) {
// console.log(generateHash("KAi"));
// message = 'zenosama';
// passphase = 'ZNEOSAMAEAJSKLHJLKALJLKAKLLKAJAJKLJAKLK';
// encrypted = messageEncrypt(message, passphase);
// decrypted = messageDecrypt(encrypted, passphase);
// console.log('Encrypted :' + encrypted);
// console.log('Size of Encrypted :' + sizeof(encrypted));
// console.log('Decrypted: ' + decrypted);
// console.log(sizeof('U2FsdGVkX19+SiLlnUMiuz+mNakFA6PDrrHWmxldZSA='));
}