From 56d2e34ef431b255ffceea1cf5ffa505a14dfaee Mon Sep 17 00:00:00 2001 From: fatalerrors Date: Tue, 8 Nov 2022 16:30:46 +0100 Subject: [PATCH] added password generator --- profile.d/pwd.sh | 144 +++++++++++++++++++++++++++++++++++++++++++++++ profile.sh | 3 +- 2 files changed, 146 insertions(+), 1 deletion(-) create mode 100755 profile.d/pwd.sh diff --git a/profile.d/pwd.sh b/profile.d/pwd.sh new file mode 100755 index 0000000..b51133a --- /dev/null +++ b/profile.d/pwd.sh @@ -0,0 +1,144 @@ +# ------------------------------------------------------------------------------ +# genpwd : generate a password with different criteria +# default 16 car with up and low car, symbol and number +# The function is very slow on Windows +# ------------------------------------------------------------------------------ +genpwd() +{ + local length=16 + local occurs=2 # Bug, if set to 1, seems to be ignored + local symb=1 maj=1 min=1 numb=1 + local nbpwd=1 + + for opt in $@; do + case $opt in + "-h"|"--help") + echo "genpwd: generate a secure random password." + echo + echo "Usage: genpwd [options] [--extracars=] [--length=] [nb_passwd]" + echo + echo "Options:" + echo " -h, --help Display that help screen" + echo " -s, --nosymbols Exclude symbols" + echo " -n, --nonumbers Exclude numbers" + echo " -u, --noup Exclude uppercase letters" + echo " -l, --nolow Exclude lowercase letters" + echo " -e=, --extracars=" + echo " Add the given caracters to the possible caracter list" + echo " -L=, --length=" + echo " Set length of the password (default is $length)" + echo " -o=, --occurences=" + echo " Set the maximum occurences of a same caracter (default is $occurs)" + echo + echo "If the --extracars parameter is given, at least one of the given caracter will" + echo "be used in the final password." + echo + echo "Please note that some caracters might be interpreted by Bash or Awk programs," + echo "and thus, cannot be used without provoquing errors. Those identified caracters" + echo "are :" + echo ' * ? \ $ { }' + echo + return 0 + ;; + "-s"|"--nosymbols") + symb=0 + ;; + "-n"|"--nonumbers") + numb=0 + ;; + "-u"|"--noup") + maj=0 + ;; + "-l"|"--nolow") + min=0 + ;; + "-e"?*|"--extracars"?*) + local extcar=$(echo "$opt" | cut -f 2- -d '=') + ;; + "-L"?*|"--length"?*) + local length=$(echo "$opt" | cut -f 2- -d '=') + if ! [[ $length =~ ^[0-9]+$ ]]; then + echo "The --length parameter requires a number." + return 1 + fi + ;; + "-o"?*|"--occurences"?*) + local occurs=$(echo "$opt" | cut -f 2- -d '=') + if ! [[ $occurs =~ ^[1-9]+$ ]]; then + echo "The --occurs parameter requires a number from 1 to 9." + return 1 + fi + ;; + "-*") + echo "Unknow parameter ${opt}." + return 1 + ;; + *) + if ! [[ $opt =~ ^[1-9]+$ ]]; then + echo "Unknow parameter ${opt}." + return 1 + else + local nbpwd=$opt + fi + ;; + esac + done + + # Function selecting a random caracter from the list in parameter + pickcar() + { + # When a character is picked we check if it's not appearing already twice + # elsewhere, we choose an other char, to compensate weak bash randomizer + while [[ -z $char ]]; do + local char=$(echo ${1:RANDOM%${#1}:1} $RANDOM) + if [[ $(awk -F"$char" '{print NF-1}' <<< "$picked") -gt $occurs ]]; then + unset char + fi + done + picked+="$char" + echo "$char" + } + + echo "Generating $nbpwd passwords, please wait..." + for n in $( seq 1 $nbpwd ); do + { + local carset='' # store final caracter set to use + local picked='' # store already used caracter + local rlength=0 # store already assigned length of caracters + + # ?, *, $ and \ impossible to use to my knowledge as it would be interpreted + if [[ $symb == 1 ]]; then + pickcar '!.@#&%/^-_' + carset+='!.@#&%/^-_' + (( rlength++ )) + fi + if [[ $numb == 1 ]]; then + pickcar '0123456789' + carset+='0123456789' + (( rlength++ )) + fi + if [[ $maj == 1 ]]; then + pickcar 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + carset+='ABCDEFGHIJKLMNOPQRSTUVWXYZ' + (( rlength++ )) + fi + if [[ $min == 1 ]]; then + pickcar 'abcdefghijklmnopqrstuvwxyz' + carset+='abcdefghijklmnopqrstuvwxyz' + (( rlength++ )) + fi + if [[ -n $extcar ]]; then + pickcar "$extcar" + carset+=$extcar + (( rlength++ )) + fi + + for i in $( seq 1 $(( $length - $rlength )) ); do + pickcar "$carset" + done + } | sort -R | awk '{printf "%s", $1}' + unset picked carset rlength + echo + done +} +export -f genpwd diff --git a/profile.sh b/profile.sh index 1b14691..3d6a57b 100644 --- a/profile.sh +++ b/profile.sh @@ -34,6 +34,7 @@ # 29/07/2022 v2.8.2 : added warning for non bash users # 27/08/2022 v3.0.0 : splitted everything, added rain screensaver # 07/11/2022 v3.0.1 : added concatenation to rmspc, added ku, error managed in meteo +# 08/11/2022 v3.1.0 : added password generator # ------------------------------------------------------------------------------ # Copyright (c) 2013-2022 Geoffray Levasseur # Protected by the BSD3 license. Please read bellow for details. @@ -69,7 +70,7 @@ # * OF SUCH DAMAGE. # ------------------------------------------------------------------------------ -export PROFVERSION="3.0.1" +export PROFVERSION="3.1.0" export DEFAULT_CITY="Toulouse"