Questions tagged [php-password-hash]

Password hashing API to simplify creating and managing passwords in a way that they cannot be easily reversed. Replaces SHA1 and MD5 insecure hashing. Available natively with PHP 5.5.0+ or via an external compatibility package in PHP 5.3.7 and higher.

password_hash(), a function, was born out of a need to easily secure passwords in a way that the hash would not easily be broken, should an attacker obtain a table containing hashed passwords.
While its underlying hashing algorithm was readily available through the PHP crypt() function, pulling together all the necessary pieces to make this function work was beyond most programmers. As a result, many new programmers would turn to the insecure and hashing systems that have been around for a long time.

Password hash simply provides a wrapper for crypt(), using BCRYPT. The system generates a random salt by default (recommended) and returns a hash that contains the salt, the cost and the hashed password. Passing that string back into password_verify(), along with the plain-text password, will tell you if they match.

$pass = 'password';
$hash = password_hash($pass, PASSWORD_DEFAULT);
// $hash now contains a string that looks like
// $2y$10$WNxIiMP4o7EevZKNL01T4uwux1TvOhFRj31XVfG7lQV7HhVY5tsIK

$verify = password_verify($pass, $hash);
var_dump($verify); // bool(true)

Password Hash is native to PHP 5.5.0 or later, however, a compatible library exists for PHP 5.3.7 or later, that functions in the same way.

161 questions
142
votes
4 answers

How to use PHP's password_hash to hash and verify passwords

Recently I have been trying to implement my own security on a log in script I stumbled upon on the internet. After struggling of trying to learn how to make my own script to generate a salt for each user, I stumbled upon password_hash. From what I…
Josh Potter
  • 1,629
  • 2
  • 13
  • 11
36
votes
2 answers

How do I use the Argon2 algorithm with password_hash?

So I heard that PHP 7.2 introduced the new Argon2 algorithm. But I'm confused on how I can use it with my existing code. For instance, I have this $password = password_hash('somepassword', PASSWORD_DEFAULT, ['cost' => 12]); Does PASSWORD_DEFAULT…
Machavity
  • 30,841
  • 27
  • 92
  • 100
34
votes
4 answers

Using PHP 5.5's password_hash and password_verify function

Say I wanted to store a password for a user, would this be the right way to do it with PHP 5.5's password_hash() function (or this version for PHP 5.3.7+: https://github.com/ircmaxell/password_compat)? $options = array("cost" => 10, "salt" =>…
Doug Smith
  • 29,668
  • 57
  • 204
  • 388
32
votes
2 answers

Verify password hash in nodejs which was generated in php

My php code generates a hash using password_hash which I store in a database. Below is the PHP code: $hash = password_hash($value, PASSWORD_BCRYPT, array('cost' => $cost)); I would like to verify / check the password against this hash in nodejs. I…
Sudesh
  • 1,129
  • 3
  • 14
  • 29
28
votes
1 answer

password_hash returns different value every time

I'm making a login system, and I want to hash the passwords to make them more secure, but it returns a different hash every time, and can't even be verified using password_verify(), here is my code: $password = password_hash($password4,…
ItzBenteThePig
  • 696
  • 1
  • 9
  • 19
25
votes
3 answers

PASSWORD_DEFAULT vs PASSWORD_BCRYPT

What is the difference between PASSWORD_DEFAULT and PASSWORD_BCRYPT? Do they both use Blowfish encryption algorithm? What is cost in an algorithm? How to set up password_hash in PHP produce a 255-hash length instead of 60?
rexhin
  • 409
  • 1
  • 4
  • 11
23
votes
1 answer

Call to undefined function password_hash()

I am running php version 5.4.16 on localhost right now, while I am developing my site. I want to use password_hash(), but I keep getting this error: Fatal error: Call to undefined function password_hash() in /dir/to/file.php on line 123 Why is…
18
votes
2 answers

Generating Password Hash In PHP 5.5 And Setting Cost Option

I know PHP 5.5 is in alpha but this class I am making is just being made in advance to utilize it's hashing feature by using function_exists(). I checked out the password_hash documentation. The 3rd argument is for $options which currently supports…
kittycat
  • 14,983
  • 9
  • 55
  • 80
17
votes
1 answer

Argon2 Algorithm in PHP7: understanding the time_cost parameter

I'm trying to implement the Argon2 algorithm in an authentification library. I want to be able to provide some useful tips for the users to set the parameters. While I understand how memory_cost and threads parameters affect the algorithm, I can't…
Indigo
  • 745
  • 5
  • 16
17
votes
3 answers

php password_hash and password_verify issues no match

I am trying out a new function from PHP 5.5 called password_hash(). No matter what i do the $hash and the $password wont match. $password = "test"; $hash = "$2y$10$fXJEsC0zWAR2tDrmlJgSaecbKyiEOK9GDCRKDReYM8gH2bG2mbO4e"; if…
Daniel
  • 2,002
  • 5
  • 20
  • 32
14
votes
2 answers

Password does not match after being encrypted using crypt() and password_hash() function

I modified my old post. I tried the crypt() function and now trying to work with password_hash() and password_verify() to verify the encrypted password coming from database but on each call, password_hash() function retuns a different encrypted…
Hunza Ali
  • 189
  • 1
  • 3
  • 10
14
votes
2 answers

what is an alternative to password_hash() for (PHP 5 < 5.5.0)?

According to manual: password_hash this function can be used for (PHP 5 >= 5.5.0) After searching for an alternative way I found this simple function from here: http://www.sitepoint.com/password-hashing-in-php/ function generateHash($password) { …
Mark
  • 163
  • 1
  • 1
  • 6
13
votes
2 answers

What will happen if they changed PASSWORD_DEFAULT in PHP Password library?

Consider this line of code by using PHP: $password = password_hash($password, PASSWORD_DEFAULT); What will happen if they changed the default password hashing algorithm? I mean I will be having hashed password inside the database. Then, from my own…
Ambitions
  • 2,369
  • 3
  • 13
  • 24
12
votes
3 answers

Understanding how salt is generated/used in bcrypt password_hash

I am working on an existing Symfony 2.8 web app project that uses FOSUserBundle for user authentication. In addition to the web front end the users can use different smartphone client to connect to the web app using a REST API. Thus the users need…
Andrei Herford
  • 17,570
  • 19
  • 91
  • 225
12
votes
3 answers

password_compat for older php version

PHP 5.5 will support the password functions password_hash() and password_verify(). Also there is a file to add this functions in >5.3.7. My problem is that I am currently running the debian package 5.3.3-7+squeeze14. Is there a way to make this…
PiTheNumber
  • 22,828
  • 17
  • 107
  • 180
1
2 3
10 11