Skip to content

Instantly share code, notes, and snippets.

@igorjs
Last active August 23, 2021 06:43
Show Gist options
  • Save igorjs/9efc6d09c23eded97bc447405f1d2a6e to your computer and use it in GitHub Desktop.
Save igorjs/9efc6d09c23eded97bc447405f1d2a6e to your computer and use it in GitHub Desktop.
/**
* 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