Skip to content

Instantly share code, notes, and snippets.

@mreschke
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mreschke/9140387 to your computer and use it in GitHub Desktop.
Save mreschke/9140387 to your computer and use it in GitHub Desktop.
Create, Edit, Search and List a single GPG encrypted file
#!/bin/bash
# Create, Edit, Search and List a single GPG encrypted file
# Copyright (C) 2014 Matthew Reschke
# License MIT <mreschke.com/license/mit>
# Written by Matthew Reschke <mreschke.com/me> 2014-01-24
file="$1"
option="$2"
me=`basename $0`
editor="vim"
function decrypt() {
#echo "Decrypting passwords file"
read -s -p "Passphrase: " pass
echo
gpg --passphrase $pass "$file.gpg" > /dev/null 2>&1 \
&& echo "-----------" && return 0 \
|| echo "Access Denied" && return 2
}
function encrypt() {
echo "Encrypting passwords file"
gpg --batch --yes --passphrase $pass -c "$file"
}
function cleanup() {
if [ -e "$file" ]; then
rm $file
fi
}
function usage() {
# Show usage
echo "secret-file (gpg encrypted file helper) 1.0"
echo "Copyright (C) 2014 Matthew Reschke"
echo "License MIT <mreschke.com/license/mit>"
echo "Written by Matthew Reschke <mreschke.com/me> 2014-01-24"
echo
echo "Usage:"
echo " $me /some/file.txt --create"
echo " $me /some/file.txt --edit"
echo " $me /some/file.txt --list"
echo " $me /some/file.txt searchquery"
echo
}
if [[ -z "$file" ]]; then
usage
exit 1
fi
if [ -e "$file.gpg" ]; then
if [ "$option" = "--edit" ]; then
# Editing passwords file
decrypt && $editor $file && encrypt
cleanup
elif [ "$option" = "--list" ]; then
# Show all passwords
decrypt && cat $file
cleanup
elif [ "$option" = "" ]; then
usage
else
# Search passwords file
decrypt && cat $file | grep -i $option
cleanup
fi
else
if [ "$option" == "--create" ]; then
read -s -p "Passphrase: " pass
touch $file && encrypt && cleanup && $editor $file && encrypt
cleanup
else
echo "File $file not found"
fi
fi
@mreschke
Copy link
Author

Great for managing a your private password file (keep all your passwords in one GPG encrypted file). If you create your password file with everything on one line then the searchquery (grep) works great. Example file:

SITE: google.com             myusername / mypassword
SITE: facebook.com           myusername / mypassword

FTP: myftpsite.com           myusername / mypassword

I also setup other small bash scripts that simply calls this secret-file script passing in the proper parameters. So I have a file called /home/mreschke/.passwords with all my secret passwords. I have /usr/local/bin/secret-file + a /usr/local/bin/password script like this

#!/bin/bash
/usr/local/bin/secret-file /home/mreschke/.passwords $1

So all I have todo is run
password --list
password --edit
password searchquery
...
perfectly easy single GPG file management

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment