Skip to content

Instantly share code, notes, and snippets.

@factorin-j
Created January 10, 2020 04:51
Show Gist options
  • Select an option

  • Save factorin-j/1ec625bf3f52a40daa3dbc59544a8596 to your computer and use it in GitHub Desktop.

Select an option

Save factorin-j/1ec625bf3f52a40daa3dbc59544a8596 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
CMD=$(basename $0)
PEM="key.pem"
PUB="key.pub"
ENC=".env.enc"
TMP=".env.tmp"
ENV=".env"
if [[ ! -f ${PEM} ]]; then
echo "Private file not found."
exit 1
fi
if [[ ! -f ${PUB} ]]; then
echo "Public file not found."
exit 1
fi
envEncrypt() {
openssl rsautl -in ${ENV} -pubin -inkey ${PUB} -encrypt | base64 > ${ENC}
echo "[${CMD}] Encrypted file: "${ENV}" ~> "${ENC}
}
envDecrypt() {
cat ${ENC} | base64 --decode > ${TMP}
openssl rsautl -in ${TMP} -out ${ENV} -inkey ${PEM} -decrypt
echo "[${CMD}] Decrypted file: "${ENC}" ~> "${ENV}
rm -rf ${TMP}
}
showUsage() {
echo "Usage: ${CMD} <command>"
echo "Commands:"
echo " encrypt - Encrypts ${ENV} file and save to ${ENC} file."
echo " decrypt - Decrypts ${ENC} file and save to ${ENV} file."
exit 0
}
case $1 in
encrypt) envEncrypt;;
decrypt) envDecrypt;;
*) showUsage;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment