Advertisement
opexxx

drgenpass

May 27th, 2014
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.24 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # $Id: drgenpass 44 2011-09-15 23:17:50Z jtk $
  4. # http://dragonresearchgroup.org
  5.  
  6. usage() {
  7.     echo $0 [password_length] [number_of_passwords] ['character_set']
  8.     echo
  9.     echo 'password_length       positive integer, default=16, optional'
  10.     echo 'number_of_passwords   positive integer, default=1, optional'
  11.     echo 'character_set         string, default=[:graph:], optional'
  12.     exit 1
  13. }
  14.  
  15. # is_positive_int() derived from Portable Shell Scripting, Blinn, p. 134
  16. is_positive_int() {
  17.     if [ $# -ne 1 ]
  18.     then
  19.         return 1
  20.     fi
  21.  
  22.     expr "$1" + 1 > /dev/null 2>&1
  23.     if [ $? -ge 2 ]
  24.     then
  25.         return 1
  26.     fi
  27.  
  28.     if [ $1 -lt 1 ]
  29.     then
  30.         return 1
  31.     fi
  32.  
  33.     return 0
  34. }
  35.  
  36. LEN=${1:-16}          # arg1, length of password, optional
  37. NUM=${2:-1}           # arg2, number of passwords to generate, optional
  38. SET=${3:-[:graph:]}   # arg3, character class or set, optional
  39.  
  40. is_positive_int $LEN || usage
  41. is_positive_int $NUM || usage
  42.  
  43. # tr and fold technique derived from:
  44. #   <http://blog.colovirt.com/2009/01/07/linux-generating-strong-passwords-using-randomurandom/>
  45. # NOTE: LANG=C is a hack for when tr is locale aware
  46. cat /dev/urandom | LANG=C tr -cd $SET | fold -w $LEN | head -n $NUM
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement