1: <?php
2: /**
3: * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
4: * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
5: *
6: * Licensed under The MIT License
7: * For full copyright and license information, please see the LICENSE.txt
8: * Redistributions of files must retain the above copyright notice.
9: *
10: * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
11: * @link https://cakephp.org CakePHP(tm) Project
12: * @since 2.4.0
13: * @license https://opensource.org/licenses/mit-license.php MIT License
14: */
15: namespace Cake\Auth;
16:
17: use Cake\Core\InstanceConfigTrait;
18:
19: /**
20: * Abstract password hashing class
21: */
22: abstract class AbstractPasswordHasher
23: {
24:
25: use InstanceConfigTrait;
26:
27: /**
28: * Default config
29: *
30: * These are merged with user-provided config when the object is used.
31: *
32: * @var array
33: */
34: protected $_defaultConfig = [];
35:
36: /**
37: * Constructor
38: *
39: * @param array $config Array of config.
40: */
41: public function __construct(array $config = [])
42: {
43: $this->setConfig($config);
44: }
45:
46: /**
47: * Generates password hash.
48: *
49: * @param string|array $password Plain text password to hash or array of data
50: * required to generate password hash.
51: * @return string Password hash
52: */
53: abstract public function hash($password);
54:
55: /**
56: * Check hash. Generate hash from user provided password string or data array
57: * and check against existing hash.
58: *
59: * @param string|array $password Plain text password to hash or data array.
60: * @param string $hashedPassword Existing hashed password.
61: * @return bool True if hashes match else false.
62: */
63: abstract public function check($password, $hashedPassword);
64:
65: /**
66: * Returns true if the password need to be rehashed, due to the password being
67: * created with anything else than the passwords generated by this class.
68: *
69: * Returns true by default since the only implementation users should rely
70: * on is the one provided by default in php 5.5+ or any compatible library
71: *
72: * @param string $password The password to verify
73: * @return bool
74: */
75: public function needsRehash($password)
76: {
77: return password_needs_rehash($password, PASSWORD_DEFAULT);
78: }
79: }
80: