Commit | Line | Data |
---|---|---|
14d4f286 S |
1 | <?php |
2 | /** | |
3 | * The basic Hash implementation. | |
4 | * | |
5 | * It's worth noting, since there's no prefix, you cannot create a hash using | |
6 | * the factory method. | |
7 | * | |
8 | * PHP version 5.3 | |
9 | * | |
10 | * @category PHPCryptLib | |
11 | * @package Password | |
12 | * @subpackage Implementation | |
13 | * @author Anthony Ferrara <ircmaxell@ircmaxell.com> | |
14 | * @copyright 2011 The Authors | |
15 | * @license http://www.opensource.org/licenses/mit-license.html MIT License | |
16 | * @version Build @@version@@ | |
17 | */ | |
18 | ||
19 | namespace CryptLib\Password\Implementation; | |
20 | ||
21 | use CryptLib\Random\Factory as RandomFactory; | |
22 | ||
23 | /** | |
24 | * The basic Hash implementation. | |
25 | * | |
26 | * It's worth noting, since there's no prefix, you cannot create a hash using | |
27 | * the factory method. | |
28 | * | |
29 | * @category PHPCryptLib | |
30 | * @package Password | |
31 | * @subpackage Implementation | |
32 | * @author Anthony Ferrara <ircmaxell@ircmaxell.com> | |
33 | */ | |
34 | class Hash implements \CryptLib\Password\Password { | |
35 | ||
36 | /** | |
37 | * @var Generator The random generator to use for seeds | |
38 | */ | |
39 | protected $generator = null; | |
40 | ||
41 | /** | |
42 | * @var Hash The hash function to use (MD5) | |
43 | */ | |
44 | protected $hash = null; | |
45 | ||
46 | /** | |
47 | * Determine if the hash was made with this method | |
48 | * | |
49 | * @param string $hash The hashed data to check | |
50 | * | |
51 | * @return boolean Was the hash created by this method | |
52 | */ | |
53 | public static function detect($hash) { | |
54 | $res = preg_match('/^[a-fA-F0-9]+$/', $hash); | |
55 | $res &= (int) in_array(strlen($hash), array(32, 40, 64, 128)); | |
56 | return (boolean) $res; | |
57 | } | |
58 | ||
59 | /** | |
60 | * Return the prefix used by this hashing method | |
61 | * | |
62 | * @return string The prefix used | |
63 | */ | |
64 | public static function getPrefix() { | |
65 | return false; | |
66 | } | |
67 | ||
68 | /** | |
69 | * Load an instance of the class based upon the supplied hash | |
70 | * | |
71 | * @param string $hash The hash to load from | |
72 | * | |
73 | * @return Password the created instance | |
74 | * @throws InvalidArgumentException if the hash wasn't created here | |
75 | */ | |
76 | public static function loadFromHash($hash) { | |
77 | if (!static::detect($hash)) { | |
78 | throw new \InvalidArgumentException('Hash Not Created Here'); | |
79 | } | |
80 | $hashMethod = ''; | |
81 | switch (strlen($hash)) { | |
82 | case 32: | |
83 | $hashMethod = 'md5'; | |
84 | break; | |
85 | case 40: | |
86 | $hashMethod = 'sha1'; | |
87 | break; | |
88 | case 64: | |
89 | $hashMethod = 'sha256'; | |
90 | break; | |
91 | case 128: | |
92 | $hashMethod = 'sha512'; | |
93 | break; | |
94 | } | |
95 | return new static($hashMethod); | |
96 | } | |
97 | ||
98 | /** | |
99 | * Build a new instance | |
100 | * | |
101 | * @param string $hashMethod The hash function to use for hashing | |
102 | * @param Generator $generator The random generator to use for seeds | |
103 | * @param Factory $factory The hash factory to use for this instance | |
104 | * | |
105 | * @return void | |
106 | */ | |
107 | public function __construct( | |
108 | $hashMethod, | |
109 | \CryptLib\Random\Generator $generator = null | |
110 | ) { | |
111 | $this->hash = $hashMethod; | |
112 | if (is_null($generator)) { | |
113 | $random = new RandomFactory(); | |
114 | $generator = $random->getMediumStrengthGenerator(); | |
115 | } | |
116 | $this->generator = $generator; | |
117 | } | |
118 | ||
119 | /** | |
120 | * Create a password hash for a given plain text password | |
121 | * | |
122 | * @param string $password The password to hash | |
123 | * | |
124 | * @return string The formatted password hash | |
125 | */ | |
126 | public function create($password) { | |
127 | throw new \BadMethodCallException( | |
128 | 'Unsalted Passwords are only implemented for verification' | |
129 | ); | |
130 | } | |
131 | ||
132 | /** | |
133 | * Verify a password hash against a given plain text password | |
134 | * | |
135 | * @param string $password The password to hash | |
136 | * @param string $hash The supplied ahsh to validate | |
137 | * | |
138 | * @return boolean Does the password validate against the hash | |
139 | */ | |
140 | public function verify($password, $hash) { | |
141 | $test = hash($this->hash, $password); | |
142 | return $test == $hash; | |
143 | } | |
144 | ||
145 | } |