Last active
August 29, 2015 13:56
-
-
Save mreschke/9140387 to your computer and use it in GitHub Desktop.
Create, Edit, Search and List a single GPG encrypted file
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
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
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
So all I have todo is run
password --list
password --edit
password searchquery
...
perfectly easy single GPG file management