<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/security.database.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'fr',
  ),
  'this' => 
  array (
    0 => 'security.database.storage.php',
    1 => 'Mod&egrave;le de stockage avec chiffrement',
    2 => 'Mod&egrave;le de stockage avec chiffrement',
  ),
  'up' => 
  array (
    0 => 'security.database.php',
    1 => 'S&eacute;curit&eacute; des bases de donn&eacute;es',
  ),
  'prev' => 
  array (
    0 => 'security.database.connection.php',
    1 => 'Connexions au serveur de base de donn&eacute;es',
  ),
  'next' => 
  array (
    0 => 'security.database.sql-injection.php',
    1 => 'Injection SQL',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'fr',
    'path' => 'security/database.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="security.database.storage" class="sect1">
  <h2 class="title">Modèle de stockage avec chiffrement</h2>
  <p class="simpara">
   Les protocoles SSL/SSH protègent les données qui circulent entre
   le serveur et le client : SSL/SSH ne protège pas les données
   une fois dans la base. SSL est un protocole en ligne.
  </p>
  <p class="simpara">
   Une fois que le pirate a obtenu l&#039;accès direct à la base de données
   (en contournant le serveur web), les données sensibles, stockées dans la
   base sont accessibles directement, à moins que les données de la base
   ne soient protégées par la base. Chiffrer les données est une bonne
   solution pour réduire cette menace, mais très peu de bases de données
   offrent ce type de chiffrement.
  </p>
  <p class="simpara">
   Le moyen le plus simple pour contourner ce problème est de créer un
   logiciel de chiffrement propre, et de l&#039;utiliser dans les scripts PHP.
   <abbr title="PHP: Hypertext Preprocessor">PHP</abbr> peut aider dans cette tâche grâce aux nombreuses extensions
   dont il dispose, comme
   <a href="book.openssl.php" class="link">OpenSSL</a> et
   <a href="book.sodium.php" class="link">Sodium</a>, qui connaissent un large éventail
   de méthodes de chiffrement. Le script PHP va chiffrer les données qui
   seront stockées, et les déchiffrer lorsqu&#039;elles seront relues. Voir la
   suite pour des exemples d&#039;utilisation de ce chiffrement.
  </p>
  
  <div class="sect2" id="security.database.storage.hashing">
   <h3 class="title">Hachage</h3>
   <p class="simpara">
    Dans le cas de données vraiment sensibles, si la représentation originale
    n&#039;est pas nécessaire (pour affichage, ou comparaison), utiliser un
    hachage est une bonne solution. L&#039;exemple classique est le stockage de
    mots de passe dans les bases de données, après les avoir passés par
    un hachage cryptographique.
   </p>
   <p class="simpara">
    Les fonctions <a href="ref.password.php" class="link">password</a>
    fournissent une bonne façon de hacher les données sensibles et de travailler avec ces empreintes.
   </p>
   <p class="simpara">
    La fonction <span class="function"><a href="function.password-hash.php" class="function">password_hash()</a></span> est utilisée pour hacher une chaîne donnée
    en utilisant l&#039;algorithme le plus fort actuellement disponible et la fonction
    <span class="function"><a href="function.password-verify.php" class="function">password_verify()</a></span> vérifie si le mot de passe fourni correspond au
    hachage stocké en base de données.
   </p>
   <div class="example" id="example-1">
    <p><strong>Exemple #1 Hacher un champ mot de passe</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #FF8000">// stockage du hash du mot de passe<br /></span><span style="color: #0000BB">$query  </span><span style="color: #007700">= </span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #DD0000">"INSERT INTO users(name,pwd) VALUES('%s','%s');"</span><span style="color: #007700">,<br />            </span><span style="color: #0000BB">pg_escape_string</span><span style="color: #007700">(</span><span style="color: #0000BB">$username</span><span style="color: #007700">),<br />            </span><span style="color: #0000BB">password_hash</span><span style="color: #007700">(</span><span style="color: #0000BB">$password</span><span style="color: #007700">, </span><span style="color: #0000BB">PASSWORD_DEFAULT</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">pg_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$connection</span><span style="color: #007700">, </span><span style="color: #0000BB">$query</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// on vérifie si l'utilisateur a soumis le bon mot de passe<br /></span><span style="color: #0000BB">$query </span><span style="color: #007700">= </span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #DD0000">"SELECT pwd FROM users WHERE name='%s';"</span><span style="color: #007700">,<br />            </span><span style="color: #0000BB">pg_escape_string</span><span style="color: #007700">(</span><span style="color: #0000BB">$username</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">$row </span><span style="color: #007700">= </span><span style="color: #0000BB">pg_fetch_assoc</span><span style="color: #007700">(</span><span style="color: #0000BB">pg_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$connection</span><span style="color: #007700">, </span><span style="color: #0000BB">$query</span><span style="color: #007700">));<br /><br />if (</span><span style="color: #0000BB">$row </span><span style="color: #007700">&amp;&amp; </span><span style="color: #0000BB">password_verify</span><span style="color: #007700">(</span><span style="color: #0000BB">$password</span><span style="color: #007700">, </span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">'pwd'</span><span style="color: #007700">])) {<br />    echo </span><span style="color: #DD0000">'Bonjour, ' </span><span style="color: #007700">. </span><span style="color: #0000BB">htmlspecialchars</span><span style="color: #007700">(</span><span style="color: #0000BB">$username</span><span style="color: #007700">) . </span><span style="color: #DD0000">'!'</span><span style="color: #007700">;<br />} else {<br />    echo </span><span style="color: #DD0000">'L\'authentification a échoué pour ' </span><span style="color: #007700">. </span><span style="color: #0000BB">htmlspecialchars</span><span style="color: #007700">(</span><span style="color: #0000BB">$username</span><span style="color: #007700">) . </span><span style="color: #DD0000">'.'</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
      </div>

   </div>
  </div>
 </div><?php manual_footer($setup); ?>