summaryrefslogtreecommitdiff
path: root/badpass
blob: 4d03eb9935f3168761af668db5c5d2af6e999c15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/bin/sh

# A bad password derivation scheme, hopefully portable.
# Requires `expect' to enable `keychain' key unlocking.
# Read source for detail.

PREFIX="Prepended to every password, usually special chars + uppercase"
MIDFIX="A not-necessarily-secret string to prevent pre-baked rainbow tables"

echo -n "Master password: "

# Supress echo, and trap exit for correct restoration
stty -echo
trap 'stty echo' EXIT

# Read without processing at all
IFS=""
read -r MASTER

# Restore state
stty echo
trap - EXIT
echo

USEKEYCHAIN () {
    expect - <<EOF
spawn keychain --nogui --quiet $1
set send_slow {1 0.01}
expect {
  "Enter passphrase for $HOME/.ssh/$1:"; { send -s "$2\n"; echo DONE; }
}
EOF
    echo
}

CALL=echo
if [ "$1" = "-k" ]; then
    CALL=USEKEYCHAIN
    shift
fi;

for NAME in "$@"; do
    PREPASS=$(echo $NAME$MIDFIX$MASTER | sha256sum | cut -b -24)
    $CALL $NAME $PREFIX$PREPASS
done