getHashedString.php 977 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. function getHashedString($password) {
  3. // Inspired by http://alias.io/2010/01/store-passwords-safely-with-php-and-mysql/
  4. // A higher $cost is more secure but consumes more processing power
  5. $cost = 10;
  6. // Create a random salt
  7. if (extension_loaded('openssl')) {
  8. $salt = strtr(substr(base64_encode(openssl_random_pseudo_bytes(17)),0,22), '+', '.');
  9. } elseif (extension_loaded('mcrypt')) {
  10. $salt = strtr(substr(base64_encode(mcrypt_create_iv(17, MCRYPT_DEV_URANDOM)),0,22), '+', '.');
  11. } else {
  12. $salt = '';
  13. for ($i = 0; $i < 22; $i++) {
  14. $salt .= substr("./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", mt_rand(0, 63), 1);
  15. }
  16. }
  17. // Prefix information about the hash so PHP knows how to verify it later.
  18. // "$2a$" Means we're using the Blowfish algorithm. The following two digits are the cost parameter.
  19. $salt = sprintf("$2a$%02d$", $cost) . $salt;
  20. // Hash the password with the salt
  21. return crypt($password, $salt);
  22. }
  23. ?>