3 * The Joomla based hash implementation based off of the md5-hex hash method
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 Joomla based hash implementation based off of the md5-hex hash method
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 Joomla
implements \CryptLib\Password\Password
{
37 * @var Generator The random generator to use for seeds
39 protected $generator = null;
42 * Determine if the hash was made with this method
44 * @param string $hash The hashed data to check
46 * @return boolean Was the hash created by this method
48 public static function detect($hash) {
49 return (boolean
) preg_match('/^[a-fA-F0-9]{32}:[a-zA-z0-9]{32}$/', $hash);
53 * Return the prefix used by this hashing method
55 * @return string The prefix used
57 public static function getPrefix() {
62 * Load an instance of the class based upon the supplied hash
64 * @param string $hash The hash to load from
66 * @return Password the created instance
67 * @throws InvalidArgumentException if the hash wasn't created here
69 public static function loadFromHash($hash) {
70 if (!static::detect($hash)) {
71 throw new \
InvalidArgumentException('Hash Not Created Here');
77 * Build a new instance
79 * @param Generator $generator The random generator to use for seeds
80 * @param Factory $factory The hash factory to use for this instance
84 public function __construct(
85 \CryptLib\Random\Generator
$generator = null
87 if (is_null($generator)) {
88 $random = new RandomFactory();
89 $generator = $random->getMediumStrengthGenerator();
91 $this->generator
= $generator;
95 * Create a password hash for a given plain text password
97 * @param string $password The password to hash
99 * @return string The formatted password hash
101 public function create($password) {
102 $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
103 $salt = $this->generator
->generateString(32, $chars);
104 $hash = md5($password . $salt);
105 return $hash . ':' . $salt;
109 * Verify a password hash against a given plain text password
111 * @param string $password The password to hash
112 * @param string $hash The supplied ahsh to validate
114 * @return boolean Does the password validate against the hash
116 public function verify($password, $hash) {
117 if (!static::detect($hash)) {
118 throw new \
InvalidArgumentException(
119 'The hash was not created here, we cannot verify it'
122 list ($hash, $salt) = explode(':', $hash, 2);
123 $test = md5($password . $salt);
124 return $test == $hash;