I'm new to password_hash
and password_verify
, and they appear to be the most efficient way of storing passwords securely!
I noticed that password_hash
produces different hash for the same plain-text
value every time
!
This means that if a user tried to create an account with the password (thisIsMyPassword)
it will generate a hash like this $2y$10$VCNH8ndve8hwbvLJ2nMHtOsEiigE4zA7ViADxCJfq9bmUCmkNkcce
,
And if another or the same user tried to create another account with the same password i.e. (thisIsMyPassword)
the account will be created and the hash value of the password will be something like $2y$10$Hqssc5nn3pzgfwqVwQrQz.Ny71q972RXmCmyV9ykywG8iELbsf47a
!
Now you see the same value i.e. (thisIsMyPassword)
resulted in different hashes!
Is this OK?
Is it OK to let the users use same passwords, as long as the password hash is different in the database?
The password hash includes a so-called salt, a small random value, which is here to prevent dictionary attacks, here is what PHP manual says:
If omitted, a random salt will be generated by password_hash() for each password hashed. This is the intended mode of operation.
The value you get as the output, is not really a plain hash, but a string made of - algorithm id, salt and HASH(password,salt).
The used algorithm, cost and salt are returned as part of the hash. Therefore, all information that's needed to verify the hash is included. in it. This allows the password_verify() function to verify the hash without needing separate storage for the salt or algorithm information.