diff options
author | Dennis Eriksen <d@ennis.no> | 2022-12-19 20:34:25 +0100 |
---|---|---|
committer | Dennis Eriksen <d@ennis.no> | 2022-12-19 20:34:25 +0100 |
commit | 61db3b16cf51e21b2293d257e0c23fad9afb37f5 (patch) | |
tree | 25d851e810c7eef2f1ecc3625c6aab5325942792 /bin | |
parent | makepass wasn't working on bsd (bsd-version of head didn't take -c as an (diff) | |
download | makepass-61db3b16cf51e21b2293d257e0c23fad9afb37f5.tar.gz |
adding a zsh and a posix sh implementation of makepass.
Diffstat (limited to 'bin')
l---------[-rwxr-xr-x] | bin/bin/makepass | 60 | ||||
-rwxr-xr-x | bin/bin/makepass.bash | 59 | ||||
-rwxr-xr-x | bin/bin/makepass.sh | 110 | ||||
-rwxr-xr-x | bin/bin/makepass.zsh | 88 |
4 files changed, 258 insertions, 59 deletions
diff --git a/bin/bin/makepass b/bin/bin/makepass index 0ebc60b..a40a9ef 100755..120000 --- a/bin/bin/makepass +++ b/bin/bin/makepass @@ -1,59 +1 @@ -#!/usr/bin/env bash -# Filename: ~/bin/makepass -# Purpose: Creating random passwords. -# Authors: Dennis Eriksen <d@ennis.no> -# Some parts have been shamelessly stolen from other places. -# These parts will normally have a comment saying where it's -# from. For instance, this header format is stolen from the -# GRML-team (grml.org). -# Bug-Reports: Email <idgatt@dnns.no> -# License: This file is licensed under the GPL v2. -################################################################################ -# This file takes randomness from /dev/urandom and turns it into random -# passwords. -################################################################################ - - -# Copyright (c) 2018 Dennis Eriksen • d@ennis.no - -# makepass-function -function makepass { - local l=$1 - local words - local first - local last - MAKEPASS_WORDLIST=${MAKEPASS_WORDLIST:-/usr/share/dict/words} - # if $l is not a number, then exit - [[ ! $l =~ ^[0-9]+$ ]] && [[ ! "$l" == "" ]] && echo "not a number" && return 1 - # if $1 is actually empty, set $l to random value for each output - echo "Normal passwords:" - for i in {1..10}; do - [ "$1" = "" ] && l=$(shuf -i 8-44 -n 1) - head -n10 /dev/urandom | tr -dc _A-Z-a-z-0-9 | cut -c-${1:-$l}; - done | column - echo "" - - echo "Passwords with special characters:" - for i in {1..6}; do - [ "$1" = "" ] && l=$(shuf -i 16-64 -n 1) - first=$(head -n10 /dev/urandom | tr -dc A-Za-z | cut -c-1) - words=$(head -n10 /dev/urandom | tr -dc '!#$%&/()=?+-_,.;:<>[]{}|\@*^A-Z-a-z-0-9' | cut -c-${1:-$l}) - last=$(head -n10 /dev/urandom | tr -dc A-Za-z | cut -c-1) - echo "${first}${words}${last}" - done | column - - if [ -r "${MAKEPASS_WORDLIST}" ]; then - echo "" - echo "Passphrases:" - - for i in {1..5}; do - words=$(shuf -n 8 "${MAKEPASS_WORDLIST}" | tr '\n' '-' | tr -dc '_A-Z-a-z-0-9') - echo "${words:0:-1}" - done; - fi -} - -makepass "${@:-}" - -## END OF FILE ################################################################# -# vim:syntax=sh filetype=sh +makepass.zsh
\ No newline at end of file diff --git a/bin/bin/makepass.bash b/bin/bin/makepass.bash new file mode 100755 index 0000000..0ebc60b --- /dev/null +++ b/bin/bin/makepass.bash @@ -0,0 +1,59 @@ +#!/usr/bin/env bash +# Filename: ~/bin/makepass +# Purpose: Creating random passwords. +# Authors: Dennis Eriksen <d@ennis.no> +# Some parts have been shamelessly stolen from other places. +# These parts will normally have a comment saying where it's +# from. For instance, this header format is stolen from the +# GRML-team (grml.org). +# Bug-Reports: Email <idgatt@dnns.no> +# License: This file is licensed under the GPL v2. +################################################################################ +# This file takes randomness from /dev/urandom and turns it into random +# passwords. +################################################################################ + + +# Copyright (c) 2018 Dennis Eriksen • d@ennis.no + +# makepass-function +function makepass { + local l=$1 + local words + local first + local last + MAKEPASS_WORDLIST=${MAKEPASS_WORDLIST:-/usr/share/dict/words} + # if $l is not a number, then exit + [[ ! $l =~ ^[0-9]+$ ]] && [[ ! "$l" == "" ]] && echo "not a number" && return 1 + # if $1 is actually empty, set $l to random value for each output + echo "Normal passwords:" + for i in {1..10}; do + [ "$1" = "" ] && l=$(shuf -i 8-44 -n 1) + head -n10 /dev/urandom | tr -dc _A-Z-a-z-0-9 | cut -c-${1:-$l}; + done | column + echo "" + + echo "Passwords with special characters:" + for i in {1..6}; do + [ "$1" = "" ] && l=$(shuf -i 16-64 -n 1) + first=$(head -n10 /dev/urandom | tr -dc A-Za-z | cut -c-1) + words=$(head -n10 /dev/urandom | tr -dc '!#$%&/()=?+-_,.;:<>[]{}|\@*^A-Z-a-z-0-9' | cut -c-${1:-$l}) + last=$(head -n10 /dev/urandom | tr -dc A-Za-z | cut -c-1) + echo "${first}${words}${last}" + done | column + + if [ -r "${MAKEPASS_WORDLIST}" ]; then + echo "" + echo "Passphrases:" + + for i in {1..5}; do + words=$(shuf -n 8 "${MAKEPASS_WORDLIST}" | tr '\n' '-' | tr -dc '_A-Z-a-z-0-9') + echo "${words:0:-1}" + done; + fi +} + +makepass "${@:-}" + +## END OF FILE ################################################################# +# vim:syntax=sh filetype=sh diff --git a/bin/bin/makepass.sh b/bin/bin/makepass.sh new file mode 100755 index 0000000..0877cc9 --- /dev/null +++ b/bin/bin/makepass.sh @@ -0,0 +1,110 @@ +#!/usr/bin/env sh +# Filename: ~/bin/makepass.sh +# Purpose: Creating random passwords. +# Authors: Dennis Eriksen <d@ennis.no> +# Bug-Reports: Email <idgatt@dnns.no> +# License: This file is licensed under the GPL v2. +################################################################################ +# This file takes randomness from /dev/urandom and turns it into random +# passwords. +################################################################################ + + +# Copyright (c) 2018-2022 Dennis Eriksen • d@ennis.no + + +# 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() { + RANDOM=0 + while [ ! "$RANDOM" = "${RANDOM#0}" ]; do + RANDOM=$(< /dev/urandom tr -cd "[:digit:]" | head -c 8) + done + printf '%s\n' "$RANDOM" + 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="$(< /dev/urandom tr -cd '[:alpha:]' | head -c $len)" + else + string="$(< /dev/urandom tr -cd '[:alpha:]' | head -c 1)" + string="${string}$(< /dev/urandom tr -cd "$chars" | head -c $((len-2)))" + string="${string}$(< /dev/urandom tr -cd '[:alpha:]' | head -c 1)" + fi + else + string="$(< /dev/urandom tr -cd "$chars" | head -c $len)" + fi + + printf '%s\n' "$string" + unset len chars fla string + return 0 +) + +# makepass-function +makepass() { + len=$1 + MAKEPASS_WORDLIST=${MAKEPASS_WORDLIST:-/usr/share/dict/words} + + printf '%s\n' 'Normal passwords:' + i=0 + while [ $i -lt 10 ]; do + _random "${len:-}" '_A-Z-a-z-0-9' true + i=$((i + 1)) + done | column + printf '\n' + + printf '%s\n' 'Passwords with special characters:' + i=0 + while [ $i -lt 6 ]; do + _random "${len:-}" '!#$%&/()=?+-_,.;:<>[]{}|\@*^A-Z-a-z-0-9' true + i=$((i + 1)) + done | column + + if [ -r "${MAKEPASS_WORDLIST}" ]; then + printf '\n' + printf '%s\n' 'Passphrases:' + lines=$(wc -l < ${MAKEPASS_WORDLIST}) + i=0 + while [ $i -lt 5 ]; do + # shuf is the best solution here, but it is very much not portable. + #words=$(shuf -n 8 "${MAKEPASS_WORDLIST}" | tr '\n' '-' | tr -dc '_A-Z-a-z-0-9') + words="" + j=0 + while [ $j -lt 8 ]; do + words="${words}-$(sed -n $(($(_RANDOM) % $lines + 1))p "${MAKEPASS_WORDLIST}" | tr -dc '_A-Z-a-z-0-9')" + j=$((j + 1)) + done + printf '%s\n' "${words#-}" + i=$((i + 1)) + done + fi + + unset len i lines j words + return 0 +} + +makepass "${@:-}" && return 0 + +## END OF FILE ################################################################# diff --git a/bin/bin/makepass.zsh b/bin/bin/makepass.zsh new file mode 100755 index 0000000..0579503 --- /dev/null +++ b/bin/bin/makepass.zsh @@ -0,0 +1,88 @@ +#!/usr/bin/env zsh +emulate -L zsh +# Filename: ~/bin/makepass.zsh +# Purpose: Creating random passwords. +# Authors: Dennis Eriksen <d@ennis.no> +# Bug-Reports: Email <idgatt@dnns.no> +# License: This file is licensed under the GPL v2. +################################################################################ +# This file takes randomness from /dev/urandom and turns it into random +# passwords. +################################################################################ + + +# Copyright (c) 2018-2022 Dennis Eriksen • d@ennis.no + + +# Start with 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 + +# Function to create random stuff +function _random() { + setopt local_options + RANDOM=$(< /dev/urandom tr -cd "[:digit:]" | head -c 8) # seed RANDOM + + # Default is a number between 8 and 44 + local -i len=$1 + (( len == 0 )) && len=$((RANDOM % (44 - 8 + 1) + 8)) + + # Default to [:alnum:] if no chars are specified + local chars=${2:-'[:alnum:]'} + + # First-Last-Alpha - if you want the first and the last letter to be from [:alpha:] + local fla=${3:-'false'} + + local string + + if [[ $fla == true ]]; then + if (( len <= 2 )); then + string="$(< /dev/urandom tr -cd '[:alpha:]' | head -c $len)" + else + string="$(< /dev/urandom tr -cd '[:alpha:]' | head -c 1)" + string+="$(</dev/urandom tr -cd "$chars" | head -c $((len-2)))" + string+="$(</dev/urandom tr -cd '[:alpha:]' | head -c 1)" + fi + else + string="$(< /dev/urandom tr -cd "$chars" | head -c $len)" + fi + + 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=$(< /dev/urandom tr -cd '[:digit:]' | head -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 ################################################################# |