Password Generator



Published: 2017-05-14 12:11:19 +0000
Categories: BASH,

Language

BASH

Description

A password generator in BASH. It uses /dev/urandom to help generate a random alphanumeric password

By default it will generate a 16 character password with special characters, but accepts various options to control what the password is comprised of

gen_passwd [Length] [nc (No special chars) | lc (lower case no specials) | pin (numeric only) | mixed (default)

Based upon the encryption key generator in my backup encryption scripts

Similar to

pwgen

Snippet

gen_passwd(){
      keylen=${1-16}
      charflag=$2

      if [ "$keylen" == "--help" ] || [ "$keylen" == "-h" ]
      then
          echo "$0 [Length] [nc (No special chars) | lc (lower case no specials) | pin (numeric only) | mixed (default)]"
          return 1
      fi

      if [ "$charflag" == "nc" ]
      then
          chars='a-zA-Z0-9'
      elif [ "$charflag" == "lc" ]
      then
          chars='a-z0-9'
      elif [ "$charflag" == "pin" ]
      then
          chars='0-9'
      else
          chars='a-zA-Z0-9-_!@#$%^&*()_+{}|:<>?='
      fi

      cat /dev/urandom | tr -dc "$chars" |fold -w $keylen| head -n 1
}

Usage Example

gen_passwd         # 16 Char alphanumeric password, mixed characters
gen_passwd 12      # 12 Char alphanumeric password, mixed characters
gen_passwd 12 nc   # 12 Char alphanumeric password, no special characters
gen_passwd 12 lc   # 12 Char alphanumeric password, lower case chars, no special characters
gen_passwd 6 pin   # 6 Char PIN (numbers only)

License

GNU GPL V2

Keywords

Password, Generator, Random,

Latest Posts


Copyright © 2019 Ben Tasker | Sitemap | Privacy Policy
Available at snippets.bentasker.co.uk, yr4pnhounvdybotb.onion and snippets.6zdgh5a5e6zpchdz.onion