1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Zemit Framework. |
5
|
|
|
* |
6
|
|
|
* (c) Zemit Team <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Zemit\Mvc\Model; |
13
|
|
|
|
14
|
|
|
use Phalcon\Di\DiInterface; |
15
|
|
|
use Phalcon\Security; |
16
|
|
|
use Zemit\Config\ConfigInterface; |
17
|
|
|
|
18
|
|
|
trait Hash |
19
|
|
|
{ |
20
|
|
|
abstract public function getDI(): DiInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Hash string |
24
|
|
|
*/ |
25
|
|
|
public function hash(string $string, ?string $salt = null, ?string $workFactor = null): string |
26
|
|
|
{ |
27
|
|
|
$config = $this->getDI()->get('config'); |
28
|
|
|
assert($config instanceof ConfigInterface); |
29
|
|
|
|
30
|
|
|
$security = $this->getDI()->get('security'); |
31
|
|
|
assert($security instanceof Security); |
32
|
|
|
|
33
|
|
|
// Get salt & workFactor |
34
|
|
|
$salt ??= $config->path('security.salt') ?? ''; |
35
|
|
|
$workFactor ??= $config->path('security.workFactor') ?? 10; |
36
|
|
|
|
37
|
|
|
// return salted hash |
38
|
|
|
return $security->hash($salt . $string, $workFactor); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Check hash |
43
|
|
|
*/ |
44
|
|
|
public function checkHash(?string $hash = null, ?string $string = null, int $maxPassLength = 0): bool |
45
|
|
|
{ |
46
|
|
|
// hash empty, not valid |
47
|
|
|
if (empty($hash)) { |
48
|
|
|
return false; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// string empty not valid |
52
|
|
|
if (empty($string)) { |
53
|
|
|
return false; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$config = $this->getDI()->get('config'); |
57
|
|
|
assert($config instanceof ConfigInterface); |
58
|
|
|
|
59
|
|
|
$security = $this->getDI()->get('security'); |
60
|
|
|
assert($security instanceof Security); |
61
|
|
|
|
62
|
|
|
// Get salt |
63
|
|
|
$salt = $config->path('security.salt') ?? ''; |
64
|
|
|
|
65
|
|
|
// check hash |
66
|
|
|
return $security->checkHash($salt . $string, $hash, $maxPassLength); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|