Created
June 14, 2023 04:35
-
-
Save AlexandroMtzG/9e0ebd0751f49e3f6d8b1eac3651195b to your computer and use it in GitHub Desktop.
This file contains 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
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