#!/usr/bin/env bash # Filename: makepass.bash # Purpose: Creating random passwords. # Authors: Dennis Eriksen # Bug-Reports: Email # License: This file is licensed under the BSD 3-Clause license. ################################################################################ # This file takes randomness from /dev/urandom and turns it into random # passwords. ################################################################################ # Copyright (c) 2018-2023 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 (( l <= 1 || l > 255 )) && echo "Argument must be between 0 and 255" && return 1 # if $1 is actually empty, set $l to random value for each output echo "Normal passwords:" for _ 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 _ 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 _ 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 #################################################################