7 * @category PHPCryptLib
9 * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
10 * @copyright 2011 The Authors
11 * @license http://www.opensource.org/licenses/mit-license.html MIT License
12 * @version Build @@version@@
15 namespace CryptLib\Password
;
17 use CryptLib\Password\Implementation\Blowfish
;
20 * The Password Factory
22 * @category PHPCryptLib
24 * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
26 class Factory
extends \CryptLib\Core\AbstractFactory
{
29 * @var array An array of implementation classes
31 protected $implementations = array();
34 * Build a new instance of the factory, loading core implementations
38 public function __construct() {
39 $this->loadImplementations();
43 * Create a new password hash from the supplied password
45 * This defaults to using Blowfish if $prefix is not supplied
47 * @param string $password The password to hash
48 * @param string $prefix The prefix for the implementation
50 * @return string The hashed password
51 * @throws DomainException if the supplied prefix is not supported
53 public function createHash($password, $prefix = '$2a$') {
54 if ($prefix === false) {
55 throw new \
DomainException('Unsupported Prefix Supplied');
57 foreach ($this->implementations
as $impl) {
58 if ($impl::getPrefix() == $prefix) {
59 $instance = new $impl;
60 return $instance->create($password);
63 throw new \
DomainException('Unsupported Prefix Supplied');
67 * Verify a hash with a supplied password
69 * @param string $password The password to check against
70 * @param string $hash The hash to verify
72 * @return boolean True if valid, false if not
73 * @throws DomainException if the supplied prefix is not supported
75 public function verifyHash($password, $hash) {
76 foreach ($this->implementations
as $impl) {
77 if ($impl::detect($hash)) {
78 $instance = $impl::loadFromHash($hash);
79 return $instance->verify($password, $hash);
82 throw new \
DomainException('Unsupported Password Hash Supplied');
86 * Register a password implementation for this factory instance
88 * @param string $name The name of the stategy
89 * @param string $class The class name of the implementation
91 * @return Factory $this The current factory instance
93 public function registerImplementation($name, $class) {
96 __NAMESPACE__
. '\\Password',
104 * Load all core password hashing implementations
108 protected function loadImplementations() {
110 __DIR__
. '/Implementation',
111 __NAMESPACE__
. '\\Implementation\\',
112 array($this, 'registerImplementation')