Last active
August 15, 2019 14:02
-
-
Save wpcarro/cb89372482e452510412d747262899e2 to your computer and use it in GitHub Desktop.
Personal helper functions for encrypting and decrypting.
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
| # Note: there are almost assuredly faster and potentially more secure ways to encrypt files. | |
| # My focus with these function was security and ergonomics. I find GPG functions difficult to | |
| # remember and unwieldy to type. The easier encrypting and decrypting is, the more likely I am | |
| # to do it. All other things being equal, some encryption is probably better than no encryption. | |
| # | |
| # Usage: | |
| # $ encrypt *.txt dir_{a,b} | |
| # $ decrypt *.txt.gpg dir_{a,b}.tar.gz.gpg | |
| me=wpcarro@gmail.com | |
| _do_encrypt() { | |
| # Helper function for `encrypt`. | |
| # depends gpg targz_dir | |
| echo "Encrypting..." | |
| if [ -f $1 ]; then | |
| gpg --encrypt --recipient="${me}" $1 | |
| rm $1 | |
| elif [ -d $1 ]; then | |
| targz_dir $1 | |
| # NOTE: recursion is nice here but it causes the echo statements to happen | |
| # twice. Because of this, we redirect to /dev/null. | |
| _do_encrypt "$1.tar.gz" >/dev/null | |
| fi | |
| echo "Done." | |
| } | |
| _do_decrypt() { | |
| # Helper function for `decrypt`. | |
| # depends gpg untargz_dir | |
| echo "Decrypting..." | |
| gpg --decrypt $1 2>/dev/null >"${1%.gpg}" | |
| rm $1 | |
| # If the file ends with tar.gz, it was most like a directory that we targz'd | |
| # then encrypted. | |
| if [[ "${1%.gpg}" =~ \.tar.gz$ ]]; then | |
| untargz_dir "${1%.gpg}" >/dev/null | |
| fi | |
| echo "Done." | |
| } | |
| encrypt() { | |
| # Convenience function around encrypting files and directories. | |
| # Appends a .gpg extension and deletes the unencrypted source. | |
| # depends _do_encrypt | |
| for f in $@; do | |
| _do_encrypt $f | |
| done | |
| } | |
| decrypt() { | |
| # Convenience function around decrypting .gpg files and directories. | |
| # Deletes the original encrypted file with the .gpg extension. | |
| # depends _do_decrypt | |
| for f in $@; do | |
| _do_decrypt $f | |
| done | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment