1 | <?php |
||
6 | class Encryptor implements EncryptorInterface |
||
7 | { |
||
8 | /** @var string $secret the secret user key */ |
||
9 | protected $secret = ''; |
||
10 | |||
11 | protected $expire = 2592000; // 30 days |
||
12 | protected $digestAlgo = 'sha256'; |
||
13 | protected $cipherAlgo = 'aes-256-ctr'; |
||
14 | protected $cipherIvLen = 16; // aes-256-ctr length |
||
15 | protected $digestLength = 32; // sha256 length for raw-data |
||
16 | |||
17 | /** |
||
18 | * Encryptor constructor. |
||
19 | * @param string $secret the secret key to be used in openssl_digest |
||
20 | */ |
||
21 | 7 | public function __construct(string $secret) |
|
25 | |||
26 | /** |
||
27 | * Compares 2 hashes |
||
28 | * @param string $digest Digest to compare with |
||
29 | * @param string $message Original message |
||
30 | * @param string $sid Session id |
||
31 | * @return bool |
||
32 | */ |
||
33 | 1 | private function hashesEqual(string $digest, string $message, string $sid): bool |
|
40 | |||
41 | /** |
||
42 | * Encrypt a string. |
||
43 | * |
||
44 | * @param string $in String to encrypt. |
||
45 | * @param string $key Encryption key. |
||
46 | * |
||
47 | * @param string $sid |
||
48 | * @return string The encrypted string. |
||
49 | * @throws OpenSSLException |
||
50 | */ |
||
51 | 2 | public function encryptString(string $in, string $key, string $sid): string |
|
67 | |||
68 | /** |
||
69 | * Decrypt a string. |
||
70 | * |
||
71 | * @param string $in String to decrypt. |
||
72 | * @param string $key Decryption key. |
||
73 | * |
||
74 | * @param string $sid Session id |
||
75 | * |
||
76 | * @return string The decrypted string. |
||
77 | * @throws OpenSSLException |
||
78 | */ |
||
79 | 1 | public function decryptString(string $in, string $key, string $sid): string |
|
114 | |||
115 | /** |
||
116 | * @param int $expire |
||
117 | */ |
||
118 | 1 | public function setExpire($expire) |
|
122 | |||
123 | /** |
||
124 | * @return int |
||
125 | */ |
||
126 | 3 | public function getExpire(): int |
|
130 | |||
131 | /** |
||
132 | * @param string $digestAlgo |
||
133 | */ |
||
134 | 2 | public function setDigestAlgo($digestAlgo) |
|
138 | |||
139 | /** |
||
140 | * @return string |
||
141 | */ |
||
142 | 3 | public function getDigestAlgo(): string |
|
146 | |||
147 | /** |
||
148 | * @param string $cipherAlgo |
||
149 | */ |
||
150 | 2 | public function setCipherAlgo($cipherAlgo) |
|
154 | |||
155 | /** |
||
156 | * @return string |
||
157 | */ |
||
158 | 3 | public function getCipherAlgo(): string |
|
162 | |||
163 | /** |
||
164 | * @param int $cipherIvLen |
||
165 | */ |
||
166 | 1 | public function setCipherIvLen($cipherIvLen) |
|
170 | |||
171 | /** |
||
172 | * @return int |
||
173 | */ |
||
174 | 1 | public function getCipherIvLen(): int |
|
178 | |||
179 | } |