Skip to content

Instantly share code, notes, and snippets.

@AlexandroMtzG
Created June 14, 2023 04:35
Show Gist options
  • Save AlexandroMtzG/9e0ebd0751f49e3f6d8b1eac3651195b to your computer and use it in GitHub Desktop.
Save AlexandroMtzG/9e0ebd0751f49e3f6d8b1eac3651195b to your computer and use it in GitHub Desktop.
import CryptoJS from "crypto-js";
function encrypt(text: string) {
const secret = process.env.CRYPTO_SECRET?.toString();
if (!secret) {
throw Error("CRYPTO_SECRET not set");
}
const ciphertext = CryptoJS.AES.encrypt(text, secret).toString();
return ciphertext;
}
function decrypt(ciphertext: string) {
const secret = process.env.CRYPTO_SECRET?.toString();
if (!secret) {
throw Error("CRYPTO_SECRET not set");
}
const bytes = CryptoJS.AES.decrypt(ciphertext, secret);
const originalText = bytes.toString(CryptoJS.enc.Utf8);
return originalText;
}
export default {
encrypt,
decrypt,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment