diff --git a/lib/AccountManager/Tools.pm b/lib/AccountManager/Tools.pm index 5b73f594a80388b5bdb7a4679f9888dba8dcd617..5e148515fd5a6870e39044952685c099b2a6a54c 100644 --- a/lib/AccountManager/Tools.pm +++ b/lib/AccountManager/Tools.pm @@ -7,6 +7,7 @@ use Digest::SHA; use Digest::MD5; use Encode; use English qw(-no_match_vars); +use List::Util qw(shuffle); use List::MoreUtils qw(pairwise); use MIME::Base64; use Template; @@ -49,49 +50,28 @@ sub sha256_hash { # This function generates a random password sub generate_password { - my $length_of_randomstring = 10; # the length of - # the random string to generate - - # plusieurs tirages : - # 1-tirage des caractères obligatoires : les mettre dans un tableau - my @uppers = ('A' .. 'N', 'P' .. 'Z'); - my @lowers = ('a' .. 'k', 'm' .. 'z'); - my @punctuation = (':', '!', '?', '&', '$', '=', '-', '#'); - my @numerics = ('0' .. '9'); - my @rndtab; - push(@rndtab, $uppers[ rand @uppers ]); - push(@rndtab, $lowers[ rand @lowers ]); - push(@rndtab, $punctuation[ rand @punctuation ]); - - ## Pas de caractères 8bit pour l'antispam - push(@rndtab, $numerics[ rand @numerics ]); - - # 2-tirage des caractères optionnels : les ajouter au tableau + my $size = 10; + + my @uppers = ('A' .. 'N', 'P' .. 'Z'); + my @lowers = ('a' .. 'k', 'm' .. 'z'); + my @punctuations = (':', '!', '?', '&', '$', '=', '-', '#'); + my @numerics = ('0' .. '9'); + my @all = (@uppers, @lowers, @punctuations, @numerics); + + # start with a random character of each class my @chars = ( - 'a' .. 'k', 'm' .. 'z', 'A' .. 'N', 'P' .. 'Z', - '0' .. '9', '_', '%', ';', - ':', '!', '?', '&', - '$', '*', '(', ')', - '{, }', '[', ']', - '.', '=', '-', '#' + $uppers[ rand @uppers ], + $lowers[ rand @lowers ], + $punctuations[ rand @punctuations ], + $numerics[ rand @numerics ] ); - foreach (5 .. $length_of_randomstring) { - # rand @chars will generate a random - # number between 0 and scalar @chars - push(@rndtab, $chars[ rand @chars ]); + # complete with additional characters + for my $i (1 .. $size - 4) { + push(@chars, $all[ rand @all ]); } -# 3-ordonnancement de ceux-ci : les retirer aléatoirement du tableau en les concaténant dans une chaîne - my $rndstring = ''; - my $cpt = 1; - while ($cpt <= $length_of_randomstring) { - my $indice = rand @rndtab; - $rndstring .= $rndtab[$indice]; - splice(@rndtab, $indice, 1); - $cpt += 1; - } - return $rndstring; + return join('', shuffle(@chars)); } # ID is based on time + PID