1 | <?php |
||
12 | abstract class AESCipher implements Cipher |
||
13 | { |
||
14 | /** |
||
15 | * Mapping from key size in bits to AES cipher implementation class name. |
||
16 | * |
||
17 | * @internal |
||
18 | * |
||
19 | * @var array |
||
20 | */ |
||
21 | const MAP_KEYSIZE_TO_CLS = array( |
||
22 | /* @formatter:off */ |
||
23 | 128 => AES128Cipher::class, |
||
24 | 192 => AES192Cipher::class, |
||
25 | 256 => AES256Cipher::class |
||
26 | /* @formatter:on */ |
||
27 | ); |
||
28 | |||
29 | /** |
||
30 | * Get the cipher method name recognized by OpenSSL. |
||
31 | * |
||
32 | * @return string |
||
33 | */ |
||
34 | abstract protected function _cipherName(): string; |
||
35 | |||
36 | /** |
||
37 | * Get the key size in bytes. |
||
38 | * |
||
39 | * @return int |
||
40 | */ |
||
41 | abstract protected function _keySize(): int; |
||
42 | |||
43 | /** |
||
44 | * Get AES cipher instance by key length. |
||
45 | * |
||
46 | * @param int $len Key length in bytes |
||
47 | * @throws \UnexpectedValueException |
||
48 | * @return self |
||
49 | */ |
||
50 | 40 | public static function fromKeyLength(int $len): self |
|
60 | |||
61 | /** |
||
62 | * |
||
63 | * @see \Sop\GCM\Cipher\Cipher::encrypt() |
||
64 | * @throws \UnexpectedValueException If key size is incorrect |
||
65 | * @throws \RuntimeException For generic errors |
||
66 | * @return string |
||
67 | */ |
||
68 | 56 | public function encrypt(string $data, string $key): string |
|
83 | |||
84 | /** |
||
85 | * Get latest OpenSSL error message. |
||
86 | * |
||
87 | * @return string |
||
88 | */ |
||
89 | 1 | protected static function _getLastOpenSSLError(): string |
|
97 | } |
||
98 |