1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SGP\IronBox; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use SGP\IronBox\Contracts\Validatable; |
7
|
|
|
use SGP\IronBox\Exceptions\IronBoxException; |
8
|
|
|
|
9
|
|
|
class ContainerKeyData implements Validatable |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $cipher; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $symmetricKey; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $initializationVector; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var int |
28
|
|
|
*/ |
29
|
|
|
protected $keyStrength; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
protected $supportedCiphers = [ |
35
|
|
|
1 => 'AES-128-CBC', |
36
|
|
|
2 => 'AES-256-CBC', |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* ContainerKeyData constructor. |
41
|
|
|
* |
42
|
|
|
* @param array $data |
43
|
|
|
*/ |
44
|
|
|
public function __construct(array $data = []) |
45
|
|
|
{ |
46
|
|
|
foreach ($data as $key => $value) { |
47
|
|
|
$key = lcfirst($key); |
48
|
|
|
|
49
|
|
|
$this->{$key} = $value; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $symmetricKey |
55
|
|
|
* |
56
|
|
|
* @return $this |
57
|
|
|
*/ |
58
|
|
|
public function symmetricKey(string $symmetricKey): ContainerKeyData |
59
|
|
|
{ |
60
|
|
|
$this->symmetricKey = $symmetricKey; |
61
|
|
|
|
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $initializationVector |
67
|
|
|
* |
68
|
|
|
* @return $this |
69
|
|
|
*/ |
70
|
|
|
public function initializationVector(string $initializationVector): ContainerKeyData |
71
|
|
|
{ |
72
|
|
|
$this->initializationVector = $initializationVector; |
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param int $keyStrength |
79
|
|
|
* |
80
|
|
|
* @return $this |
81
|
|
|
* @throws \SGP\IronBox\Exceptions\IronBoxException |
82
|
|
|
*/ |
83
|
|
|
public function keyStrength(int $keyStrength): ContainerKeyData |
84
|
|
|
{ |
85
|
|
|
if ($keyStrength !== 1 && $keyStrength !== 2) { |
86
|
|
|
throw new IronBoxException('Invalid key strength - must be either 1 or 2'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$this->keyStrength = $keyStrength; |
90
|
|
|
|
91
|
|
|
$this->setCipher(); |
92
|
|
|
|
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Validates that all required fields are present |
98
|
|
|
* |
99
|
|
|
* @return bool |
100
|
|
|
* @throws \SGP\IronBox\Exceptions\IronBoxException |
101
|
|
|
*/ |
102
|
|
|
public function validate() |
103
|
|
|
{ |
104
|
|
|
$requiredFields = ['symmetricKey', 'initializationVector', 'keyStrength', 'cipher']; |
105
|
|
|
|
106
|
|
|
foreach ($requiredFields as $requiredField) { |
107
|
|
|
if (is_null($this->{$requiredField})) { |
108
|
|
|
throw new IronBoxException("Field `{$requiredField}` is required"); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return true; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @throws \SGP\IronBox\Exceptions\IronBoxException |
117
|
|
|
*/ |
118
|
|
|
protected function setCipher() |
119
|
|
|
{ |
120
|
|
|
if (! isset($this->supportedCiphers[$this->keyStrength])) { |
121
|
|
|
throw new IronBoxException('Unsupported cipher'); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$this->cipher = $this->supportedCiphers[$this->keyStrength]; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function __get($key) |
128
|
|
|
{ |
129
|
|
|
if (! isset($this->{$key})) { |
130
|
|
|
throw new Exception("Property `{$key}` doesn't exist"); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $this->{$key}; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public static function create(array $data = []) |
137
|
|
|
{ |
138
|
|
|
return new static($data); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|