Password Security in PHP password hashing detailed

Source: Internet
Author: User
Tags rehash
This article mainly introduces the password security in PHP password hashing detailed, interested in the friend's reference, I hope to be helpful to everyone.

If you are still using MD5 encryption, it is recommended to look at the password encryption and authentication methods below.

Let's look at a simple password hashing example:

<?php//require ' password.php ';/** * The correct password is Secret-password * $passwordHash is the password stored after the hash * password_verify () Used to compare the password entered by the user with the password stored by the database. Success returns TRUE, otherwise false */$passwordHash = Password_hash (' Secret-password ', password_default); Echo $passwordHash; if ( Password_verify (' Bad-password ', $passwordHash)) {  //Correct password  echo ' Correct password ';} else {  echo ' wrong password ';  Wrong password}

The code below provides a complete simulated user class in which the user's password can be handled securely and the security requirements of the future will be changed by using password Hashing.

<?phpclass user{//Store password options so rehash & hash can share them:const hash = Password_default; Const COST = 14;//can determine how complex the algorithm should be, and then determine how long it will take to generate the hash value. (this value is treated as the number of times the algorithm itself is rerun to slow down the calculation.)  )//Internal data storage about the User:public $data;    Mock constructor:public function __construct () {///Read data from the database, storing it into $data such as:    $data->passwordhash and $data->username $this->data = new StdClass ();  $this->data->passwordhash = ' dbd014125a4bad51db85f27279f1040a '; }//Mock Save functionality Public Function Save () {//Store the data from $data to the The database}//Allo W for changing a new Password:public function SetPassword ($password) {$this->data->passwordhash = Password_has  H ($password, Self::hash, [' cost ' = Self::cost]);    }//Logic for logging a user In:public function login ($password) {//First see if they gave the right password: echo "Login:", $this->data->passworDhash, "\ n"; if (Password_verify ($password, $this->data->passwordhash)) {//Success-now see if their password needs Rehas  Hed if (Password_needs_rehash ($this->data->passwordhash, Self::hash, [' cost ' = = Self::cost])) {//We Need to rehash the password, and save it.        Just Call SetPassword $this->setpassword ($password);      $this->save (); } return true;    Need to mark the user as logged in.  } return false; }}

The above is the whole content of this article, I hope that everyone's study has helped.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.