{"id":2515,"date":"2018-11-22T14:17:41","date_gmt":"2018-11-22T19:17:41","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=2515"},"modified":"2020-09-25T11:17:45","modified_gmt":"2020-09-25T16:17:45","slug":"php-how-to-use-password_hash-and-password_verify","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2018\/11\/22\/php-how-to-use-password_hash-and-password_verify\/","title":{"rendered":"PHP: How to use password_hash and password_verify"},"content":{"rendered":"<p>In this article I will describe how to use two PHP functions, <code>password_hash<\/code> and <code>password_verify<\/code>, that are important for website login pages that use a user name and password.<\/p>\n<h4><strong>1. password_hash()<\/strong><\/h4>\n<p>Here is what the PHP documentation says about <code>password_hash<\/code>:<\/p>\n<blockquote>\n<p>password_hash() creates a new password hash using a strong one-way hashing algorithm. password_hash() is compatible with crypt(). Therefore, password hashes created by crypt() can be used with password_hash().<\/p>\n<\/blockquote>\n<p>What is a password hash? Here is how Wired describes a hash:<\/p>\n<blockquote>\n<p>A hash is designed to act as a &#8220;one-way function&#8221;: A mathematical operation that&#8217;s easy to perform, but very difficult to reverse. Like other forms of encryption, it turns readable data into a scrambled cipher. But instead of allowing someone to decrypt that data with a specific key, as typical encryption functions do, hashes aren&#8217;t designed to be decrypted. Instead, when you enter your password on a website, it simply performs the same hash again and checks the results against the hash it created of your password when you chose it, verifying the password&#8217;s validity without having to store the sensitive password itself.<\/p>\n<p><a href=\"https:\/\/www.wired.com\/2016\/06\/hacker-lexicon-password-hashing\/\">https:\/\/www.wired.com\/2016\/06\/hacker-lexicon-password-hashing\/<\/a><\/p>\n<\/blockquote>\n<p><code>password_hash()<\/code> uses two arguments, password and hash algorithm:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">password_hash( $password, PASSWORD_DEFAULT )<\/pre>\n<p>Example of usage:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">echo password_hash(\"rasmuslerdorf\", PASSWORD_BCRYPT);<\/pre>\n<p>Here is what usage looks like in PHP\/PDO in combination with use of prepared statements:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3241\" src=\"http:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-10.png\" alt=\"\" width=\"1716\" height=\"465\" srcset=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-10.png 1716w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-10-300x81.png 300w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-10-1024x277.png 1024w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-10-768x208.png 768w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-10-1536x416.png 1536w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-10-676x183.png 676w\" sizes=\"auto, (max-width: 1716px) 100vw, 1716px\" \/><\/p>\n<p>And this is what the resulting password looks like in the MySQL database:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone  wp-image-3243\" src=\"http:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-11.png\" alt=\"\" width=\"458\" height=\"79\" srcset=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-11.png 655w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-11-300x52.png 300w\" sizes=\"auto, (max-width: 458px) 100vw, 458px\" \/><\/p>\n<h4><strong>2. password_verify()<\/strong><\/h4>\n<p>The <code>password_verify()<\/code> function v<span class=\"dc-title\">erifies that a password matches a hash. It is a PHP boolean function that returns true if the password matches the hash, or false if it doesn&#8217;t.<br \/><\/span><\/p>\n<p>Here is what the PHP documentation says about password_verify:<\/p>\n<blockquote>\n<p>Verifies that the given hash matches the given password.<\/p>\n<p>Note that password_hash() returns the algorithm, cost and salt as part of the returned hash. Therefore, all information that&#8217;s needed to verify the hash is included in it. This allows the verify function to verify the hash without needing separate storage for the salt or algorithm information.<\/p>\n<p>This function is safe against timing attacks.<\/p>\n<\/blockquote>\n<p>Syntax:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">password_verify ( $password , $hash )<\/pre>\n<p>Example of usage:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">&lt;?php\n\/\/ See the password_hash() example to see where this came from.\n$hash = '$2y$07$BCryptRequires22Chrcte\/VlQH0piJtjXl.0t1XkA8pw9dMXTpOq';\n\nif (password_verify('rasmuslerdorf', $hash)) {\n    echo 'Password is valid!';\n} else {\n    echo 'Invalid password.';\n}\n?&gt;\n<\/pre>\n<p>password_hash is used to create the hash, and password_verify uses the hash every time your website needs to verify a user login.<\/p>\n<p>For more information about password_hash, see:<br \/><a href=\"https:\/\/secure.php.net\/manual\/en\/function.password-hash.php\">https:\/\/secure.php.net\/manual\/en\/function.password-hash.php<\/a><\/p>\n<p>For more information about password_verify, see:<br \/><a href=\"https:\/\/secure.php.net\/manual\/en\/function.password-verify.php\">https:\/\/secure.php.net\/manual\/en\/function.password-verify.php<\/a><\/p>\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article I will describe how to use two PHP functions, password_hash and password_verify, that are important for website login pages that use a user name and password. 1. password_hash() Here is what the PHP documentation says about password_hash: password_hash() creates a new password hash using a strong one-way hashing algorithm. password_hash() is compatible &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2018\/11\/22\/php-how-to-use-password_hash-and-password_verify\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">PHP: How to use password_hash and password_verify<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[97],"tags":[189,188,69],"class_list":["post-2515","post","type-post","status-publish","format-standard","hentry","category-php-language","tag-password_hash","tag-password_verify","tag-php"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/2515","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/comments?post=2515"}],"version-history":[{"count":17,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/2515\/revisions"}],"predecessor-version":[{"id":3244,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/2515\/revisions\/3244"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=2515"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=2515"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=2515"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}