diff options
author | Dennis Eriksen <d@ennis.no> | 2023-09-05 16:14:06 +0200 |
---|---|---|
committer | Dennis Eriksen <d@ennis.no> | 2023-09-05 16:14:06 +0200 |
commit | de3fd4546d9a14a862048b0069fe1e83839e4831 (patch) | |
tree | e4ba7b74a76388626a4309c4023da921872ce10d /makepass.zsh | |
parent | fixing some shellcheck-errors and some tiny cleanup on sh- and bash-versions (diff) | |
download | makepass-de3fd4546d9a14a862048b0069fe1e83839e4831.tar.gz |
WIP: Cleanup and standardization
I want all the scripts to work in roughly the same way, with roughly the
same functions, doing roughly the same thing. That way the benchmark
means more. And also, I might learn something.
Diffstat (limited to '')
-rwxr-xr-x | makepass.zsh | 77 |
1 files changed, 53 insertions, 24 deletions
diff --git a/makepass.zsh b/makepass.zsh index ee92c33..c337939 100755 --- a/makepass.zsh +++ b/makepass.zsh @@ -20,7 +20,7 @@ function _makepass_help() { SYNOPSIS makepass [OPTIONS] [NUM] - If a NUM is provided, the we will output passwords of that length. + If a NUM is provided, passwords will be NUM characters long. By default `makepass` will output passwords from the three following classes: @@ -40,15 +40,25 @@ DESCRIPTION -h output this help-text + -l + length of passwords. See MAKEPASS_LENGTH below + -n + number of passwords. See MAKEPASS_NUMBER below ENVIRONMENT makepass examines the following environmental variables. - MAKEPASS_DEFAULT_LENGTH - Specifies the default length of passwords. Valid values are "random" - (default), or a number. If "random", a random value between 8 and 42 - will be used for each password. If `makepass` is run with a NUM as an - argument, that NUM overrides this variable. + MAKEPASS_LENGTH + Specifies the length of passwords. Valid values are 0-255. If 0, a + random value between 8 and 42 will be used for each password. -l + overrides this environmental variable, and NUM overrides that again. So + `MAKEPASS_LENGTH=10 makepass -l 12 14` will give passwords that are 14 + characters long. + + MAKEPASS_NUMBER + The default number of passwords from each group to output. By default + 10 normal passwords, six special passwords, and six passphrases, are + printed. Valid values are 1-255. MAKEPASS_NORMAL String of characters from which to generate "normal" passwords. @@ -106,19 +116,18 @@ function randphrase() { printf '%s\n' $string; return } - - # Function to create random strings function randstring() { # Default is a number between 8 and 44 - local -i len=$(( ${1:-0} > 0 ? ${1:-0} : RANDOM % (44 - 8 + 1) + 8)) + local -i len=$(( $1 ? $1 : RANDOM % (44 - 8 + 1) + 8 )) local chars=${2:-$alnum} local flc=${3:-} # first-last-character local string repeat $len string+=$chars[$((RANDOM % $#chars + 1))] - # If a third value is provided, it is used as the forst and last character in + # If a third value is provided, it is used as the first and last character in # the string + # TODO: Maybe this needs to be redone? Do it like in the perl-version? if [[ -n $flc ]]; then string=$flc[$((RANDOM % $#flc + 1))]$string[2,-2] (( len >= 2 )) && string+=$flc[$((RANDOM % $#flc + 1))] @@ -127,7 +136,12 @@ function randstring() { printf '%s\n' "$string"; return } - +# Function to die +function die() { + print -u2 -- "$@" + print -u2 -- "Maybe try running \`$ZSH_SCRIPT -h\` for help" + exit 1 +} # makepass-function. This is where the magic happens function makepass() { @@ -140,7 +154,7 @@ function makepass() { local alnum=${alpha}${digit} local every=${alnum}${other} - local length=${MAKEPASS_DEFAULT_LENGTH:-random} + local -i length=${MAKEPASS_LENGTH:-0} local normal=${MAKEPASS_NORMAL:-$alnum'-_'} local special=${MAKEPASS_SPECIAL:-$every} local wordlist=${MAKEPASS_WORDLIST:-/usr/share/dict/words} @@ -151,27 +165,42 @@ function makepass() { local b1=$r4[1] b2=$r4[2] b3=$r4[3] b4=$r4[4] RANDOM=$(( #b1 << 24 | #b2 << 16 | #b3 << 8 | #b4 )) + # Getopts + while getopts 'hl:n:' opt; do + case $opt in + h) + _makepass_help && return 0;; + l) + [[ ! $OPTARG = <0-255> ]] && die "-l takes a number between 0 and 255" + length=$OPTARG;; + n) + [[ ! $OPTARG = <1-255> ]] && die "-n takes a number between 1 and 255" + number=$OPTARG;; + *) + die "Unknown argument";; + esac + done - # Some error-checking - # We only take one argument - (( ARGC > 1 )) && print -u2 -- "only one argument\nmaybe try running \`makepass -h\` for help" && return 1 - # if $1 == -h - [[ $1 == '-h' ]] && _makepass_help && return 0 - # if $1 is not empty and is not a number - [[ -n $1 && ! $1 = <1-> ]] && print -u2 -- "not a number above 0\nmaybe try running \`makepass -h\` for help" && return 1 - - # length of passwords - local -i len=$(( length > 0 && ! ${1:-0} > 0 ? length : ${1:-0} )) + shift $((OPTIND - 1)) + # Some error-checking + # We only take one argument in addition to the optargs + (( ARGC > 1 )) && die "only one argument" + + [[ -n $1 ]] && length=$1 + [[ $length = <0-255> ]] || die "length must be a number between 0 and 255" + # + # Print! + # # Normal passwords print "Normal passwords:" - print -c -- $(repeat 10 { randstring $len $normal; : $RANDOM }) + print -c -- $(repeat 10 { randstring $length $normal; : $RANDOM }) : $RANDOM print # Passowrds with special characters print "Passwords with special characters:" - print -c -- $(repeat 6 { randstring $len $special $alpha; : $RANDOM }) + print -c -- $(repeat 6 { randstring $length $special $alpha; : $RANDOM }) : $RANDOM # Passphrases - but only if a wordlist is available |