From 3faba161e798db3c2925105c0eeeaecabf1063d6 Mon Sep 17 00:00:00 2001
From: Guillaume Rousse <guillaume.rousse@renater.fr>
Date: Tue, 13 Feb 2018 16:34:15 +0100
Subject: [PATCH] more readability

---
 lib/AccountManager/Tools.pm | 56 ++++++++++++-------------------------
 1 file changed, 18 insertions(+), 38 deletions(-)

diff --git a/lib/AccountManager/Tools.pm b/lib/AccountManager/Tools.pm
index 5b73f59..5e14851 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
-- 
GitLab