aboutsummaryrefslogtreecommitdiff
path: root/misc/.local/bin/genpass
blob: 3d2dbe73ef64461b3c3b7e6a4b06ce772bef204c (plain) (blame)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/bash

## Poor man's password generator.
#
# Requires:
#   xclip (optional): copies password to clipboard
#   qrencode (optional): generate QR code of the password
#
# TODO:
#   Override clipboard after a configurable amount of time (https://git.zx2c4.com/password-store/tree/src/password-store.sh#n173)

NC='\033[0m'
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'

msg_info() {
    echo -e "${GREEN}$1${NC}"
}

msg_warn() {
    echo -e "${YELLOW}$1${NC}"
}

msg_error() {
    echo -e "${RED}$1${NC}"
}

print_help() {
    echo
    echo "genpass - a poor man's password generator"
    echo
    echo "USAGE:"
    echo "    genpass [OPTION ...]"
    echo
    echo "where OPTIONs are:"
    echo "    -c | --clipboard:"
    echo "        copy the generated password to clipboard (needs xclip)"
    echo "    -f | --filter <filter>:"
    echo "        specify a custom filter for the password (see specification"
    echo "        for SETs in 'tr' man page). Defaults to 'A-Za-z0-9_'."
    echo "    -h | -h | --help:"
    echo "        print this help message"
    echo "    -l | --length <num>:"
    echo "        provide password length (defaults to 32)"
    echo "    -q | --qrcode:"
    echo "        generate a qrcode of the password (needs qrencode) and"
    echo "        print it to stdout (UTF8 format)"
    echo
}

CLIPBOARD=0
QRCODE=0
LENGTH=32
FILTER=A-Za-z0-9_

while [ $# != 0 ]
do
    arg=$1
    case "$arg" in
        -c|--clipboard)
            CLIPBOARD=1
            ;;
        -f|--filter)
            if [[ $# -ge 2 ]]; then
                FILTER=$2
                shift
            else
                msg_error "No value found for flag $arg!"
                print_help
                exit 1
            fi
            ;;
        -h|--help)
            print_help
            exit 0
            ;;
        -l|--length)
            if [[ $# -ge 2 ]]; then
                LENGTH=$2
                shift
            else
                msg_error "No value found for flag $arg!"
                print_help
                exit 2
            fi
            ;;
        -q|--qrcode)
            QRCODE=1
            ;;
        *)
            msg_error "$1: invalid option"
            print_help
            exit 3
            ;;
    esac
    shift
done

PASSWD="$(tr -dc $FILTER < /dev/urandom | head -c $LENGTH)"
if [ $CLIPBOARD -eq 1 ]; then
    echo -nE "$PASSWD" | xclip -in -selection clipboard
elif [ $QRCODE -eq 1 ]; then
    echo -nE "$PASSWD" | qrencode --size=5 --type=UTF8 -o -
else
    echo -E "$PASSWD"
fi

exit 0