11427 |
nelberth |
1 |
/**
|
|
|
2 |
* AES JSON formatter for CryptoJS
|
|
|
3 |
* @link https://github.com/brainfoolong/cryptojs-aes-php
|
|
|
4 |
* @version 2.1.1
|
|
|
5 |
*/
|
|
|
6 |
import CryptoJS from "crypto-js";
|
|
|
7 |
|
|
|
8 |
|
|
|
9 |
var CryptoJSAesJson = {
|
|
|
10 |
/**
|
|
|
11 |
* Encrypt any value
|
|
|
12 |
* @param {*} value
|
|
|
13 |
* @param {string} password
|
|
|
14 |
* @return {string}
|
|
|
15 |
*/
|
|
|
16 |
'encrypt': function (value, password) {
|
|
|
17 |
return CryptoJS.AES.encrypt(JSON.stringify(value), password, { format: CryptoJSAesJson }).toString()
|
|
|
18 |
},
|
|
|
19 |
/**
|
|
|
20 |
* Decrypt a previously encrypted value
|
|
|
21 |
* @param {string} jsonStr
|
|
|
22 |
* @param {string} password
|
|
|
23 |
* @return {*}
|
|
|
24 |
*/
|
|
|
25 |
'decrypt': function (jsonStr, password) {
|
|
|
26 |
return JSON.parse(CryptoJS.AES.decrypt(jsonStr, password, { format: CryptoJSAesJson }).toString(CryptoJS.enc.Utf8))
|
|
|
27 |
},
|
|
|
28 |
/**
|
|
|
29 |
* Stringify cryptojs data
|
|
|
30 |
* @param {Object} cipherParams
|
|
|
31 |
* @return {string}
|
|
|
32 |
*/
|
|
|
33 |
'stringify': function (cipherParams) {
|
|
|
34 |
var j = { ct: cipherParams.ciphertext.toString(CryptoJS.enc.Base64) }
|
|
|
35 |
if (cipherParams.iv) j.iv = cipherParams.iv.toString()
|
|
|
36 |
if (cipherParams.salt) j.s = cipherParams.salt.toString()
|
|
|
37 |
return JSON.stringify(j).replace(/\s/g, '')
|
|
|
38 |
},
|
|
|
39 |
/**
|
|
|
40 |
* Parse cryptojs data
|
|
|
41 |
* @param {string} jsonStr
|
|
|
42 |
* @return {*}
|
|
|
43 |
*/
|
|
|
44 |
'parse': function (jsonStr) {
|
|
|
45 |
var j = JSON.parse(jsonStr)
|
|
|
46 |
var cipherParams = CryptoJS.lib.CipherParams.create({ ciphertext: CryptoJS.enc.Base64.parse(j.ct) })
|
|
|
47 |
if (j.iv) cipherParams.iv = CryptoJS.enc.Hex.parse(j.iv)
|
|
|
48 |
if (j.s) cipherParams.salt = CryptoJS.enc.Hex.parse(j.s)
|
|
|
49 |
return cipherParams
|
|
|
50 |
}
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
export default CryptoJSAesJson
|