ajout de genpass pour générer un mot de passe en shell
This commit is contained in:
parent
c957cc3cdb
commit
08aaabd37e
105
ugenpass
105
ugenpass
|
@ -1,6 +1,6 @@
|
|||
#!/bin/bash
|
||||
# -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
|
||||
source "$(dirname "$0")/ulib/ulib" || exit 1; urequire DEFAULTS
|
||||
source "$(dirname "$0")/ulib/ulib" || exit 1; urequire DEFAULTS password
|
||||
|
||||
function display_help() {
|
||||
uecho "$scriptname: générer un mot de passe au hasard
|
||||
|
@ -20,34 +20,6 @@ OPTIONS
|
|||
Spécifier le nombre minimum de chaque classe de caractère"
|
||||
}
|
||||
|
||||
UPPER="AZERTYUIOPQSDFGHJKLMWXCVBN"
|
||||
LOWER="azertyuiopqsdfghjklmwxcvbn"
|
||||
NUMBERS="1234567890"
|
||||
SYMBOLS="&\"'(-_)=^\$*!:;,?./%+#{[|\\]}"
|
||||
LEN=10
|
||||
SECTIONS=("$UPPER" "$LOWER" "$UPPER$LOWER" "$NUMBERS" "$SYMBOLS" "$NUMBERS$SYMBOLS")
|
||||
COUNTS=(0 0 4 0 0 1)
|
||||
|
||||
function random_index() {
|
||||
[ -n "$1" ] || return
|
||||
local -a __rn_array; array_copy __rn_array "$1"
|
||||
[ ${#__rn_array[*]} -gt 0 ] || return
|
||||
echo $(($RANDOM % ${#__rn_array[*]}))
|
||||
}
|
||||
|
||||
function random_value() {
|
||||
local __rv_index="$(random_index "$1")"
|
||||
[ -n "$__rv_index" ] || return
|
||||
eval "echo \${$1[$__rv_index]}"
|
||||
}
|
||||
|
||||
function random_char() {
|
||||
local section="$1"
|
||||
[ ${#section} -gt 0 ] || return
|
||||
local index=$(($RANDOM % ${#section}))
|
||||
echo "${section:$index:1}"
|
||||
}
|
||||
|
||||
len=
|
||||
upper=
|
||||
lower=
|
||||
|
@ -66,69 +38,14 @@ parse_opts "${PRETTYOPTS[@]}" \
|
|||
-B:,--special: special= \
|
||||
@ args -- "$@" && set -- "${args[@]}" || die "$args"
|
||||
|
||||
[ -n "$len" ] || len="$LEN"
|
||||
[ -n "$upper" ] || upper="${COUNTS[0]}"
|
||||
[ -n "$lower" ] || lower="${COUNTS[1]}"
|
||||
[ -n "$alpha" ] || alpha="${COUNTS[2]}"
|
||||
[ -n "$number" ] || number="${COUNTS[3]}"
|
||||
[ -n "$symbol" ] || symbol="${COUNTS[4]}"
|
||||
[ -n "$special" ] || special="${COUNTS[5]}"
|
||||
[ -n "$len" ] || len="$GENPASS_LEN"
|
||||
[ -n "$upper" ] || upper="${GENPASS_COUNTS[0]}"
|
||||
[ -n "$lower" ] || lower="${GENPASS_COUNTS[1]}"
|
||||
[ -n "$alpha" ] || alpha="${GENPASS_COUNTS[2]}"
|
||||
[ -n "$number" ] || number="${GENPASS_COUNTS[3]}"
|
||||
[ -n "$symbol" ] || symbol="${GENPASS_COUNTS[4]}"
|
||||
[ -n "$special" ] || special="${GENPASS_COUNTS[5]}"
|
||||
|
||||
# counts contient le nombre minimum de caractères de chaque section. ce tableau
|
||||
# doit être de la même taille que le tableau SECTIONS
|
||||
counts=("$upper" "$lower" "$alpha" "$number" "$symbol" "$special")
|
||||
[ "${#SECTIONS[*]}" -eq "${#counts[*]}" ] || die "SECTIONS et counts doivent être de même taille"
|
||||
|
||||
# indexes contient les indexes dans le tableau SECTIONS, avec comme poids le
|
||||
# nombre de caractères minimum de chaque sections
|
||||
indexes=()
|
||||
index=0
|
||||
for count in "${counts[@]}"; do
|
||||
while [ $count -ge 0 ]; do
|
||||
array_add indexes "$index"
|
||||
count=$(($count - 1))
|
||||
done
|
||||
index=$(($index + 1))
|
||||
done
|
||||
|
||||
# générer chaque caractère du mot de passe au hasard
|
||||
parts=()
|
||||
while true; do
|
||||
# vérifier s'il reste encore des sections avec un nombre minimum
|
||||
hasmincount=
|
||||
for mincount in "${counts[@]}"; do
|
||||
[ "$mincount" -gt 0 ] && {
|
||||
hasmincount=1
|
||||
break
|
||||
}
|
||||
done
|
||||
[ -n "$hasmincount" ] || break
|
||||
# calculer la prochaine section à générer
|
||||
while true; do
|
||||
index=$(random_index SECTIONS)
|
||||
[ ${counts[$index]} -gt 0 ] && break
|
||||
done
|
||||
# générer un caractère au hasard
|
||||
part="$(random_char "${SECTIONS[$index]}")"
|
||||
array_add parts "$part"
|
||||
counts[$index]=$((${counts[$index]} - 1))
|
||||
done
|
||||
# générer le reste du mot de passe
|
||||
while [ "${#parts[*]}" -lt "$len" ]; do
|
||||
index=$(random_value indexes)
|
||||
part="$(random_char "${SECTIONS[$index]}")"
|
||||
array_add parts "$part"
|
||||
done
|
||||
|
||||
# puis générer le mot de passe en prenant chaquer partie au hasard
|
||||
password=
|
||||
while [ ${#parts[*]} -gt 0 ]; do
|
||||
index=$(random_index parts)
|
||||
password="$password${parts[$index]}"
|
||||
before=()
|
||||
[ $index -gt 0 ] && before=("${parts[@]:0:$index}")
|
||||
after=("${parts[@]:$((index + 1))}")
|
||||
parts=("${before[@]}" "${after[@]}")
|
||||
done
|
||||
|
||||
echo "$password"
|
||||
GENPASS_LEN="$len"
|
||||
GENPASS_COUNTS=("$upper" "$lower" "$alpha" "$number" "$symbol" "$special")
|
||||
genpass
|
||||
|
|
|
@ -1 +1 @@
|
|||
23
|
||||
24
|
||||
|
|
|
@ -0,0 +1,99 @@
|
|||
##@cooked comments # -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
|
||||
## Gestion de mots de passe
|
||||
##@cooked nocomments
|
||||
##@require base
|
||||
uprovide password
|
||||
urequire base
|
||||
|
||||
function random_index() {
|
||||
# Afficher un index au hasard dans le tableau $1
|
||||
[ -n "$1" ] || return
|
||||
local -a __rn_array; array_copy __rn_array "$1"
|
||||
[ ${#__rn_array[*]} -gt 0 ] || return
|
||||
echo $(($RANDOM % ${#__rn_array[*]}))
|
||||
}
|
||||
|
||||
function random_value() {
|
||||
# Afficher une valeur au hasard dans le tableau $1
|
||||
local __rv_index="$(random_index "$1")"
|
||||
[ -n "$__rv_index" ] || return
|
||||
eval "echo \${$1[$__rv_index]}"
|
||||
}
|
||||
|
||||
function random_char() {
|
||||
# Afficher un caractère au hasard dans la chaine $1
|
||||
local section="$1"
|
||||
[ ${#section} -gt 0 ] || return
|
||||
local index=$(($RANDOM % ${#section}))
|
||||
echo "${section:$index:1}"
|
||||
}
|
||||
|
||||
GENPASS_UPPER="AZERTYUIOPQSDFGHJKLMWXCVBN"
|
||||
GENPASS_LOWER="azertyuiopqsdfghjklmwxcvbn"
|
||||
GENPASS_NUMBERS="1234567890"
|
||||
GENPASS_SYMBOLS="&\"'(-_)=^\$*!:;,?./%+#{[|\\]}"
|
||||
GENPASS_LEN=10
|
||||
GENPASS_SECTIONS=("$GENPASS_UPPER" "$GENPASS_LOWER" "$GENPASS_UPPER$GENPASS_LOWER" "$GENPASS_NUMBERS" "$GENPASS_SYMBOLS" "$GENPASS_NUMBERS$GENPASS_SYMBOLS")
|
||||
GENPASS_COUNTS=(0 0 4 0 0 1)
|
||||
|
||||
function genpass() {
|
||||
# Générer un mot de passe au hasard avec les paramètres GENPASS_*
|
||||
local -a indexes parts
|
||||
local count index hascount part password
|
||||
|
||||
[ "${#GENPASS_SECTIONS[*]}" -eq "${#GENPASS_COUNTS[*]}" ] || die "GENPASS_COUNTS et GENPASS_SECTIONS doivent être de même taille"
|
||||
|
||||
# indexes contient les indexes dans le tableau SECTIONS, avec comme poids le
|
||||
# nombre de caractères minimum de chaque sections
|
||||
indexes=()
|
||||
index=0
|
||||
for count in "${GENPASS_COUNTS[@]}"; do
|
||||
while [ $count -ge 0 ]; do
|
||||
array_add indexes "$index"
|
||||
count=$(($count - 1))
|
||||
done
|
||||
index=$(($index + 1))
|
||||
done
|
||||
|
||||
# générer chaque caractère du mot de passe au hasard
|
||||
parts=()
|
||||
while true; do
|
||||
# vérifier s'il reste encore des sections avec un nombre minimum
|
||||
hascount=
|
||||
for count in "${GENPASS_COUNTS[@]}"; do
|
||||
[ "$count" -gt 0 ] && {
|
||||
hascount=1
|
||||
break
|
||||
}
|
||||
done
|
||||
[ -n "$hascount" ] || break
|
||||
# calculer la prochaine section à générer
|
||||
while true; do
|
||||
index=$(random_index GENPASS_SECTIONS)
|
||||
[ ${GENPASS_COUNTS[$index]} -gt 0 ] && break
|
||||
done
|
||||
# générer un caractère au hasard
|
||||
part="$(random_char "${GENPASS_SECTIONS[$index]}")"
|
||||
array_add parts "$part"
|
||||
GENPASS_COUNTS[$index]=$((${GENPASS_COUNTS[$index]} - 1))
|
||||
done
|
||||
# générer les autres caractères jusqu'à GENPASS_LEN
|
||||
while [ "${#parts[*]}" -lt "$GENPASS_LEN" ]; do
|
||||
index=$(random_value indexes)
|
||||
part="$(random_char "${GENPASS_SECTIONS[$index]}")"
|
||||
array_add parts "$part"
|
||||
done
|
||||
|
||||
# puis générer le mot de passe en prenant chaque partie au hasard
|
||||
password=
|
||||
while [ ${#parts[*]} -gt 0 ]; do
|
||||
index=$(random_index parts)
|
||||
password="$password${parts[$index]}"
|
||||
before=()
|
||||
[ $index -gt 0 ] && before=("${parts[@]:0:$index}")
|
||||
after=("${parts[@]:$((index + 1))}")
|
||||
parts=("${before[@]}" "${after[@]}")
|
||||
done
|
||||
|
||||
echo "$password"
|
||||
}
|
Loading…
Reference in New Issue