3 # Copyright (C) 2008-2011, William Trevor King <wking@drexel.edu>
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.
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.
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/>.
18 # generate a random password, defaults to 8 characters
20 # usage: passwd-gen [num-characters]
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).
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
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}")
44 echo "Length of password: ${#PWD}"
45 echo "Total bytes read: ${NA}"
46 echo "Device: ${DEVICE}"