1 | <?php |
||||
2 | |||||
3 | namespace PhpZip\Util; |
||||
4 | |||||
5 | use PhpZip\Exception\RuntimeException; |
||||
6 | |||||
7 | /** |
||||
8 | * Crypto Utils. |
||||
9 | * |
||||
10 | * @internal |
||||
11 | */ |
||||
12 | class CryptoUtil |
||||
13 | { |
||||
14 | /** |
||||
15 | * Returns random bytes. |
||||
16 | * |
||||
17 | * @param int $length |
||||
18 | * |
||||
19 | * @throws \Exception |
||||
20 | * |
||||
21 | * @return string |
||||
22 | * |
||||
23 | * @deprecated Use random_bytes() |
||||
24 | */ |
||||
25 | final public static function randomBytes($length) |
||||
26 | { |
||||
27 | return random_bytes($length); |
||||
28 | } |
||||
29 | |||||
30 | /** |
||||
31 | * Decrypt AES-CTR. |
||||
32 | * |
||||
33 | * @param string $data Encrypted data |
||||
34 | * @param string $key Aes key |
||||
35 | * @param string $iv Aes IV |
||||
36 | * |
||||
37 | * @return string Raw data |
||||
38 | */ |
||||
39 | 8 | public static function decryptAesCtr($data, $key, $iv) |
|||
40 | { |
||||
41 | 8 | if (\extension_loaded('openssl')) { |
|||
42 | 8 | $numBits = \strlen($key) * 8; |
|||
43 | /** @noinspection PhpComposerExtensionStubsInspection */ |
||||
44 | 8 | return openssl_decrypt($data, 'AES-' . $numBits . '-CTR', $key, \OPENSSL_RAW_DATA, $iv); |
|||
45 | } |
||||
46 | |||||
47 | if (\extension_loaded('mcrypt')) { |
||||
48 | return mcrypt_decrypt(\MCRYPT_RIJNDAEL_128, $key, $data, 'ctr', $iv); |
||||
0 ignored issues
–
show
|
|||||
49 | } |
||||
50 | |||||
51 | throw new RuntimeException('Extension openssl or mcrypt not loaded'); |
||||
52 | } |
||||
53 | |||||
54 | /** |
||||
55 | * Encrypt AES-CTR. |
||||
56 | * |
||||
57 | * @param string $data Raw data |
||||
58 | * @param string $key Aes key |
||||
59 | * @param string $iv Aes IV |
||||
60 | * |
||||
61 | * @return string Encrypted data |
||||
62 | */ |
||||
63 | 9 | public static function encryptAesCtr($data, $key, $iv) |
|||
64 | { |
||||
65 | 9 | if (\extension_loaded('openssl')) { |
|||
66 | 9 | $numBits = \strlen($key) * 8; |
|||
67 | /** @noinspection PhpComposerExtensionStubsInspection */ |
||||
68 | 9 | return openssl_encrypt($data, 'AES-' . $numBits . '-CTR', $key, \OPENSSL_RAW_DATA, $iv); |
|||
69 | } |
||||
70 | |||||
71 | if (\extension_loaded('mcrypt')) { |
||||
72 | return mcrypt_encrypt(\MCRYPT_RIJNDAEL_128, $key, $data, 'ctr', $iv); |
||||
0 ignored issues
–
show
The function
mcrypt_encrypt() has been deprecated: 7.1
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This function has been deprecated. The supplier of the function has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead. ![]() |
|||||
73 | } |
||||
74 | |||||
75 | throw new RuntimeException('Extension openssl or mcrypt not loaded'); |
||||
76 | } |
||||
77 | } |
||||
78 |
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.