I am not sure which algorithm crypt() uses when hashing. I looked on the PHP manual, but it just says that it uses whatever is available. But how do I know which one it uses, and if it does use one, how to tell it which one to use? I am using MAMP currently as my development environment, but I figure there must be a way to find out with a statement in PHP.
Share
You specify the algorithm as part of the salt string. For example, starting with
$2a$gives you a Blowfish cypher. If the machine does not support the algorithm you are trying to use, you won’t get a meaningful result. You can attempt to find out in advance which algorithms are supported by checking some of the predefined constants, such asCRYPT_BLOWFISH, although I have noticed that the constantsCRYPT_SHA256andCRYPT_SHA512are not always defined, at least on PHP 5.2. Starting with PHP 5.3, PHP has its own implementations of the algorithms, so it does not matter what the system has available at PHP compile time like it does in PHP 5.2 and earlier. The Suhosin patch for PHP 5.2 supposedly adds at least Blowfish, but its implementation does not seem to be compatible with the one used in PHP 5.3.The PHP docs for the
crypt()function do provide some information on how to use the salt string to specify which algorithm to use:So, to specify that you want the string "password" hashed using Blowfish with 2^10 iterations, you could use
where the string starting with
XA86is the salt.Finally, if you want more examples or just want something to take care of all this password compatibility business for you, take a look at phpass. It is public domain and works nicely in my experience. It will automatically use the "best" algorithm on the system unless you specify that you want a hash that is compatible with multiple systems, in which case (I think) it uses MD5.