ypasswordcli/ypassword
Yann Esposito (Yogsototh) b5922d54d3 Works on Mac OS X
2011-10-16 21:33:47 +02:00

66 lines
1.7 KiB
Bash
Executable file

#!/usr/bin/env zsh
(($#<1)) && {
print -- "usage: $0:t pattern"
print -- " generate a password using the ypassword method"
print -- " See http://ypassword.espozito.com for more informations"
} >&2
param="$*"
passfile="~/.passwd"
cmd="awk '\$1 ~ /$param/ {print;}' < $passfile"
nblignes=$( eval $cmd | wc -l)
function col {
colnum=$1
shift
echo $* | awk "{print \$$colnum}"
}
if ((nblignes<1)); then
echo "entrée non existante"
elif ((nblignes>1)); then
echo "plusieur cas possibles : "
eval $cmd | sort
else
line="$(eval $cmd)"
site=$( col 1 $line )
domainName=$( echo $site | sed 's/.*\.\([^.]*\.[^.]*\)$/\1/')
loginName=$( col 2 $line )
longueur=$( col 3 $line )
base64=$( col 4 $line )
hashalgo=$( col 5 $line )
num=$( col 6 $line )
echo "Enter your master password please"
echo -n "$site ($loginName): "
oldmodes=$(stty -g)
stty -echo
read password
stty $oldmodes
if [[ -x =shasum ]]; then
case $hashalgo in
sha1) hashcmd="shasum -a 1" ;;
sha256) hashcmd="shasum -a 256" ;;
sha512) hashcmd="shasum -a 512" ;;
*) print -- "Unknown algorithm: $hashalgo" >&2
exit 1
esac
else
case $hashalgo in
sha1) hashcmd="sha1sum" ;;
sha256) hashcmd="sha256sum" ;;
sha512) hashcmd="sha512sum" ;;
*) print -- "Unknown algorithm: $hashalgo" >&2
exit 1
esac
fi
if [[ $base64 = "b64" ]]; then
# make a hex2b64
hashcmd="$hashcmd | cut -f1 -d\\ | xxd -r -p | base64"
fi
cmd='echo -n "'$password$num$domainName'" | '$hashcmd' | awk "{print substr(\$1,1,'$longueur');}"'
eval $cmd
fi