svn: /phpdoc/en/trunk/security/ database.xml
| From: | Maciej Sobaczewski | Date: | Wed, 16 Sep 2015 06:24:41 +0000 |
| Subject: | svn: /phpdoc/en/trunk/security/ database.xml | ||
| Groups: | php.doc.cvs | ||
| Request: | Send a blank email to doc-cvs+get-13731@lists.php.net to get a copy of this message | ||
sobak Wed, 16 Sep 2015 06:24:41 +0000
Revision: http://svn.php.net/viewvc?view=revision&revision=337837
Log:
Close bug #69416 (Use the password API on the db security page)
Bug: https://bugs.php.net/69416 (Open) Use the password API
on the db security page
Changed paths:
U phpdoc/en/trunk/security/database.xml
Modified: phpdoc/en/trunk/security/database.xml
===================================================================
--- phpdoc/en/trunk/security/database.xml 2015-09-15 22:42:57 UTC (rev 337836)
+++ phpdoc/en/trunk/security/database.xml 2015-09-16 06:24:41 UTC (rev 337837)
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
-<!-- splitted from ./index.xml, last change in rev 1.66 -->
<chapter xml:id="security.database" xmlns="http://docbook.org/ns/docbook"
xmlns:xlink="http://www.w3.org/1999/xlink">
<title>Database Security</title>
@@ -106,20 +105,64 @@
it when retrieving. See the references for further examples of how
encryption works.
</simpara>
- <simpara>
- In the case of truly hidden data, if its raw representation is not needed
- (i.e. will not be displayed), hashing may also be taken into consideration.
- The well-known example for hashing is storing the cryptographic hash of a
- password in a database, instead of the password itself. See also
- <function>crypt</function>.
- </simpara>
- <example>
- <title>Using hashed password field</title>
- <programlisting role="php">
+
+ <sect2 xml:id="security.database.storage.hashing">
+ <title>Hashing</title>
+ <simpara>
+ In the case of truly hidden data, if its raw representation is not needed
+ (i.e. will not be displayed), hashing should be taken into consideration.
+ The well-known example for hashing is storing the cryptographic hash of a
+ password in a database, instead of the password itself.
+ </simpara>
+ <simpara>
+ In PHP 5.5 or newer <link linkend="ref.password">password</link>
functions
+ provide convinient way to hash sensitive data and work with these hashes.
+ In PHP 5.3.7+ <link xmlns:xlink="http://www.w3.org/1999/xlink"
xlink:href="&url.password.compat;">
+ password_compat</link> library can also be used.
+ </simpara>
+ <simpara>
+ <function>password_hash</function> is used to hash given string using
+ strongest algorithm currently available and <function>password_verify</function>
+ checks whether given password matches the hash stored in database.
+ </simpara>
+ <example>
+ <title>Hashing password field</title>
+ <programlisting role="php">
<![CDATA[
<?php
// storing password hash
+$query = sprintf("INSERT INTO users(name,pwd) VALUES('%s','%s');",
+ pg_escape_string($username),
+ password_hash($password, PASSWORD_DEFAULT));
+$result = pg_query($connection, $query);
+
+// querying if user submitted the right password
+$query = sprintf("SELECT pwd FROM users WHERE name='%s';",
+ pg_escape_string($username));
+$row = pg_fetch_assoc(pg_query($connection, $query));
+
+if ($row && password_verify($password, $row['pwd'])) {
+ echo 'Welcome, ' . htmlspecialchars($username) . '!';
+} else {
+ echo 'Authentication failed for ' . htmlspecialchars($username) . '.';
+}
+
+?>
+]]>
+ </programlisting>
+ </example>
+ <simpara>
+ In older versions of PHP this can be achieved using <function>crypt</function>
+ function.
+ </simpara>
+ <example>
+ <title>Hashing password using <function>crypt</function>s</title>
+ <programlisting role="php">
+<![CDATA[
+<?php
+
+// storing password hash
// $random_chars retrieved e.g. using /dev/random
$query = sprintf("INSERT INTO users(name,pwd) VALUES('%s','%s');",
pg_escape_string($username),
@@ -139,8 +182,9 @@
?>
]]>
- </programlisting>
- </example>
+ </programlisting>
+ </example>
+ </sect2>
</sect1>
<sect1 xml:id="security.database.sql-injection">