mkogg.py: Fix 'self.get_mp4_metadata(self, source)'
[blog.git] / posts / passwd-gen / passwd-gen.sh
1 #!/bin/bash
2 #
3 # Copyright (C) 2008-2011, William Trevor King <wking@drexel.edu>
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 # generate a random password, defaults to 8 characters
19 #
20 # usage: passwd-gen [num-characters]
21
22 # You may want to modify NC to get the best performance depending on
23 # your usual choice of DEVICE and N.  For slower DEVICEs, it's better
24 # to have lower NC (wastes less usable characters).  For faster
25 # DEVICEs, it's better to have a higher NC (fewer slow shell loops).
26
27 N="${1:-8}"               # password length
28 NC="${NC:-4}"             # number of characters in a "chunk"
29 #SET="[:alnum:][:punct:]" # set of allowed characters (see tr(1))
30 SET="${SET:-[:alnum:]}"
31 DEVICE="${DEVICE:-/dev/random}" # use /dev/urandom for faster, weaker passwords
32
33 NA=0
34 PWD=""
35 while [ "${N}" -gt 0 ]; do
36                 ALL=$(head -c "${NC}" "${DEVICE}")
37                 let "NA = NA + ${#ALL}"
38     NEW=$(echo -E "${ALL}" | tr -d -c "${SET}" | head -c "${N}")
39     PWD="${PWD}${NEW}"
40     let "N = N - ${#NEW}"
41 done
42
43 echo "${PWD}"
44 echo "Length of password: ${#PWD}"
45 echo "Total bytes read:   ${NA}"
46 echo "Device:             ${DEVICE}"
47 echo "Set:                ${SET}"
48
49 exit 0