New hash functions in PHP

Safe and secure storage of passwords and other sensitive information is of great concern to every developer. Today I came across some new hash functions built in to the latest release of PHP.

To view the various algorithms you have available on your server add the following to a php page:

print_r(hash_algos());

To add the selected algorithm to your password or text do the following:

// sha512 is supposed to be unbreakable
$ctx = hash_init('sha512');
// apply the algorithm
hash_update($ctx, 'part_of_password');
// add more if you need
hash_update($ctx, 'continued_password');
// view result
echo hash_final($ctx);

This could come in handy when you need to encrypt your passwords stored in your database etc.

If you don’t get anything printed it’s due to the fact that your server isn’t running the latest version of PHP, this can’t be solved until either you or your host desides to upgrade…

About Author