1 | /** |
||
2 | * @todo CryptoJS |
||
3 | * @package https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.js |
||
4 | */ |
||
5 | var salt = "salt"; //salt |
||
6 | var iv = "1111111111111111"; //pass salt minimum length 12 chars |
||
7 | var iterations = "999"; //iterations |
||
8 | |||
9 | /** |
||
10 | * Get key |
||
11 | * @param {string} passphrase |
||
12 | * @param {string} salt |
||
13 | */ |
||
14 | function getKey(passphrase, salt) { |
||
15 | if (typeof CryptoJS == "undefined") return; |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
![]() |
|||
16 | var key = CryptoJS.PBKDF2(passphrase, salt, { |
||
17 | hasher: CryptoJS.algo.SHA256, |
||
18 | keySize: 64 / 8, |
||
19 | iterations: iterations, |
||
20 | }); |
||
21 | return key; |
||
22 | } |
||
23 | /** |
||
24 | * Encrypt function |
||
25 | * @param {string} passphrase |
||
26 | * @param {string} plainText |
||
27 | */ |
||
28 | function userJSEncrypt(passphrase, plainText) { |
||
29 | if (typeof CryptoJS == "undefined") return; |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
30 | var key = getKey(passphrase, salt); |
||
31 | var encrypted = CryptoJS.AES.encrypt(plainText, key, { |
||
32 | iv: CryptoJS.enc.Utf8.parse(iv), |
||
33 | }); |
||
34 | |||
35 | return encrypted.ciphertext.toString(CryptoJS.enc.Base64); |
||
36 | } |
||
37 | /** |
||
38 | * Decrypt function |
||
39 | * @param {string} passphrase |
||
40 | * @param {string} encryptedText |
||
41 | */ |
||
42 | function userJSDecrypt(passphrase, encryptedText) { |
||
43 | if (typeof CryptoJS == "undefined") return; |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
44 | var key = getKey(passphrase, salt); |
||
45 | var decrypted = CryptoJS.AES.decrypt(encryptedText, key, { |
||
46 | iv: CryptoJS.enc.Utf8.parse(iv), |
||
47 | }); |
||
48 | return decrypted.toString(CryptoJS.enc.Utf8); |
||
49 | } |
||
50 | |||
51 | // another |
||
52 | /*var salt = 'salt'; |
||
53 | var iv = '1111111111111111'; |
||
54 | */ |
||
55 | var iterations = "999"; |
||
0 ignored issues
–
show
Comprehensibility
Naming
Best Practice
introduced
by
The variable
iterations already seems to be declared on line 7 . Consider using another variable name or omitting the var keyword.
This check looks for variables that are declared in multiple lines. There may be several reasons for this. In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs. If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared. ![]() |
|||
56 | /** |
||
57 | * Crypto get key |
||
58 | * @param {String} passphrase |
||
59 | * @param {String} salt |
||
60 | */ |
||
61 | function CryptoK(passphrase, salt) { |
||
62 | var key = CryptoJS.PBKDF2(passphrase, salt, { |
||
63 | hasher: CryptoJS.algo.SHA256, |
||
64 | keySize: 64 / 8, |
||
65 | iterations: iterations, |
||
66 | }); |
||
67 | return key; |
||
68 | } |
||
69 | /** |
||
70 | * Crypto encrypt |
||
71 | * @param {String} passphrase |
||
72 | * @param {String} plainText |
||
73 | * @param {String} salt |
||
74 | * @param {String} iv |
||
75 | */ |
||
76 | function CryptoE(passphrase, plainText, salt, iv) { |
||
77 | var key = CryptoK(passphrase, salt, iterations); |
||
0 ignored issues
–
show
|
|||
78 | var encrypted = CryptoJS.AES.encrypt(plainText, key, { |
||
79 | iv: CryptoJS.enc.Utf8.parse(iv), |
||
80 | }); |
||
81 | |||
82 | return encrypted.ciphertext.toString(CryptoJS.enc.Base64); |
||
83 | } |
||
84 | /** |
||
85 | * Crypto decrypt |
||
86 | * @param {String} passphrase |
||
87 | * @param {String} encryptedText |
||
88 | * @param {String} salt |
||
89 | * @param {String} iv |
||
90 | */ |
||
91 | function CryptoD(passphrase, encryptedText, salt, iv) { |
||
92 | var key = CryptoK(passphrase, salt); |
||
93 | var decrypted = CryptoJS.AES.decrypt(encryptedText, key, { |
||
94 | iv: CryptoJS.enc.Utf8.parse(iv), |
||
95 | }); |
||
96 | return decrypted.toString(CryptoJS.enc.Utf8); |
||
97 | } |
||
98 |