| Total Complexity | 9 |
| Total Lines | 45 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 10 | class AES |
||
| 11 | { |
||
| 12 | public static function encrypt(string $text, string $key, string $iv, int $option = OPENSSL_RAW_DATA): string |
||
| 13 | { |
||
| 14 | self::validateKey($key); |
||
| 15 | self::validateIv($iv); |
||
| 16 | |||
| 17 | return openssl_encrypt($text, self::getMode($key), $key, $option, $iv); |
||
| 18 | } |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @param string|null $method |
||
| 22 | */ |
||
| 23 | public static function decrypt(string $cipherText, string $key, string $iv, int $option = OPENSSL_RAW_DATA, $method = null): string |
||
| 24 | { |
||
| 25 | self::validateKey($key); |
||
| 26 | self::validateIv($iv); |
||
| 27 | |||
| 28 | return openssl_decrypt($cipherText, $method ?: self::getMode($key), $key, $option, $iv); |
||
| 29 | } |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @param string $key |
||
| 33 | * |
||
| 34 | * @return string |
||
| 35 | */ |
||
| 36 | public static function getMode($key) |
||
| 37 | { |
||
| 38 | return 'aes-'.(8 * strlen($key)).'-cbc'; |
||
| 39 | } |
||
| 40 | |||
| 41 | public static function validateKey(string $key) |
||
| 45 | } |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @throws \InvalidArgumentException |
||
| 50 | */ |
||
| 51 | public static function validateIv(string $iv) |
||
| 55 | } |
||
| 56 | } |
||
| 57 | } |
||
| 58 |