Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the default algorithm in password_hash

Reading the documentation about a new password_hash function for PHP 5.5, I am wondering, what is the default algorithm:

password_hash("rasmuslerdorf", PASSWORD_DEFAULT);

Documentation about it does not clarify this: http://www.php.net/manual/en/password.constants.php

like image 592
Salvador Dali Avatar asked Jun 20 '13 23:06

Salvador Dali


People also ask

What algorithm does Bcryptpasswordencoder use?

The problems present in traditional UNIX password hashes led naturally to a new password scheme which we call bcrypt, referring to the Blowfish encryption algorithm. Bcrypt uses a 128-bit salt and encrypts a 192-bit magic value. It takes advantage of the expensive key setup in eksblowfish.

How does PHP password_hash work?

It is a one-way algorithm, in that you don't decrypt it to validate it, you simply pass the original string in with your password and if it generates the same hash for the provided password, you're authenticated. It's best to omit the salt and let it generate one for you.

What algorithm is used to hash passwords?

Commonly used hashing algorithms include Message Digest (MDx) algorithms, such as MD5, and Secure Hash Algorithms (SHA), such as SHA-1 and the SHA-2 family that includes the widely used SHA-256 algorithm.

How secure is PHP password_hash?

The result hash from password_hash() is secure because: It uses a strong hashing algorithm. It adds a random salt to prevent rainbow tables and dictionary attacks.


2 Answers

I have had a look into the PHP source code. It defaults to bcrypt in PHP5.5.

From ext/standard/php_password.h line 31:

#define PHP_PASSWORD_DEFAULT    PHP_PASSWORD_BCRYPT
like image 189
hek2mgl Avatar answered Sep 19 '22 19:09

hek2mgl


This has been updated in the documentation at password_hash() and will be updating shortly in the constants page (I just committed the documentation change about an hour or so ago).

This will be live today at password.constants

From the updated constants page (which hasn't gone live yet, but will be later today):

Available algorithms:

  • PASSWORD_BCRYPT (integer)

    PASSWORD_BCRYPT is used to create new password hashes using the CRYPT_BLOWFISH algorithm.

    This will always result in a hash using the "$2y$" crypt format, which is always 60 characters wide.

    Supported Options:

    • salt - to manually provide a salt to use when hashing the password. Note that this will override and prevent a salt from being automatically generated.

      If omitted, a random salt will be generated by password_hash() for each password hashed. This is the intended mode of operation.

    • cost - which denotes the algorithmic cost that should be used. Examples of these values can be found on the crypt() page.

      If ommitted, a default value of 10 will be used. This is a good baseline cost, but you may want to consider increasing it depending on your hardware.

  • PASSWORD_DEFAULT (integer)

    The default algorithm to use for hashing if no algorithm is provided. This may change in newer PHP releases when newer, stronger hashing algorithms are supported.

    It is worth noting that over time this constant can (and likely will) change. Therefore you should be aware that the length of the resulting hash can change. Therefore, if you use PASSWORD_DEFAULT you should store the resulting hash in a way that can store more than 60 characters (255 is the recomended width).

    Values for this constant:

    • PHP 5.5.0 - PASSWORD_BCRYPT

As far as when and how PASSWORD_DEFAULT will be updated, that's on the password_hash() documentation page:

Note: Updates to supported algorithms by this function (or changes to the default one) must follow the following rules:

  • Any new algorithm must be in core for at least 1 full release of PHP prior to becoming default. So if, for example, a new algorithm is added in 5.5.5, it would not be eligible for default until 5.7 (since 5.6 would be the first full release). But if a different algorithm was added in 5.6.0, it would also be eligible for default at 5.7.0.

  • The default should only change on a full release (5.6.0, 6.0.0, etc) and not on a revision release. The only exception to this is in an emergency when a critical security flaw is found in the current default.

like image 37
ircmaxell Avatar answered Sep 18 '22 19:09

ircmaxell