forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPmHash.php
More file actions
95 lines (85 loc) · 1.93 KB
/
PmHash.php
File metadata and controls
95 lines (85 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
namespace ProcessMaker\Helpers;
use Illuminate\Contracts\Hashing\Hasher as HasherContract;
class PmHash implements HasherContract
{
/**
* @var mixed|string
*/
protected $algo;
/**
* @var string
*/
protected $defaultAlgo = 'sha1';
public function __construct()
{
$priority = [
'sha3-512',
'sha3-384',
'sha3-256',
'sha512',
'sha384',
'sha256',
'sha224',
'sha1',
];
$algos = hash_algos();
foreach ($priority as $algo) {
if (in_array($algo, $algos)) {
$this->algo = $algo;
break;
}
}
$this->algo = $this->algo ?: $this->defaultAlgo;
}
/**
* @param $hashedValue
* @return array|string[]
*/
public function info($hashedValue)
{
return [
'algo' => $this->algo,
];
}
/**
* @param string $value
* @param array $options
* @return string
*/
public function make($value, array $options = [])
{
return hash_hmac(
$this->algo,
$value,
config('app.key')
);
}
/**
* @param string $value
* @param string $hashedValue
* @param array $options
* @return bool
*/
public function check($value, $hashedValue, array $options = [])
{
$fresh = $this->make($value, $options);
if (strlen($fresh) !== strlen($hashedValue)) {
return false;
}
$match = true;
for ($i = 0; isset($fresh[$i]); $i++) {
$match = $match && $fresh[$i] === $hashedValue[$i];
}
return $match;
}
/**
* @param string $hashedValue
* @param array $options
* @return false
*/
public function needsRehash($hashedValue, array $options = [])
{
return false;
}
}