Les caractères de ponctuation sont: ! " # $ % & ' ( ) * + , - . / : ; < = > @ [ \ ] ^ _ ` { | } ~
La non-confusion retire de la liste les caractères suivants: o O 0 u U u v V j J 1 I l i | ! 5 s S
Le générateur aléatoire est : mt_rand()
<?php
/**
 * @file generer_password.php
 * @brief Scripts d'images aléatoires au format JPG
 *
 * @author hughes monget
 * @see http://monget.com/
 */
define('longueur', 8);
define('nombre', 1);
define('minuscule', 1);
$arr_minuscule = range('a', 'z');
define('majuscule', 1);
$arr_majuscule = range('A', 'Z');
define('chiffre', 1);
$arr_chiffre = range('0', '9');
define('ponctuation', 0);
//$arr_ponctuation = str_split('&#{}[]/*-+%?./§,;:!|');
$arr_ponctuation = str_split('!"#$%&\'()*+,-./:;<=>@[\]^_`{|}~'); // ASCII-128
define('non_confusion', 0);
$arr_str_caractere_confus = str_split('oO0uUuvVjJ1Ili|!5sS');
define('pas_de_doublon', 1);
$str_type_nombre    = array('nombre', 'longueur');
$str_type_caractere = array('minuscule', 'majuscule', 'chiffre', 'ponctuation');
$str_type_modifieur = array('non_confusion');
$arr_liste_caractere = array();
if (isset($_REQUEST['generer']))
{
    foreach ($str_type_caractere as $str_paramhttp)
    {
        $$str_paramhttp = isset($_REQUEST[$str_paramhttp]);
        if ($$str_paramhttp)
        {
            $arr_liste_caractere = array_merge($arr_liste_caractere, ${'arr_'.$str_paramhttp});
        }
    }
    foreach ($str_type_modifieur as $str_paramhttp)
    {
        $$str_paramhttp = isset($_REQUEST[$str_paramhttp]);
    }
    foreach ($str_type_nombre as $str_paramhttp)
    {
        $$str_paramhttp = 0;
        if (isset($_REQUEST[$str_paramhttp]) && ($tmp = intval(trim($_REQUEST[$str_paramhttp]))) > 0)
        {
            $$str_paramhttp = $tmp;
        }
    }
}
else
{
    foreach ($str_type_caractere as $str_paramhttp)
    {
        ${$str_paramhttp} = constant($str_paramhttp);
    }
    foreach ($str_type_modifieur as $str_paramhttp)
    {
        ${$str_paramhttp} = constant($str_paramhttp);
    }
    foreach ($str_type_nombre as $str_paramhttp)
    {
        ${$str_paramhttp} = constant($str_paramhttp);
    }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr">
    <head>
        <title>Génération de password</title>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
        <style type="text/css">
            <!--
            input.nombre
            {
                width: 20px;
            }
            .separateur
            {
                background: #ccc;
                font-size: 0;
            }
            textarea, table, td
            {
                font-family: monospace;
                font-size: 16px;
            }
            table.indice, td.indice
            {
                margin: 0;
                padding: 0;
                border-collapse: collapse;
                border: 0 none;
                vertical-align: top;
            }
        -->
        </style>
    </head>
    <body>
        <form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>">
            <?php echo '<input type="hidden" name="cache" value="'.md5(uniqid(mt_rand(), 1)).'" />'; ?>
            <table>
<?php
foreach ($str_type_caractere as $str_paramhttp)
{
    $str_cheched = ($$str_paramhttp) ? ' checked="checked"' : '';
    echo
    '
        <tr>
            <td><label for="',$str_paramhttp,'">',ucfirst($str_paramhttp),'</label></td>
            <td><input type="checkbox" id="',$str_paramhttp,'" name="',$str_paramhttp,'" class="checkbox" ',$str_cheched,'/></td>
        </tr>
    ';
}
echo '<tr><td colspan="2" class="separateur"> </td></tr>';
foreach ($str_type_modifieur as $str_paramhttp)
{
    $str_cheched = ($$str_paramhttp) ? ' checked="checked"' : '';
    $str_libelle = ucfirst($str_paramhttp);
    $str_libelle = str_replace('_', ' ', $str_libelle);
    echo
    '
        <tr>
            <td><label for="',$str_paramhttp,'">',$str_libelle,'</label></td>
            <td><input type="checkbox" id="',$str_paramhttp,'" name="',$str_paramhttp,'" class="checkbox" ',$str_cheched,'/></td>
        </tr>
    ';
}
echo '<tr><td colspan="2" class="separateur"> </td></tr>';
foreach ($str_type_nombre as $str_paramhttp)
{
    echo
    '
        <tr>
            <td><label for="',$str_paramhttp,'">',ucfirst($str_paramhttp),'</label></td>
            <td><input class="nombre" type="text" id="',$str_paramhttp,'" name="',$str_paramhttp,'" value="',$$str_paramhttp,'" /></td>
        </tr>
    ';
}
?>
                <tr><td colspan="2" class="separateur"> </td></tr>
                <tr>
                    <td colspan="2">
                        <input type="submit" name="generer" value="Generer" />
                        <input type="reset" value="Reset" />
                    </td>
                </tr>
            </table>
        </form>
<?php
function tester_password($mdp)
{
    $longueur = strlen($mdp);
    $point = $point_min = $point_maj = $point_chiffre = $point_caracteres = 0;
    for($i = 0; $i < $longueur; $i++)
    {
        $lettre = $mdp[$i];
        if ($lettre >= 'a' && $lettre <= 'z')
        {
            $point += 1;
            $point_min = 1;
        }
        else if ($lettre >= 'A' && $lettre <= 'Z')
        {
            $point += 2;
            $point_maj = 2;
        }
        else if ($lettre >= '0' && $lettre <= '9')
        {
            $point += 3;
            $point_chiffre = 3;
        }
        else
        {
            $point += 5;
            $point_caracteres = 5;
        }
    }
    // Calcul du coefficient points/longueur
    $etape1 = $point / $longueur;
    // Calcul du coefficient de la diversité des types de caractères...
    $etape2 = $point_min + $point_maj + $point_chiffre + $point_caracteres;
    // Multiplication du coefficient de diversité avec celui de la longueur
    $resultat = $etape1 * $etape2;
    // Multiplication du résultat par la longueur de la chaîne
    $final = $resultat * $longueur;
    return $final;
}
if (isset($_REQUEST['generer']))
{
    $arr_str_message = array();
    if (!$arr_liste_caractere)
    {
        $arr_str_message[] = 'Saisir un type de caractères !';
    }
    foreach ($str_type_nombre as $str_paramhttp)
    {
        if (!$$str_paramhttp)
        {
            $arr_str_message[] = 'Saisir le champ '.$str_paramhttp.' !';
        }
    }
    if ($arr_str_message)
    {
        echo '<p>',implode('<br />', $arr_str_message),'</p>';
    }
    else
    {
        // Retrait des caractères ressemblants.
        if ($non_confusion)
        {
            foreach ($arr_liste_caractere as $key => $str_caractere)
            {
                if (in_array($str_caractere, $arr_str_caractere_confus))
                {
                    unset($arr_liste_caractere[$key]);
                }
            }
            $arr_liste_caractere = array_values($arr_liste_caractere);
        }
        // Parcours des passwords.
        $int_longueur_liste = count($arr_liste_caractere);
        $arr_str_password = array();
        $int_entropie = intval(floor(log($int_longueur_liste) * $longueur / log(2)));
        for ($ii = 0; $ii < $nombre; $ii++)
        {
            // Génération d'un password.
            $str_password = '';
            for ($jj = 0; $jj < $longueur; $jj++)
            {
                $str_password .= $arr_liste_caractere[mt_rand() % $int_longueur_liste];
            }
            $arr_str_password[] = $str_password;
        }
        echo '<table class="indice">';
        echo '<tr><td> </td><td>Complexité</td></tr>';
        echo '<tr><td class="indice">';
        echo '<textarea rows="',($nombre + 1),'" cols="',($longueur + 2),'">',htmlspecialchars(implode("\n", $arr_str_password)),'</textarea>';
        echo '</td><td class="indice">';
        echo '<table class="indice">';
        foreach ($arr_str_password as $str_password)
        {
            echo '<tr><td class="indice">',tester_password($str_password),'</td></tr>';
        }
        echo '</table>';
        echo '</tr></td></table>';
        echo '<p>Entropie: ',$int_entropie,' bits (log(',$int_longueur_liste,') * ',$longueur,' / log(2))</p>';
    }
}
echo
    '<p>',
    'Les caractères de ponctuation sont: '.htmlspecialchars(implode(' ',$arr_ponctuation)).'<br />',
    'La non-confusion retire de la liste les caractères suivants: '.htmlspecialchars(implode(' ',$arr_str_caractere_confus)),'<br />',
    'Le générateur aléatoire est : <a href="http://php.net/manual/fr/function.mt-rand.php" target="_blank">mt_rand()</a>',
    '</p>';
echo '<hr />';
highlight_file(__FILE__);
?>
    </body>
</html>