I was just reading a post to create a login system and while reading I saw about password hashing.
We are going to store an sha256 hash which is a string always containing 64 characters.
This article is teaching to use sha256 hash function. After reading this, I did not stop and started to search more about creating secure login system and then I came up with this article by wikiHow. In this article, they are using sha512 hash function. This really confused me that which function am I going to use for my next login system. I still searched google for better article and found this article by crackstation. The writer recommends both sha256 and sha512.
Only cryptographic hash functions may be used to implement password hashing. Hash functions like SHA256, SHA512, RipeMD, and WHIRLPOOL are cryptographic hash functions.
I thought my search is over and I can use sha256
or sha512
function but while searching more I found this SO Question. The accepted answer by Robert K had new things for me. The things about I have never heard before which is bcrypt and scrypt.
All this stuff was written about 2 to 4 years ago.
Question
Which is the best password hashing algorithm used these days for PHP?
The main thing here is that you want to choose a hash that is slow. The only feasible attack vector for any of these hashes is brute forcing. Meaning, an attacker can only try all possible passwords one after the other, hashing them using the same algorithm as you did and compare them to the hash. The longer this takes for one password the more infeasible it is to find a match.
The SHA family of algorithms is designed to be fast, because they're not designed to be used for this purpose. As such they are by themselves somewhat unsuitable for password hashing; though they can be used as part of an algorithm which makes them suitable, such as PBKDF2 (which in short repeats the hashing many thousands of times to stretch it).
bcrypt and scrypt are explicitly designed to be slow and are as such much more suitable for password hashing. bcrypt is designed to be very expensive in terms of CPU power, while scrypt is designed to be very expensive in terms of memory consumption. CPU power is better scalable using todays hardware than memory is, so scrypt is currently seen as the best thing to use. Though it is very cutting edge at the moment and has seen little support in terms of usable code. bcrypt on the other hand is supported by PHP using
password_hash
directly.