|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Palladium\Entity; |
|
4
|
|
|
|
|
5
|
|
|
class NonceIdentity extends Identity |
|
6
|
|
|
{ |
|
7
|
|
|
|
|
8
|
|
|
const HASH_ALGO = PASSWORD_BCRYPT; |
|
9
|
|
|
const HASH_COST = 12; |
|
10
|
|
|
|
|
11
|
|
|
const NONCE_SIZE = 16; |
|
12
|
|
|
const KEY_SIZE = 32; |
|
13
|
|
|
|
|
14
|
|
|
private $identifier; |
|
15
|
|
|
private $key; |
|
16
|
|
|
private $hash; |
|
17
|
|
|
|
|
18
|
|
|
protected $type = Identity::TYPE_NONCE; |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @codeCoverageIgnore |
|
23
|
|
|
*/ |
|
24
|
|
|
public function setIdentifier(string $identifier) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->identifier = $identifier; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @codeCoverageIgnore |
|
32
|
|
|
*/ |
|
33
|
|
|
public function getIdentifier() |
|
34
|
|
|
{ |
|
35
|
|
|
return $this->identifier; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
1 |
|
public function generateNewNonce() |
|
40
|
|
|
{ |
|
41
|
1 |
|
$this->identifier = bin2hex(random_bytes(NonceIdentity::NONCE_SIZE)); |
|
42
|
1 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @codeCoverageIgnore |
|
47
|
|
|
*/ |
|
48
|
|
|
public function getFingerprint(): string |
|
49
|
|
|
{ |
|
50
|
|
|
return hash('sha384', $this->identifier); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Sets a new key and resets the hash. |
|
56
|
|
|
*/ |
|
57
|
1 |
|
public function generateNewKey(int $cost = NonceIdentity::HASH_COST) |
|
58
|
|
|
{ |
|
59
|
1 |
|
$this->key = bin2hex(random_bytes(NonceIdentity::KEY_SIZE)); |
|
60
|
1 |
|
$this->hash = $this->makeHash($this->key, $cost); |
|
61
|
1 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param string $key |
|
66
|
|
|
*/ |
|
67
|
5 |
|
private function makeHash($key, int $cost): string |
|
68
|
|
|
{ |
|
69
|
5 |
|
return password_hash($key, NonceIdentity::HASH_ALGO, ['cost' => $cost]); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param string $key |
|
75
|
|
|
*/ |
|
76
|
3 |
|
public function matchKey($key): bool |
|
77
|
|
|
{ |
|
78
|
3 |
|
return password_verify($key, $this->hash); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Assigns a new identification key and resets a the hash. |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $key |
|
86
|
|
|
*/ |
|
87
|
4 |
|
public function setKey(string $key = null, int $cost = NonceIdentity::HASH_COST) |
|
88
|
|
|
{ |
|
89
|
4 |
|
$this->hash = null; |
|
90
|
4 |
|
$this->key = $key; |
|
91
|
4 |
|
$this->hash = $this->makeHash($key, $cost); |
|
92
|
4 |
|
} |
|
93
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @codeCoverageIgnore |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getKey() |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->key; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @codeCoverageIgnore |
|
106
|
|
|
*/ |
|
107
|
|
|
public function setHash(string $hash) |
|
108
|
|
|
{ |
|
109
|
|
|
$this->hash = $hash; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @codeCoverageIgnore |
|
114
|
|
|
* @return string |
|
115
|
|
|
*/ |
|
116
|
|
|
public function getHash() |
|
117
|
|
|
{ |
|
118
|
|
|
return $this->hash; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|