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