aboutsummaryrefslogtreecommitdiffstats
path: root/bin
diff options
context:
space:
mode:
authorDennis Eriksen <d@ennis.no>2022-12-20 08:37:18 +0100
committerDennis Eriksen <d@ennis.no>2022-12-20 08:37:18 +0100
commit4cc9c043a869b5c74194de18d6bfe443580f4f20 (patch)
tree99e1c0dfe6deadba717ba49afdc9fed290d8bf68 /bin
parentforgot to change an instance of head -c (diff)
downloadmakepass-4cc9c043a869b5c74194de18d6bfe443580f4f20.tar.gz
a bit more work on makepass.zsh and makepass.sh
Diffstat (limited to 'bin')
-rwxr-xr-xbin/bin/makepass.sh102
-rwxr-xr-xbin/bin/makepass.zsh71
2 files changed, 86 insertions, 87 deletions
diff --git a/bin/bin/makepass.sh b/bin/bin/makepass.sh
index 502867a..78e3b75 100755
--- a/bin/bin/makepass.sh
+++ b/bin/bin/makepass.sh
@@ -7,65 +7,26 @@
################################################################################
# This file takes randomness from /dev/urandom and turns it into random
# passwords.
-################################################################################
+#
+# This particualr script is meant to be fully POSIX compatible.
+################################################################################
# Copyright (c) 2018-2022 Dennis Eriksen • d@ennis.no
+# makepass-function
+makepass() {
+ MAKEPASS_WORDLIST=${MAKEPASS_WORDLIST:-/usr/share/dict/words}
-# Start with error-checking
-
-# We only take one argument
-[ "$#" -gt 1 ] && printf '%s\n' 'only one argument' && return 1
-
-# if $1 is not empty and is not a number
-if [ ! -z "$1" ] && ! printf '%d' "$1" >/dev/null 2>&1; then printf '%s\n' 'not a number' && return 1; fi
-if [ ! -z "$1" ] && [ ! ${1:-0} -gt 0 ]; then printf '%s\n' 'not a number above 0'; return 1; fi
-
-# get random number
-_RANDOM() {
- N=0
- while [ ! "$N" = "${N#0}" ]; do
- N=$(head -n100 /dev/urandom | tr -cd "[:digit:]" | tail -c 8)
- done
- printf '%s\n' "$N"
- return 0
-}
-
-# Function to create random stuff
-_random() (
- # sh does not like leading zeroes when doing math with numbers. Let's reset until we have a number that doesn't start with 0.
-
- # Default is a number between 8 and 44
- len=${1:-$(($(_RANDOM) % (44 - 8 + 1) + 8))}
-
- # Default to [:alnum:] if no chars are specified
- chars=${2:-'[:alnum:]'}
-
- # First-Last-Alpha - if you want the first and the last letter to be from [:alpha:]
- fla=${3:-'false'}
+ # We only take one argument
+ [ "$#" -gt 1 ] && printf '%s\n' 'only one argument' && return 1
- if [ "$fla" = "true" ]; then
- if [ $len -le 2 ]; then
- string="$(head -n10 /dev/urandom | tr -cd '[:alpha:]' | tail -c $len)"
- else
- string="$(head -n10 /dev/urandom | tr -cd '[:alpha:]' | tail -c 1)"
- string="${string}$(head -n100 /dev/urandom | tr -cd "$chars" | tail -c $((len-2)))"
- string="${string}$(head -n10 /dev/urandom | tr -cd '[:alpha:]' | tail -c 1)"
- fi
- else
- string="$(head -n100 /dev/urandom | tr -cd "$chars" | tail -c $len)"
- fi
+ # if $1 is not empty and is not a number
+ if [ ! -z "$1" ] && ! printf '%d' "$1" >/dev/null 2>&1; then printf '%s\n' 'not a number' && return 1; fi
+ if [ ! -z "$1" ] && [ ! ${1:-0} -gt 0 ]; then printf '%s\n' 'not a number above 0'; return 1; fi
- printf '%s\n' "$string"
- unset len chars fla string
- return 0
-)
-
-# makepass-function
-makepass() {
+ # Go!
len=$1
- MAKEPASS_WORDLIST=${MAKEPASS_WORDLIST:-/usr/share/dict/words}
printf '%s\n' 'Normal passwords:'
i=0
@@ -105,6 +66,45 @@ makepass() {
return 0
}
+# get random number
+_RANDOM() {
+ N=0
+ while [ ! "$N" = "${N#0}" ]; do
+ N=$(head -n 100 /dev/urandom | tr -cd "[:digit:]" | tail -c 8)
+ done
+ printf '%s\n' "$N" && return 0
+}
+
+# Function to create random stuff
+_random() (
+ # sh does not like leading zeroes when doing math with numbers. Let's reset until we have a number that doesn't start with 0.
+
+ # Default is a number between 8 and 44
+ len=${1:-$(($(_RANDOM) % (44 - 8 + 1) + 8))}
+
+ # Default to [:alnum:] if no chars are specified
+ chars=${2:-'[:alnum:]'}
+
+ # First-Last-Alpha - if you want the first and the last letter to be from [:alpha:]
+ fla=${3:-'false'}
+
+ if [ "$fla" = "true" ]; then
+ if [ $len -le 2 ]; then
+ string="$(head -n 10 /dev/urandom | tr -cd '[:alpha:]' | tail -c $len)"
+ else
+ string="$(head -n 10 /dev/urandom | tr -cd '[:alpha:]' | tail -c 1)"
+ string="${string}$(head -n 100 /dev/urandom | tr -cd "$chars" | tail -c $((len-2)))"
+ string="${string}$(head -n 10 /dev/urandom | tr -cd '[:alpha:]' | tail -c 1)"
+ fi
+ else
+ string="$(head -n 100 /dev/urandom | tr -cd "$chars" | tail -c $len)"
+ fi
+
+ printf '%s\n' "$string"
+ unset len chars fla string
+ return 0
+)
+
makepass "${@:-}" && return 0
## END OF FILE #################################################################
diff --git a/bin/bin/makepass.zsh b/bin/bin/makepass.zsh
index e5dd8bd..182317f 100755
--- a/bin/bin/makepass.zsh
+++ b/bin/bin/makepass.zsh
@@ -8,24 +8,52 @@ emulate -L zsh
################################################################################
# This file takes randomness from /dev/urandom and turns it into random
# passwords.
-################################################################################
+################################################################################
# Copyright (c) 2018-2022 Dennis Eriksen • d@ennis.no
+# makepass-function. This is where the magic happens
+function makepass() {
+ setopt local_options
-# Start with error-checking
+ MAKEPASS_WORDLIST=${MAKEPASS_WORDLIST:-/usr/share/dict/words}
+ RANDOM=$(head -n 10 /dev/urandom | tr -cd '[:digit:]' | tail -c 10) # seed RANDOM
-# We only take one argument
-(( ARGC > 1 )) && print "only one argument" && return 1
+ # Some error-checking
+ # We only take one argument
+ (( ARGC > 1 )) && print "only one argument" && return 1
+ # if $1 is not empty and is not a number
+ [[ ! -z "$1" && ! $1 = <1-> ]] && print "not a number above 0" && return 1
-# if $1 is not empty and is not a number
-[[ ! -z "$1" && ! $1 = <1-> ]] && print "not a number above 0" && return 1
+ local -i len=$1 # is automatically 0 if $1 is unset or not a number
+
+ # Normal passwords
+ print "Normal passwords:"
+ (repeat 10 _random $len '_A-Z-a-z-0-9') | column
+ print
+
+ # Passowrds with special characters
+ print "Passwords with special characters:"
+ (repeat 6 _random $len '!#$%&/()=?+-_,.;:<>[]{}|\@*^A-Z-a-z-0-9' true) | column
+
+ # Passphrases - but only if a wordlist is available
+ if [[ -r "$MAKEPASS_WORDLIST" ]]; then
+ print
+ print "Passphrases:"
+ local -a words=(${(f)"$(<"$MAKEPASS_WORDLIST")"})
+
+ for i in {1..5}; do
+ print "$RANDOM" > /dev/null # The values of RANDOM form an intentionally-repeatable pseudo-random sequence; subshells that reference RANDOM will result in identical pseudo-random values unless the value of RANDOM is referenced or seeded in the parent shell in between subshell invocations.
+ print $(repeat 8 print "$words[RANDOM % $#words +1]") | tr ' ' '-' | tr -dc '_A-Z-a-z-0-9'; print
+ done
+ fi
+}
# Function to create random stuff
function _random() {
setopt local_options
- RANDOM=$(head -n 100 /dev/urandom | tr -cd "[:digit:]" | tail -c 8) # seed RANDOM
+ RANDOM=$(head -n 10 /dev/urandom | tr -cd "[:digit:]" | tail -c 8) # seed RANDOM
# Default is a number between 8 and 44
local -i len=$1
@@ -54,35 +82,6 @@ function _random() {
printf '%s\n' "$string"; return
}
-# makepass-function
-function makepass() {
- setopt local_options
- local -i len=$1
- MAKEPASS_WORDLIST=${MAKEPASS_WORDLIST:-/usr/share/dict/words}
- RANDOM=$(head -n10 /dev/urandom | tr -cd '[:digit:]' | tail -c 10) # seed RANDOM
-
- # Normal passwords
- print "Normal passwords:"
- (repeat 10 _random $len '_A-Z-a-z-0-9') | column
- print
-
- # Passowrds with special characters
- print "Passwords with special characters:"
- (repeat 6 _random $len '!#$%&/()=?+-_,.;:<>[]{}|\@*^A-Z-a-z-0-9' true) | column
-
- # Passphrases
- if [[ -r "$MAKEPASS_WORDLIST" ]]; then
- print
- print "Passphrases:"
- local -a words=(${(f)"$(<"$MAKEPASS_WORDLIST")"})
-
- for i in {1..5}; do
- print "$RANDOM" > /dev/null # The values of RANDOM form an intentionally-repeatable pseudo-random sequence; subshells that reference RANDOM will result in identical pseudo-random values unless the value of RANDOM is referenced or seeded in the parent shell in between subshell invocations.
- print $(repeat 8 print "$words[RANDOM % $#words +1]") | tr ' ' '-' | tr -dc '_A-Z-a-z-0-9'; print
- done
- fi
-}
-
makepass "${@:-}"
## END OF FILE #################################################################