1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright © 2017 Toan Nguyen. All rights reserved. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Gojira\Framework\Encryption; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class encapsulates cryptographic algorithm |
13
|
|
|
* |
14
|
|
|
* @package Gojira\Framework\Encryption |
15
|
|
|
* @author Toan Nguyen <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class Crypt |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $cipher; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $mode; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $initVector; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Encryption algorithm module handle |
36
|
|
|
* |
37
|
|
|
* @var resource |
38
|
|
|
*/ |
39
|
|
|
protected $handle; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Constructor |
43
|
|
|
* |
44
|
|
|
* @param string $key Secret encryption key. |
45
|
|
|
* It's unsafe to store encryption key in memory, so no getter for key exists. |
46
|
|
|
* @param string $cipher Cipher algorithm (one of the MCRYPT_ciphername constants) |
47
|
|
|
* @param string $mode Mode of cipher algorithm (MCRYPT_MODE_modeabbr constants) |
48
|
|
|
* @param string|bool $initVector Initial vector to fill algorithm blocks. |
49
|
|
|
* TRUE generates a random initial vector. |
50
|
|
|
* FALSE fills initial vector with zero bytes to not use it. |
51
|
|
|
* |
52
|
|
|
* @throws \Exception |
53
|
|
|
*/ |
54
|
|
|
public function __construct($key, $cipher = MCRYPT_BLOWFISH, $mode = MCRYPT_MODE_ECB, $initVector = false) |
55
|
|
|
{ |
56
|
|
|
$this->cipher = $cipher; |
57
|
|
|
$this->mode = $mode; |
58
|
|
|
$this->handle = mcrypt_module_open($cipher, '', $mode, ''); |
59
|
|
|
try { |
60
|
|
|
$maxKeySize = mcrypt_enc_get_key_size($this->handle); |
61
|
|
|
if (strlen($key) > $maxKeySize) { |
62
|
|
|
throw new \Gojira\Framework\Exception\LocalizedException( |
63
|
|
|
new \Gojira\Framework\Phrase('Key must not exceed %1 bytes.', [$maxKeySize]) |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
$initVectorSize = mcrypt_enc_get_iv_size($this->handle); |
67
|
|
|
if (true === $initVector) { |
68
|
|
|
/* Generate a random vector from human-readable characters */ |
69
|
|
|
$abc = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; |
70
|
|
|
$initVector = ''; |
71
|
|
|
for ($i = 0; $i < $initVectorSize; $i++) { |
72
|
|
|
$initVector .= $abc[rand(0, strlen($abc) - 1)]; |
73
|
|
|
} |
74
|
|
|
} elseif (false === $initVector) { |
75
|
|
|
/* Set vector to zero bytes to not use it */ |
76
|
|
|
$initVector = str_repeat("\0", $initVectorSize); |
77
|
|
|
} elseif (!is_string($initVector) || strlen($initVector) != $initVectorSize) { |
78
|
|
|
throw new \Gojira\Framework\Exception\LocalizedException( |
79
|
|
|
new \Gojira\Framework\Phrase('Init vector must be a string of %1 bytes.', [$initVectorSize]) |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
$this->initVector = $initVector; |
83
|
|
|
} catch (\Exception $e) { |
84
|
|
|
mcrypt_module_close($this->handle); |
85
|
|
|
throw $e; |
86
|
|
|
} |
87
|
|
|
mcrypt_generic_init($this->handle, $key, $initVector); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Destructor frees allocated resources |
92
|
|
|
* |
93
|
|
|
* @return void |
94
|
|
|
*/ |
95
|
|
|
public function __destruct() |
96
|
|
|
{ |
97
|
|
|
mcrypt_generic_deinit($this->handle); |
98
|
|
|
mcrypt_module_close($this->handle); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Retrieve a name of currently used cryptographic algorithm |
103
|
|
|
* |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
public function getCipher() |
107
|
|
|
{ |
108
|
|
|
return $this->cipher; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Mode in which cryptographic algorithm is running |
113
|
|
|
* |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
|
|
public function getMode() |
117
|
|
|
{ |
118
|
|
|
return $this->mode; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Retrieve an actual value of initial vector that has been used to initialize a cipher |
123
|
|
|
* |
124
|
|
|
* @return string |
125
|
|
|
*/ |
126
|
|
|
public function getInitVector() |
127
|
|
|
{ |
128
|
|
|
return $this->initVector; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Encrypt a data |
133
|
|
|
* |
134
|
|
|
* @param string $data String to encrypt |
135
|
|
|
* |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
|
|
public function encrypt($data) |
139
|
|
|
{ |
140
|
|
|
if (strlen($data) == 0) { |
141
|
|
|
return $data; |
142
|
|
|
} |
143
|
|
|
return mcrypt_generic($this->handle, $data); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Decrypt a data |
148
|
|
|
* |
149
|
|
|
* @param string $data String to decrypt |
150
|
|
|
* |
151
|
|
|
* @return string |
152
|
|
|
*/ |
153
|
|
|
public function decrypt($data) |
154
|
|
|
{ |
155
|
|
|
if (strlen($data) == 0) { |
156
|
|
|
return $data; |
157
|
|
|
} |
158
|
|
|
$data = mdecrypt_generic($this->handle, $data); |
159
|
|
|
/* |
160
|
|
|
* Returned string can in fact be longer than the unencrypted string due to the padding of the data |
161
|
|
|
* @link http://www.php.net/manual/en/function.mdecrypt-generic.php |
162
|
|
|
*/ |
163
|
|
|
$data = rtrim($data, "\0"); |
164
|
|
|
return $data; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|