add methods to decrypt return data from router
[GitHub/Stricted/speedport-hybrid-php-api.git] / CryptLib / Password / Factory.php
1 <?php
2 /**
3 * The Password Factory
4 *
5 * PHP version 5.3
6 *
7 * @category PHPCryptLib
8 * @package Password
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@@
13 */
14
15 namespace CryptLib\Password;
16
17 use CryptLib\Password\Implementation\Blowfish;
18
19 /**
20 * The Password Factory
21 *
22 * @category PHPCryptLib
23 * @package Password
24 * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
25 */
26 class Factory extends \CryptLib\Core\AbstractFactory {
27
28 /**
29 * @var array An array of implementation classes
30 */
31 protected $implementations = array();
32
33 /**
34 * Build a new instance of the factory, loading core implementations
35 *
36 * @return void
37 */
38 public function __construct() {
39 $this->loadImplementations();
40 }
41
42 /**
43 * Create a new password hash from the supplied password
44 *
45 * This defaults to using Blowfish if $prefix is not supplied
46 *
47 * @param string $password The password to hash
48 * @param string $prefix The prefix for the implementation
49 *
50 * @return string The hashed password
51 * @throws DomainException if the supplied prefix is not supported
52 */
53 public function createHash($password, $prefix = '$2a$') {
54 if ($prefix === false) {
55 throw new \DomainException('Unsupported Prefix Supplied');
56 }
57 foreach ($this->implementations as $impl) {
58 if ($impl::getPrefix() == $prefix) {
59 $instance = new $impl;
60 return $instance->create($password);
61 }
62 }
63 throw new \DomainException('Unsupported Prefix Supplied');
64 }
65
66 /**
67 * Verify a hash with a supplied password
68 *
69 * @param string $password The password to check against
70 * @param string $hash The hash to verify
71 *
72 * @return boolean True if valid, false if not
73 * @throws DomainException if the supplied prefix is not supported
74 */
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);
80 }
81 }
82 throw new \DomainException('Unsupported Password Hash Supplied');
83 }
84
85 /**
86 * Register a password implementation for this factory instance
87 *
88 * @param string $name The name of the stategy
89 * @param string $class The class name of the implementation
90 *
91 * @return Factory $this The current factory instance
92 */
93 public function registerImplementation($name, $class) {
94 $this->registerType(
95 'implementations',
96 __NAMESPACE__ . '\\Password',
97 $name,
98 $class
99 );
100 return $this;
101 }
102
103 /**
104 * Load all core password hashing implementations
105 *
106 * @return void
107 */
108 protected function loadImplementations() {
109 $this->loadFiles(
110 __DIR__ . '/Implementation',
111 __NAMESPACE__ . '\\Implementation\\',
112 array($this, 'registerImplementation')
113 );
114 }
115
116 }