When storing passwords, should I use PHP's built in crypt
or phpass?
If using crypt
, should I use CRYPT_SHA512
or CRYPT_BLOWFISH
?
When storing passwords, should I use PHP's built in crypt
or phpass?
If using crypt
, should I use CRYPT_SHA512
or CRYPT_BLOWFISH
?
For storing passwords, use PHPass
example of code using phpass:
require('PasswordHash.php');
$pwdHasher = new PasswordHash(8, FALSE);
// $hash is what you would store in your database
$hash = $pwdHasher->HashPassword( $password );
// $hash would be the $hashed stored in your database for this user
$checked = $pwdHasher->CheckPassword($password, $hash);
if ($checked) {
echo 'password correct';
} else {
echo 'wrong credentials';
}
internally, PHPass uses PHP's crypt()
function, but all the nasty details have already been solved.
Currently, using CRYPT_BLOWFISH is the best practice. CRYPT_BLOWFISH in PHP is an implementation of the Bcrypt hash. Bcrypt is based on the Blowfish block cipher, making use of it's expensive key setup to slow the algorithm down.
If you use PHPass as in the above example and PHP >= 5.3, you will be using BCrypt.
The answer is dependent on what do you want to achieve. Different methods are for different situations. There is another good library called PHP Secure Communications Library which you can probably use (read Who should use phpseclib?).
phpass is limited by the fact that it's trying to be PHP4 compliant. Quoting from the source code:
# We're kind of forced to use MD5 here since it's the only
# cryptographic primitive available in all versions of PHP
# currently in use. To implement our own low-level crypto
# in PHP would result in much worse performance and
# consequently in lower iteration counts and hashes that are
# quicker to crack (by non-PHP code).
MD5 is totally insecure. Is your code base going to support PHP4? If not I'd suggest just using PHP's built in hash() function and pass to it 'sha512' or something.
HashPassword
uses bcrypt when it's available, and only uses MD5 as a last resort. Also, when they use MD5, they're not just using straight MD5, they're using MD5 as the hash function in a salted variable-iteration password hashing function. Using just SHA512 would be significantly worse (and even replacing MD5 with SHA512 in their function would make very little difference).
Commented
Jul 16, 2012 at 16:04