3 * The basic Hash implementation.
5 * It's worth noting, since there's no prefix, you cannot create a hash using
10 * @category PHPCryptLib
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@@
19 namespace CryptLib\Password\Implementation
;
21 use CryptLib\Random\Factory
as RandomFactory
;
24 * The basic Hash implementation.
26 * It's worth noting, since there's no prefix, you cannot create a hash using
29 * @category PHPCryptLib
31 * @subpackage Implementation
32 * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
34 class Hash
implements \CryptLib\Password\Password
{
37 * @var Generator The random generator to use for seeds
39 protected $generator = null;
42 * @var Hash The hash function to use (MD5)
44 protected $hash = null;
47 * Determine if the hash was made with this method
49 * @param string $hash The hashed data to check
51 * @return boolean Was the hash created by this method
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;
60 * Return the prefix used by this hashing method
62 * @return string The prefix used
64 public static function getPrefix() {
69 * Load an instance of the class based upon the supplied hash
71 * @param string $hash The hash to load from
73 * @return Password the created instance
74 * @throws InvalidArgumentException if the hash wasn't created here
76 public static function loadFromHash($hash) {
77 if (!static::detect($hash)) {
78 throw new \
InvalidArgumentException('Hash Not Created Here');
81 switch (strlen($hash)) {
89 $hashMethod = 'sha256';
92 $hashMethod = 'sha512';
95 return new static($hashMethod);
99 * Build a new instance
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
107 public function __construct(
109 \CryptLib\Random\Generator
$generator = null
111 $this->hash
= $hashMethod;
112 if (is_null($generator)) {
113 $random = new RandomFactory();
114 $generator = $random->getMediumStrengthGenerator();
116 $this->generator
= $generator;
120 * Create a password hash for a given plain text password
122 * @param string $password The password to hash
124 * @return string The formatted password hash
126 public function create($password) {
127 throw new \
BadMethodCallException(
128 'Unsalted Passwords are only implemented for verification'
133 * Verify a password hash against a given plain text password
135 * @param string $password The password to hash
136 * @param string $hash The supplied ahsh to validate
138 * @return boolean Does the password validate against the hash
140 public function verify($password, $hash) {
141 $test = hash($this->hash
, $password);
142 return $test == $hash;