1 | <?php |
||
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) |
||
89 | |||
90 | /** |
||
91 | * Destructor frees allocated resources |
||
92 | * |
||
93 | * @return void |
||
94 | */ |
||
95 | public function __destruct() |
||
100 | |||
101 | /** |
||
102 | * Retrieve a name of currently used cryptographic algorithm |
||
103 | * |
||
104 | * @return string |
||
105 | */ |
||
106 | public function getCipher() |
||
110 | |||
111 | /** |
||
112 | * Mode in which cryptographic algorithm is running |
||
113 | * |
||
114 | * @return string |
||
115 | */ |
||
116 | public function getMode() |
||
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() |
||
130 | |||
131 | /** |
||
132 | * Encrypt a data |
||
133 | * |
||
134 | * @param string $data String to encrypt |
||
135 | * |
||
136 | * @return string |
||
137 | */ |
||
138 | public function encrypt($data) |
||
145 | |||
146 | /** |
||
147 | * Decrypt a data |
||
148 | * |
||
149 | * @param string $data String to decrypt |
||
150 | * |
||
151 | * @return string |
||
152 | */ |
||
153 | public function decrypt($data) |
||
166 | } |
||
167 |