Total Complexity | 11 |
Total Lines | 104 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
1 | <?php |
||
5 | class CookieIdentity extends Identity |
||
6 | { |
||
7 | |||
8 | const SERIES_SIZE = 16; |
||
9 | const KEY_SIZE = 32; |
||
10 | |||
11 | private $series; |
||
12 | private $key; |
||
13 | private $hash; |
||
14 | |||
15 | protected $type = Identity::TYPE_COOKIE; |
||
16 | |||
17 | |||
18 | 11 | public function setSeries(string $series = null) |
|
19 | { |
||
20 | 11 | $this->series = $series; |
|
21 | 11 | } |
|
22 | |||
23 | |||
24 | /** |
||
25 | * @codeCoverageIgnore |
||
26 | */ |
||
27 | public function getSeries() |
||
28 | { |
||
29 | return $this->series; |
||
30 | } |
||
31 | |||
32 | |||
33 | /** |
||
34 | * Produces a hash from series to obscure it for storage. |
||
35 | */ |
||
36 | 12 | public function getFingerprint(): string |
|
37 | { |
||
38 | 12 | return hash('sha384', $this->series); |
|
39 | } |
||
40 | |||
41 | |||
42 | 10 | public function generateNewSeries() |
|
43 | { |
||
44 | 10 | $this->series = bin2hex(random_bytes(CookieIdentity::SERIES_SIZE)); |
|
45 | 10 | } |
|
46 | |||
47 | |||
48 | /** |
||
49 | * Assigns a new identification key and resets a the hash. |
||
50 | * |
||
51 | * @param string $key |
||
52 | */ |
||
53 | 5 | public function setKey(string $key = null) |
|
54 | { |
||
55 | 5 | $this->hash = null; |
|
56 | 5 | $this->key = $key; |
|
57 | 5 | $this->hash = $this->makeHash($key); |
|
58 | 5 | } |
|
59 | |||
60 | |||
61 | /** |
||
62 | * @codeCoverageIgnore |
||
63 | * @return string|null |
||
64 | */ |
||
65 | public function getKey() |
||
66 | { |
||
67 | return $this->key; |
||
68 | } |
||
69 | |||
70 | |||
71 | /** |
||
72 | * Sets a new key and resets the hash. |
||
73 | */ |
||
74 | 11 | public function generateNewKey() |
|
75 | { |
||
76 | 11 | $key = bin2hex(random_bytes(CookieIdentity::KEY_SIZE)); |
|
77 | 11 | $this->key = $key; |
|
78 | 11 | $this->hash = $this->makeHash($key); |
|
79 | 11 | } |
|
80 | |||
81 | |||
82 | /** |
||
83 | * @codeCoverageIgnore |
||
84 | */ |
||
85 | public function getHash() |
||
88 | } |
||
89 | |||
90 | |||
91 | 16 | private function makeHash($key): string |
|
92 | { |
||
93 | 16 | return hash('sha384', $key); |
|
94 | } |
||
95 | |||
96 | |||
97 | /** |
||
98 | * @param string $hash |
||
99 | */ |
||
100 | 11 | public function setHash(string $hash = null) |
|
101 | { |
||
102 | 11 | $this->hash = $hash; |
|
103 | 11 | } |
|
104 | |||
105 | |||
106 | 6 | public function matchKey($key): bool |
|
109 | } |
||
110 | } |
||
111 |