From: martin f. krafft Date: Fri, 8 Dec 2023 13:06:38 +0000 (+0100) Subject: much improved password generator X-Git-Url: https://git.madduck.net/etc/pass.git/commitdiff_plain/HEAD?hp=2aced065a0982b92423fbc9ff292f441ed3ba692 much improved password generator --- diff --git a/.bin/genpw-editor b/.bin/genpw-editor index adf8b66..94fd07d 100755 --- a/.bin/genpw-editor +++ b/.bin/genpw-editor @@ -1,15 +1,32 @@ #!/bin/sh set -eu -if command -v diceware >/dev/null; then - PW_GENERATOR='diceware -s2' +generate_pw() +{ + local pw + if command -v diceware >/dev/null; then + words=4 + DICEWARE="$(diceware -n$words -d" " -s2 | sed -re "s,['\"],\\\\&,g")" \ + python3 - <<-_eof + import os, random, string, shlex + words = shlex.split(os.getenv('DICEWARE')) + delims = random.choices(string.digits, k=${words}-1) + for w, d in zip(words[:-1], delims[:len(words)-1]): + print(w, end=d) + print(words[-1]) + _eof -elif command -v pwgen >/dev/null; then - PW_GENERATOR='pwgen -y 20 1' + elif command -v pwgen >/dev/null; then + pwgen -sy 32 1 -else - PW_GENERATOR='dd if=/dev/random bs=256 count=1 | base64' + else + dd if=/dev/random bs=32 count=1 | base64 -fi + fi +} -exec vim -c 'set noshelltemp' -c "0read! $PW_GENERATOR" -c 2d -n "$@" +PASSPW="$(generate_pw)" +X_SELECTION="${PASSWORD_STORE_X_SELECTION:-clipboard}" +echo -n "$PASSPW" | xclip -selection $X_SELECTION +export PASSWD_ESCAPED="$(echo $PASSPW | sed -e 's,",\\",g')" +exec vim -c 'set noshelltemp' -c '0read! echo -n "$PASSWD_ESCAPED"' -c 2d -n "$@" diff --git a/.bin/pass b/.bin/pass new file mode 100755 index 0000000..c15a218 --- /dev/null +++ b/.bin/pass @@ -0,0 +1,31 @@ +#!/bin/sh +set -eu + +real_pass() { + local OLDIFS; OLDIFS="$IFS" + IFS=: + local SELF; SELF="$(realpath $0)" + local CMD; CMD="$(basename "$SELF")" + local ret; ret=127 + for p in $PATH; do + case "$SELF" in ($p/*) continue;; esac + if [ -x "$p/$CMD" ]; then + ${exec:-} "$p/$CMD" "$@" + ret=$? + break + fi + done + IFS="$OLDIFS" + return $ret +} + +KEYGRIP=083F1106EEDD459ED7761C825FDB3E60FA7D8F3F + +gpg-connect-agent "keyinfo $KEYGRIP" /bye | while read a b c d e f g h; do + if [ "$g" != 1 ]; then + real_pass .meta/pass-key | /usr/lib/gnupg/gpg-preset-passphrase -c $KEYGRIP + fi + break +done + +exec=exec real_pass "$@" diff --git a/.gitignore.d/pass b/.gitignore.d/pass index 61a21a1..6d0a468 100644 --- a/.gitignore.d/pass +++ b/.gitignore.d/pass @@ -1,5 +1,6 @@ * !/.bin/genpw-editor +!/.bin/pass !/.gitignore.d/pass !/.zsh/aliases/genpw !/.zsh/aliases/getpw diff --git a/.zsh/aliases/genpw b/.zsh/aliases/genpw index c497602..8e11b07 100644 --- a/.zsh/aliases/genpw +++ b/.zsh/aliases/genpw @@ -1 +1,2 @@ -() { EDITOR="genpw-editor $2" pass edit "$1"; } +() { EDITOR="genpw-editor" pass edit "$1"; } +compdef $0=pass diff --git a/.zsh/aliases/pmnpw b/.zsh/aliases/pmnpw index 2ff7e1a..a6d22c4 100644 --- a/.zsh/aliases/pmnpw +++ b/.zsh/aliases/pmnpw @@ -1 +1 @@ -() { case "$1" in (*@*) genpw ${1%%@*}/$1;; (*) genpw $1/${1}@pobox.madduck.net ${@:2};; esac } +() { case "$1" in (*@*) genpw ${1%%@*}/$1;; (*) genpw $1/${1}@pobox.madduck.net;; esac }