Created
November 5, 2012 23:29
-
-
Save 0xa/4021097 to your computer and use it in GitHub Desktop.
GPG password manager
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 | |
# 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