Last active
August 23, 2021 06:43
-
-
Save igorjs/9efc6d09c23eded97bc447405f1d2a6e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* DISCLAIMER: | |
* This Utility is in a Work in Progress state. | |
* The ideia is to encrypt the sensitive data in the Frontend to prevent XSS data stealing. | |
* | |
* @author Igor Santos <[email protected]> | |
* | |
* @see https://www.npmjs.com/package/crypto-js | |
*/ | |
import CryptoJS from 'crypto-js'; | |
export const encryptData = (data: unknown, salt: string): string => { | |
return CryptoJS.AES.encrypt(JSON.stringify(data), salt).toString(); | |
}; | |
export const decryptData = (ciphertext: string, salt: string): unknown => { | |
try { | |
const bytes = CryptoJS.AES.decrypt(ciphertext, salt); | |
return JSON.parse(bytes.toString(CryptoJS.enc.Utf8)); | |
} catch (err) { | |
return null; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment