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:


$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.

  • 1
    The great thing about phpass is that it is incredibly simple to setup and use. – user10211 Jul 12 '12 at 12:40

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?).

  • 1
    I've been reading through the documentation and concluded this library is totally unsuited for password hashing. Its focus is on encryption. it does feature a crypt_hash method, but only general purpose algorithms (md2, md5, md5-96, sha1, sha1-96, sha256, sha384, and sha512) are available. All in all, I do not see any additional value over PHP's mcrypt extension. – Jacco Nov 15 '12 at 11:26

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.

  • 7
    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). – Brendan Long Jul 16 '12 at 16:04
  • 1
    -1 "(...) to it 'sha512' or something." password hashing is a lot more complicated than just saying 'no' to 1 hash function and loosely hinting that some other hashing algorithm could be better, without any further argumentation. PHPass is widely reviewed and found to be (one of) the best libraries available for PHP. – Jacco Jul 17 '12 at 11:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.