1 | <?php |
||
27 | class OpensslBridge |
||
28 | { |
||
29 | /** |
||
30 | * This string is used when hashing to ensure cross compatibility between |
||
31 | * dcrypt\mcrypt and dcrypt\aes. Since v7, this is only needed for backwards |
||
32 | * compatibility with older versions |
||
33 | */ |
||
34 | const RIJNDA = 'rijndael-128'; |
||
35 | |||
36 | /** |
||
37 | * Decrypt cyphertext |
||
38 | * |
||
39 | * @param string $data Cyphertext to decrypt |
||
40 | * @param string $pass Password that should be used to decrypt input data |
||
41 | * @param int $cost Number of extra HMAC iterations to perform on key |
||
42 | * @return string |
||
43 | */ |
||
44 | public static function decrypt(string $data, string $pass, int $cost = 0): string |
||
67 | |||
68 | /** |
||
69 | * Encrypt plaintext |
||
70 | * |
||
71 | * @param string $data Plaintext string to encrypt. |
||
72 | * @param string $pass Password used to encrypt data. |
||
73 | * @param int $cost Number of extra HMAC iterations to perform on key |
||
74 | * @return string |
||
75 | */ |
||
76 | public static function encrypt(string $data, string $pass, int $cost = 0): string |
||
93 | |||
94 | /** |
||
95 | * Create a message authentication checksum. |
||
96 | * |
||
97 | * @param string $data Ciphertext that needs a checksum. |
||
98 | * @param string $iv Initialization vector. |
||
99 | * @param string $key HMAC key |
||
100 | * @return string |
||
101 | */ |
||
102 | private static function checksum(string $data, string $iv, string $key): string |
||
114 | |||
115 | /** |
||
116 | * Transform password into key and perform iterative HMAC (if specified) |
||
117 | * |
||
118 | * @param string $pass Encryption key |
||
119 | * @param string $iv Initialization vector |
||
120 | * @param int $cost Number of HMAC iterations to perform on key |
||
121 | * @return string |
||
122 | */ |
||
123 | private static function key(string $pass, string $iv, int $cost): string |
||
129 | |||
130 | /** |
||
131 | * Verify checksum during decryption step and throw error if mismatching. |
||
132 | * |
||
133 | * @param string $calculated |
||
134 | * @param string $supplied |
||
135 | * @throws \InvalidArgumentException |
||
136 | */ |
||
137 | private static function checksumVerify(string $calculated, string $supplied) |
||
144 | |||
145 | /** |
||
146 | * Return the encryption mode string. This function is really only needed for backwards |
||
147 | * compatibility. |
||
148 | * |
||
149 | * @return string |
||
150 | */ |
||
151 | private static function mode(): string |
||
169 | |||
170 | /** |
||
171 | * Calculate checksum size |
||
172 | * |
||
173 | * @return int |
||
174 | */ |
||
175 | private static function cksize(): int |
||
179 | |||
180 | /** |
||
181 | * Get iv size |
||
182 | * |
||
183 | * @return int |
||
184 | */ |
||
185 | private static function ivsize(): int |
||
189 | } |
||
190 |