Skip to content

Instantly share code, notes, and snippets.

@0xa
Created November 5, 2012 23:29
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 0xa/4021097 to your computer and use it in GitHub Desktop.
Save 0xa/4021097 to your computer and use it in GitHub Desktop.
GPG password manager
#!/bin/bash
# Where to store passwords
DATAFILE="./pwmgr_data.gpg"
# Your GPG key
RECIPIENT="xa@xomg.net"
# Command used to generate passwords
# (pwgen is nice)
GENCMD="pwgen -sy1 -n 64"
function help {
echo "-- GPG Password Manager --"
echo "usage: pwmgr.sh <action> <key>"
echo "action: get : print password to stdout"
echo " set : change password with stdin"
echo " gen : generate a random password"
echo " new : create a new empty password file (erase)"
echo " help: this text"
echo "made by Xa <xa@xomg.net>"
}
function gpg_readfile {
gpg --decrypt $DATAFILE
}
function gpg_readkey {
gpg_readfile | grep -G "^$1:" | cut -d":" -f 2
}
function gpg_writefile {
content=`cat /dev/stdin`
echo "$content" | gpg --encrypt --recipient $RECIPIENT > $DATAFILE
}
function gpg_writekey {
file=`gpg_readfile`
keyline=`echo $file | grep -G \"^$1:\"`
if [ "$keyline" != "" ]; then
echo $file | sed "s/^$1\:.+$/$1:$2/" | gpg_writefile
else
(echo "$file" && echo "$1:$2") | gpg_writefile
fi
}
function gpg_append {
(gpg_readfile && echo "$1:$2") | gpg_writefile
}
function a_get {
gpg_readkey $1
}
function a_set {
read passwd
gpg_writekey $1 $passwd
}
function a_gen {
gpg_writekey $1 `$GENCMD`
}
function a_new {
touch $DATAFILE
cp $DATAFILE $DATAFILE.bak
echo "" | gpg_writefile
}
if [ -z $1 ] || ( ( [ "$1" != "new" ] && [ "$1" != "help" ] ) && [ -z $2 ] ); then
help
exit
fi
case "$1" in
"get")
a_get $2;;
"set")
a_set $2;;
"gen")
a_gen $2;;
"new")
a_new;;
"help")
help;;
*)
help;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment