1 | <?php |
||
17 | abstract class AesEncryptionStrategy |
||
18 | { |
||
19 | /**#@+ |
||
20 | * Encryption constants |
||
21 | */ |
||
22 | const ENCRYPTION_CIPHER = MCRYPT_RIJNDAEL_128; |
||
23 | const ENCRYPTION_MODE = MCRYPT_MODE_CBC; |
||
24 | /**#@-*/ |
||
25 | |||
26 | /** |
||
27 | * A secret key |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | private $key; |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | private $method; |
||
37 | |||
38 | /** |
||
39 | * Constructor |
||
40 | * |
||
41 | * @param string $key The secret key |
||
42 | * @param string $method |
||
43 | */ |
||
44 | public function __construct($key, $method = AesEnum::METHOD_256) |
||
49 | |||
50 | /** |
||
51 | * Create an initialization vector |
||
52 | * |
||
53 | * @return string |
||
54 | */ |
||
55 | abstract public function createIv(); |
||
56 | |||
57 | /** |
||
58 | * Get the size of the IV |
||
59 | * |
||
60 | * @return int |
||
61 | */ |
||
62 | abstract public function getIvSize(); |
||
63 | |||
64 | /** |
||
65 | * Encrypt data |
||
66 | * |
||
67 | * @param mixed $data |
||
68 | * @param string $iv |
||
69 | * @return mixed |
||
70 | */ |
||
71 | abstract public function encryptData($data, $iv); |
||
72 | |||
73 | /** |
||
74 | * Decrypt data |
||
75 | * |
||
76 | * @param $data |
||
77 | * @param $iv |
||
78 | * @return mixed |
||
79 | */ |
||
80 | abstract public function decryptData($data, $iv); |
||
81 | |||
82 | /** |
||
83 | * Encode data/iv using base64 and pipe-delimited |
||
84 | * |
||
85 | * @param mixed $encryptedData |
||
86 | * @param string $mac |
||
87 | * @param string $iv |
||
88 | * @return string |
||
89 | */ |
||
90 | public function encodeData($encryptedData, $mac, $iv) |
||
94 | |||
95 | /** |
||
96 | * Decodes data/iv and returns array |
||
97 | * |
||
98 | * @param string $data |
||
99 | * @return array |
||
100 | */ |
||
101 | public function decodeData($data) |
||
109 | |||
110 | |||
111 | /** |
||
112 | * Get hmac hash of data |
||
113 | * |
||
114 | * @param $data |
||
115 | * @return string |
||
116 | */ |
||
117 | public function getMac($data) |
||
121 | |||
122 | /** |
||
123 | * Get the key as a packed binary string |
||
124 | * |
||
125 | * @return string |
||
126 | */ |
||
127 | protected function getKey() |
||
141 | |||
142 | /** |
||
143 | * Get the encryption method |
||
144 | * |
||
145 | * @return string |
||
146 | */ |
||
147 | protected function getMethod() |
||
151 | |||
152 | } |
||
153 |