hash.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php namespace Laravel;
  2. class Hash {
  3. /**
  4. * Determine if an unhashed value matches a given Bcrypt hash.
  5. *
  6. * @param string $value
  7. * @param string $hash
  8. * @return bool
  9. */
  10. public static function check($value, $hash)
  11. {
  12. return crypt($value, $hash) === $hash;
  13. }
  14. /**
  15. * Hash a password using the Bcrypt hashing scheme.
  16. *
  17. * <code>
  18. * // Create a Bcrypt hash of a value
  19. * $hash = Hash::make('secret');
  20. *
  21. * // Use a specified number of iterations when creating the hash
  22. * $hash = Hash::make('secret', 12);
  23. * </code>
  24. *
  25. * @param string $value
  26. * @param int $rounds
  27. * @return string
  28. */
  29. public static function make($value, $rounds = 8)
  30. {
  31. $work = str_pad($rounds, 2, '0', STR_PAD_LEFT);
  32. // Bcrypt expects the salt to be 22 base64 encoded characters including
  33. // dots and slashes. We will get rid of the plus signs included in the
  34. // base64 data and replace them with dots. OpenSSL will be used if it
  35. // is available, otherwise we will use the Str::random method.
  36. if (function_exists('openssl_random_pseudo_bytes'))
  37. {
  38. $salt = openssl_random_pseudo_bytes(16);
  39. }
  40. else
  41. {
  42. $salt = Str::random(40);
  43. }
  44. $salt = substr(strtr(base64_encode($salt), '+', '.'), 0 , 22);
  45. return crypt($value, '$2a$'.$work.'$'.$salt);
  46. }
  47. }