Every good programmer knows, that passwords should never be stored in clear text. Instead a one way hash (or digest) should be used. This way user passwords are not at risk1) in case of an intrusion.
In cryptography, a cryptographic hash function is a transformation that takes an input and returns a fixed-size string, which is called the hash value.
But sometimes you may want to switch the used hash algorithm with keeping backwards compatibility to old hashes 2). Or you may want to authenticate against different systems all using different hashes.
DokuWiki does support a broad range of authentication backends. For creating and verifying password hashes we have two functions: auth_cryptPassword() and auth_verifyPassword(). The functions are completely self contained and can be used outside DokuWiki as well.
auth_cryptPassword() is used to create a new hash from a given plaintext password. The second argument is the wanted algorithm. Currently supported are:
To create a hash just call it like this:
$hash = auth_cryptPassword('mypassword','smd5');
The hash can be stored in your user table. To verify a user password later, auth_veryPassword() is used:
// get stored password hash from db $hash_from_db = db_get_hash($_POST['user']); // verify the provided password $ok = auth_verifyPassword($_POST['pass'],$hash_from_db); // $ok will be true if the provided password was correct
The nice thing about auth_veryPassword() is that it will automatically determine which hash algorithm was used to create the stored password hash. This way you can have different types of password hashes in your database.
Update: All the code is now wrapped up in a class.