-
-
Save factorin-j/1ec625bf3f52a40daa3dbc59544a8596 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
| #!/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