Connexion
S'inscrire
Actualités
Quoi de neuf
Activités générales
Auteurs
Forums
Nouveaux messages
Rechercher un forum
Quoi de neuf
Nouveaux messages
Nouveaux messages de profil
Activités générales
Membres
Membres inscrits
Visiteurs actuels
Nouveaux messages de profil
Rechercher dans les messages des profils
Teams
Créé ton équipe
Quoi de neuf ?
Nouveaux messages
Rechercher un forum
Menu
Connexion
S'inscrire
Install the app
Install
Forums
Forums généraux
Informatique
Développement
Sécurisez vos mots de passe en PHP
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
Vous utilisez un navigateur non à jour ou ancien. Il ne peut pas afficher ce site ou d'autres sites correctement.
Vous devez le mettre à jour ou utiliser un
navigateur alternatif
.
Répondre à la discussion
Message
<blockquote data-quote="Snake's" data-source="post: 5984866" data-attributes="member: 109105"><p style="text-align: center"><span style="color: #b30000"><strong><strong>[FA]fa-key fa-border fa-3x[/FA]</strong></strong></span></p> <p style="text-align: center"><span style="color: #b30000"><strong>[FA]fa-lock[/FA] <span style="font-family: 'Open Sans Condensed'">SÉCURISEZ VOS MOTS DE PASSE EN PHP</span></strong></span></p> <p style="text-align: center"><strong></strong></p> <p style="text-align: center"><strong></strong></p> <p style="text-align: center"><strong>[ATTACH=full]74543[/ATTACH] </strong></p> <p style="text-align: center"><strong></strong></p> <p style="text-align: center"></p><p>Salut à tous, <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite391" alt=":membre:" title="Membre :membre:" loading="lazy" data-shortname=":membre:" /></p><p></p><p>Aujourd'hui nous allons voir comment <strong><span style="color: #b30000">sécuriser vos mots </span></strong>de passe en PHP.</p><p style="text-align: center"></p> <p style="text-align: center"></p> <p style="text-align: center">[ATTACH=full]74543[/ATTACH]</p> <p style="text-align: center"></p> <p style="text-align: center"></p> <p style="text-align: center"><strong><span style="color: #b30000">[FA]fa-list-alt fa-border fa-3x[/FA]</span></strong></p> <p style="text-align: center"><strong><span style="color: #b30000"><span style="font-family: 'Open Sans Condensed'">INTRODUCTION</span></span></strong></p> <p style="text-align: center"><strong></strong></p> <p style="text-align: center"><strong></strong></p><p>Il est évident que vous n'allez pas stocker les mots de passe de vos utilisateurs tels quels, il faut les <strong><span style="color: #b30000">crypter</span></strong>, ou bien les <strong><span style="color: #b30000">hasher</span></strong>.</p><p></p><p><strong><span style="color: #404040">[fa]fa-info-circle fa-lg fa-border pull-left fa-stack-3x[/fa]</span></strong> <u>Note :</u> <em>Veuillez à ne pas confondre <strong>cryptage </strong>et <strong>hashage</strong>. Le cryptage est réversible, le hashage ne l'est pas.</em></p><p></p><p></p><p style="text-align: center">[ATTACH=full]74543[/ATTACH] </p> <p style="text-align: center"></p> <p style="text-align: center"></p> <p style="text-align: center"></p> <p style="text-align: center"></p> <p style="text-align: center"><strong><span style="color: #b30000">[FA]fa-lock fa-border fa-3x[/FA]</span></strong></p> <p style="text-align: center"><strong><span style="color: #b30000"><span style="font-family: 'Open Sans Condensed'"> [fa]fa-key[/fa] HASHER UN MOT DE PASSE</span></span></strong></p> <p style="text-align: center"><strong></strong></p> <p style="text-align: center"><strong></strong></p><p></p><p>Pour <strong><span style="color: #b30000">hasher </span></strong>un mot de passe, c'est très simple.</p><p>PHP propose une fonction nommée <strong><span style="color: #b30000">password_hash </span></strong>pour hasher des chaînes de caractères.</p><p></p><p><u>Exemple :</u></p><p></p><p>[PHP]$pass = 'RealityGaming' ;</p><p>$hash = password_hash($pass,PASSWORD_BCRYPT);[/PHP]</p><p></p><p>Le résultat de ma variable <strong><span style="color: #b30000">$hash </span></strong>ne sera <u>jamais</u> le même résultat.</p><p><span style="font-size: 15px"></span></p><p><span style="font-size: 15px"><strong><span style="color: #b30000">PASSWORD_BCRYPT </span></strong>est une <strong><span style="color: #b30000">constante</span></strong>, il y a une autre constante qui est : <strong><span style="color: #b30000">PASSWORD_DEFAULT</span></strong>, mais celle-ci <u>n'est pas recommandée</u> <em>(elle fait référence à l'algorithme par défaut de PHP)</em>.</span></p><p><span style="font-size: 15px">Une liste des constantes disponibles se trouve <a href="http://php.net/manual/fr/password.constants.php" target="_blank">ici</a>. </span></p><p><span style="font-size: 15px"></span></p><p><span style="font-size: 15px">La fonction <strong><span style="color: #b30000">password_hash </span></strong>possède un <strong><span style="color: #b30000">troisième argument</span></strong>, celui de spécifier des options pour l'algorithme. </span></p><p><span style="font-size: 15px"></span></p><p><span style="font-size: 15px">Nous l'exprimerons comme ceci :</span></p><p>[PHP]$pass = 'RealityGaming' ;</p><p>$hash = password_hash($pass,PASSWORD_BCRYPT,['cost' => 10]);[/PHP]</p><p></p><p>Dans cet exemple on augmente le <strong><span style="color: #b30000">coût </span></strong>(cost) de l'algorithme le rendant par la même occasion plus lente et donc plus dure à <em>« brute-forcer »</em>.</p><p></p><p>Vous pouvez déterminer le <em>« </em><strong><span style="color: #b30000">coût idéal</span> </strong><em>» </em>grâce à une fonction trouvée sur le net.</p><p>[PHP]function getOptimalCost($timeTarget)</p><p>{</p><p> $cost = 9;</p><p> do {</p><p> $cost++;</p><p> $start = microtime(true);</p><p> password_hash("RealityGaming", PASSWORD_BCRYPT, ["cost" => $cost]);</p><p> $end = microtime(true);</p><p> } while (($end - $start) < $timeTarget);</p><p></p><p> return $cost;</p><p>}</p><p></p><p>echo getOptimalCost(0.3);[/PHP]</p><p>Personnellement j'obtiens un résultat de <strong><span style="color: #b30000">11</span></strong>.</p><p></p><p>Il existe également l'option <em>« </em><span style="color: #b30000"><strong>sel </strong></span><em>»</em> (salt), qui permet de générer ses propres grains de sel.</p><p>Je ne conseille pas d'utiliser cette fonction, car mal utilisée elle pourrait réduire la sécurité du hash.</p><p></p><p>Voici un <strong><span style="color: #b30000">schéma </span></strong>expliquant ce que je viens de dire. <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite344" alt=":nerd:" title="Intello :nerd:" loading="lazy" data-shortname=":nerd:" /></p><p></p><p style="text-align: center">[ATTACH=full]74544[/ATTACH] </p> <p style="text-align: center"></p> <p style="text-align: center"></p> <p style="text-align: center">[ATTACH=full]74543[/ATTACH] </p> <p style="text-align: center"></p> <p style="text-align: center"></p> <p style="text-align: center"></p> <p style="text-align: center"></p> <p style="text-align: center"><strong><span style="color: #b30000">[FA]fa-clone fa-border fa-3x[/FA]</span></strong></p> <p style="text-align: center"><strong><span style="color: #b30000"><span style="font-family: 'Open Sans Condensed'"> [fa]fa-copy[/fa] COMPARER UN MOT DE PASSE ET UN HASH</span></span></strong></p> <p style="text-align: center"><strong></strong></p> <p style="text-align: center"><strong></strong></p><p><span style="font-size: 15px"></span></p><p><span style="font-size: 15px">Comme dit plus haut, <strong><span style="color: #b30000">le résultat d'un hash n'est jamais le même</span></strong>, alors comment comparer ce résultat avec le mot de passe initial saisit par l'utilisateur ? <img src="/images/smiley-pack/etonne.gif" class="smilie" loading="lazy" alt=":shock:" title="Etonné :shock:" data-shortname=":shock:" /></span></p><p><span style="font-size: 15px"></span></p><p><span style="font-size: 15px">Heureusement pour nous, PHP possède une fonction nommée </span><strong><span style="font-size: 15px"><span style="color: #b30000">password_verify</span></span></strong><span style="font-size: 15px">.</span></p><p></p><p><span style="font-size: 15px">Nous l'utiliserons comme ceci : </span></p><p><span style="font-size: 15px">[PHP]if(password_verify('RealityGaming', '$2y$10$nQWnPp/DAAz0Z41/zydo1.9pIKqQMmvGXj9XagxA0lIngdZ29eauW')) echo 'OK';</span></p><p><span style="font-size: 15px">else echo 'ERREUR';[/PHP]</span></p><p></p><p><span style="font-size: 15px">Le résultat est </span><strong><span style="font-size: 15px"><span style="color: #b30000">OK</span></span></strong><span style="font-size: 15px">, car le hash que j'ai fourni est le résultat de : </span></p><p><span style="font-size: 15px">[PHP]echo $hash;[/PHP]</span></p><p></p><p></p><p style="text-align: center">[ATTACH=full]74543[/ATTACH] </p> <p style="text-align: center"></p> <p style="text-align: center"></p> <p style="text-align: center"></p> <p style="text-align: center"><strong><span style="color: #b30000">[FA]fa-eye fa-border fa-3x[/FA]</span></strong></p> <p style="text-align: center"><strong><span style="color: #b30000"><span style="font-family: 'Open Sans Condensed'"> [fa]fa-key[/fa] VÉRIFIER UN HASH</span></span></strong></p> <p style="text-align: center"><strong></strong></p> <p style="text-align: center"><strong></strong></p><p></p><p>Avec le temps, il se peut qu'un hash ne fonctionne plus, car vous avez augmenté le coût par exemple.</p><p>Avec une fonction PHP, vous pouvez avoir toutes les informations sur un hash. Cette fonction se nomme <strong><span style="color: #b30000">password_get_info</span></strong>.</p><p></p><p>Nous l'utilisons comme ceci :</p><p>[PHP]print_r(password_get_info($hash));[/PHP]</p><p></p><p>Cela affichera le type d'algorithme ainsi que les options que vous avez utilisées :</p><p>[PHP] Array</p><p>(</p><p> [algo] => 1</p><p> [algoName] => bcrypt</p><p> [options] => Array</p><p> (</p><p> [cost] => 11</p><p> )</p><p>)[/PHP]</p><p></p><p>C'est pratique d'avoir ces valeurs, mais PHP dispose d'une fonction qui vérifie la validité d'un hash en fonction des options que vous voulez !</p><p>Cette fonction se nomme : <strong><span style="color: #b30000">password_needs_rehash</span></strong>.</p><p></p><p>Voici un <strong><span style="color: #b30000">exemple </span></strong>d'utilisation :</p><p>[PHP] $pass = 'RealityGaming' ;</p><p> $hash = password_hash($pass,PASSWORD_BCRYPT,['cost' => 11]);</p><p> $options = array('cost' => 11);</p><p> if (password_verify($pass, $hash))</p><p> {</p><p> if (password_needs_rehash($hash, PASSWORD_BCRYPT, $options))</p><p> {</p><p> $hash = password_hash($pass, PASSWORD_BCRYPT, $options);</p><p> }</p><p> }[/PHP]</p><p></p><p></p><p></p><p style="text-align: center">[ATTACH=full]74543[/ATTACH] </p> <p style="text-align: center"></p> <p style="text-align: center"></p> <p style="text-align: center"><strong><span style="color: #b30000">[FA]fa-hourglass-end fa-border fa-3x[/FA]</span></strong></p> <p style="text-align: center"><strong><span style="color: #b30000"><span style="font-family: 'Open Sans Condensed'">FIN DU TUTORIEL</span></span></strong></p> <p style="text-align: center"><strong></strong></p> <p style="text-align: center"><strong></strong></p> <p style="text-align: center"></p><p>J'espère que ce tutoriel vous aura plu.</p><p>Vous n'avez plus d'excuses pour stocker vos mots de passes tels quels maintenant, faites attention aux <strong><span style="color: #b30000">données sensibles</span></strong> de vos utilisateurs. <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite351" alt=";)" title="Clin d'oeil ;)" loading="lazy" data-shortname=";)" /></p><p></p><p>PS : Je vous ai présenté uniquement les méthodes basiques, il y a beaucoup d'autres méthodes, comme le <em>sha256, sha1, md5</em> .. <img src="/data/assets/smilies/joueur.gif" class="smilie" loading="lazy" alt=":p" title="Joueur :p" data-shortname=":p" /></p><p></p><p style="text-align: right"><em><a href="https://reality-gaming.fr/teams/realitygaming-tech.2310/" target="_blank">RealityGaming Tech</a></em></p></blockquote><p></p>
[QUOTE="Snake's, post: 5984866, member: 109105"] [CENTER][COLOR=#b30000][B][B][FA]fa-key fa-border fa-3x[/FA][/B][/B] [B][FA]fa-lock[/FA] [FONT=Open Sans Condensed]SÉCURISEZ VOS MOTS DE PASSE EN PHP[/FONT][/B][/COLOR] [B] [ATTACH=full]74543[/ATTACH] [/B] [/CENTER] Salut à tous, :membre: Aujourd'hui nous allons voir comment [B][COLOR=#b30000]sécuriser vos mots [/COLOR][/B]de passe en PHP. [CENTER] [ATTACH=full]74543[/ATTACH] [B][COLOR=#b30000][FA]fa-list-alt fa-border fa-3x[/FA] [FONT=Open Sans Condensed]INTRODUCTION[/FONT][/COLOR] [/B][/CENTER] Il est évident que vous n'allez pas stocker les mots de passe de vos utilisateurs tels quels, il faut les [B][COLOR=#b30000]crypter[/COLOR][/B], ou bien les [B][COLOR=#b30000]hasher[/COLOR][/B]. [B][COLOR=#404040][fa]fa-info-circle fa-lg fa-border pull-left fa-stack-3x[/fa][/COLOR][/B] [U]Note :[/U] [I]Veuillez à ne pas confondre [B]cryptage [/B]et [B]hashage[/B]. Le cryptage est réversible, le hashage ne l'est pas.[/I] [CENTER][ATTACH=full]74543[/ATTACH] [B][COLOR=#b30000][FA]fa-lock fa-border fa-3x[/FA] [FONT=Open Sans Condensed] [fa]fa-key[/fa] HASHER UN MOT DE PASSE[/FONT][/COLOR] [/B][/CENTER] Pour [B][COLOR=#b30000]hasher [/COLOR][/B]un mot de passe, c'est très simple. PHP propose une fonction nommée [B][COLOR=#b30000]password_hash [/COLOR][/B]pour hasher des chaînes de caractères. [U]Exemple :[/U] [PHP]$pass = 'RealityGaming' ; $hash = password_hash($pass,PASSWORD_BCRYPT);[/PHP] Le résultat de ma variable [B][COLOR=#b30000]$hash [/COLOR][/B]ne sera [U]jamais[/U] le même résultat. [SIZE=4] [B][COLOR=#b30000]PASSWORD_BCRYPT [/COLOR][/B]est une [B][COLOR=#b30000]constante[/COLOR][/B], il y a une autre constante qui est : [B][COLOR=#b30000]PASSWORD_DEFAULT[/COLOR][/B], mais celle-ci [U]n'est pas recommandée[/U] [I](elle fait référence à l'algorithme par défaut de PHP)[/I]. Une liste des constantes disponibles se trouve [URL='http://php.net/manual/fr/password.constants.php']ici[/URL]. La fonction [B][COLOR=#b30000]password_hash [/COLOR][/B]possède un [B][COLOR=#b30000]troisième argument[/COLOR][/B], celui de spécifier des options pour l'algorithme. Nous l'exprimerons comme ceci :[/SIZE] [PHP]$pass = 'RealityGaming' ; $hash = password_hash($pass,PASSWORD_BCRYPT,['cost' => 10]);[/PHP] Dans cet exemple on augmente le [B][COLOR=#b30000]coût [/COLOR][/B](cost) de l'algorithme le rendant par la même occasion plus lente et donc plus dure à [I]« brute-forcer »[/I]. Vous pouvez déterminer le [I]« [/I][B][COLOR=#b30000]coût idéal[/COLOR] [/B][I]» [/I]grâce à une fonction trouvée sur le net. [PHP]function getOptimalCost($timeTarget) { $cost = 9; do { $cost++; $start = microtime(true); password_hash("RealityGaming", PASSWORD_BCRYPT, ["cost" => $cost]); $end = microtime(true); } while (($end - $start) < $timeTarget); return $cost; } echo getOptimalCost(0.3);[/PHP] Personnellement j'obtiens un résultat de [B][COLOR=#b30000]11[/COLOR][/B]. Il existe également l'option [I]« [/I][COLOR=#b30000][B]sel [/B][/COLOR][I]»[/I] (salt), qui permet de générer ses propres grains de sel. Je ne conseille pas d'utiliser cette fonction, car mal utilisée elle pourrait réduire la sécurité du hash. Voici un [B][COLOR=#b30000]schéma [/COLOR][/B]expliquant ce que je viens de dire. :nerd: [CENTER][ATTACH=full]74544[/ATTACH] [ATTACH=full]74543[/ATTACH] [B][COLOR=#b30000][FA]fa-clone fa-border fa-3x[/FA] [FONT=Open Sans Condensed] [fa]fa-copy[/fa] COMPARER UN MOT DE PASSE ET UN HASH[/FONT][/COLOR] [/B][/CENTER] [SIZE=4] Comme dit plus haut, [B][COLOR=#b30000]le résultat d'un hash n'est jamais le même[/COLOR][/B], alors comment comparer ce résultat avec le mot de passe initial saisit par l'utilisateur ? :shock: Heureusement pour nous, PHP possède une fonction nommée [/SIZE][B][SIZE=4][COLOR=#b30000]password_verify[/COLOR][/SIZE][/B][SIZE=4].[/SIZE] [SIZE=4]Nous l'utiliserons comme ceci : [PHP]if(password_verify('RealityGaming', '$2y$10$nQWnPp/DAAz0Z41/zydo1.9pIKqQMmvGXj9XagxA0lIngdZ29eauW')) echo 'OK'; else echo 'ERREUR';[/PHP][/SIZE] [SIZE=4]Le résultat est [/SIZE][B][SIZE=4][COLOR=#b30000]OK[/COLOR][/SIZE][/B][SIZE=4], car le hash que j'ai fourni est le résultat de : [PHP]echo $hash;[/PHP][/SIZE] [CENTER][ATTACH=full]74543[/ATTACH] [SIZE=4][/SIZE] [B][COLOR=#b30000][FA]fa-eye fa-border fa-3x[/FA] [FONT=Open Sans Condensed] [fa]fa-key[/fa] VÉRIFIER UN HASH[/FONT][/COLOR] [/B][/CENTER] Avec le temps, il se peut qu'un hash ne fonctionne plus, car vous avez augmenté le coût par exemple. Avec une fonction PHP, vous pouvez avoir toutes les informations sur un hash. Cette fonction se nomme [B][COLOR=#b30000]password_get_info[/COLOR][/B]. Nous l'utilisons comme ceci : [PHP]print_r(password_get_info($hash));[/PHP] Cela affichera le type d'algorithme ainsi que les options que vous avez utilisées : [PHP] Array ( [algo] => 1 [algoName] => bcrypt [options] => Array ( [cost] => 11 ) )[/PHP] C'est pratique d'avoir ces valeurs, mais PHP dispose d'une fonction qui vérifie la validité d'un hash en fonction des options que vous voulez ! Cette fonction se nomme : [B][COLOR=#b30000]password_needs_rehash[/COLOR][/B]. Voici un [B][COLOR=#b30000]exemple [/COLOR][/B]d'utilisation : [PHP] $pass = 'RealityGaming' ; $hash = password_hash($pass,PASSWORD_BCRYPT,['cost' => 11]); $options = array('cost' => 11); if (password_verify($pass, $hash)) { if (password_needs_rehash($hash, PASSWORD_BCRYPT, $options)) { $hash = password_hash($pass, PASSWORD_BCRYPT, $options); } }[/PHP] [CENTER][ATTACH=full]74543[/ATTACH] [B][COLOR=#b30000][FA]fa-hourglass-end fa-border fa-3x[/FA] [FONT=Open Sans Condensed]FIN DU TUTORIEL[/FONT][/COLOR] [/B] [/CENTER] J'espère que ce tutoriel vous aura plu. Vous n'avez plus d'excuses pour stocker vos mots de passes tels quels maintenant, faites attention aux [B][COLOR=#b30000]données sensibles[/COLOR][/B] de vos utilisateurs. ;) PS : Je vous ai présenté uniquement les méthodes basiques, il y a beaucoup d'autres méthodes, comme le [I]sha256, sha1, md5[/I] .. :p [RIGHT][I][URL='https://reality-gaming.fr/teams/realitygaming-tech.2310/']RealityGaming Tech[/URL][/I][/RIGHT] [/QUOTE]
Insérer les citations…
Vérification
Publier la réponse
Forums
Forums généraux
Informatique
Développement
Sécurisez vos mots de passe en PHP
Ce site utilise des cookies. En continuant à utiliser ce site, vous acceptez l'utilisation des cookies.
Acceptez
En savoir plus.…
Haut