| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
@copyright |
|---|
| 6 |
@author will@flourishlib.com |
|---|
| 7 |
@license http://flourishlib.com/license |
|---|
| 8 |
|
|---|
| 9 |
@package |
|---|
| 10 |
@link http://flourishlib.com/fCryptography |
|---|
| 11 |
|
|---|
| 12 |
@version |
|---|
| 13 |
@changes http://php.net/disk_free_space |
|---|
| 14 |
@changes |
|---|
| 15 |
@changes |
|---|
| 16 |
@changes |
|---|
| 17 |
@changes |
|---|
| 18 |
@changes |
|---|
| 19 |
@changes |
|---|
| 20 |
@changes |
|---|
| 21 |
|
|---|
| 22 |
class fCryptography |
|---|
| 23 |
{ |
|---|
| 24 |
|
|---|
| 25 |
const checkPasswordHash = 'fCryptography::checkPasswordHash'; |
|---|
| 26 |
const hashPassword = 'fCryptography::hashPassword'; |
|---|
| 27 |
const publicKeyDecrypt = 'fCryptography::publicKeyDecrypt'; |
|---|
| 28 |
const publicKeyEncrypt = 'fCryptography::publicKeyEncrypt'; |
|---|
| 29 |
const publicKeySign = 'fCryptography::publicKeySign'; |
|---|
| 30 |
const publicKeyVerify = 'fCryptography::publicKeyVerify'; |
|---|
| 31 |
const random = 'fCryptography::random'; |
|---|
| 32 |
const randomString = 'fCryptography::randomString'; |
|---|
| 33 |
const symmetricKeyDecrypt = 'fCryptography::symmetricKeyDecrypt'; |
|---|
| 34 |
const symmetricKeyEncrypt = 'fCryptography::symmetricKeyEncrypt'; |
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
@param |
|---|
| 41 |
@param |
|---|
| 42 |
@return |
|---|
| 43 |
|
|---|
| 44 |
static public function checkPasswordHash($password, $hash) |
|---|
| 45 |
{ |
|---|
| 46 |
$salt = substr($hash, 29, 10); |
|---|
| 47 |
|
|---|
| 48 |
if (self::hashWithSalt($password, $salt) == $hash) { |
|---|
| 49 |
return TRUE; |
|---|
| 50 |
} |
|---|
| 51 |
|
|---|
| 52 |
return FALSE; |
|---|
| 53 |
} |
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
@throws |
|---|
| 60 |
|
|---|
| 61 |
@param |
|---|
| 62 |
@param |
|---|
| 63 |
@return |
|---|
| 64 |
|
|---|
| 65 |
static private function createPrivateKeyResource($private_key_file, $password) |
|---|
| 66 |
{ |
|---|
| 67 |
if (!file_exists($private_key_file)) { |
|---|
| 68 |
throw new fProgrammerException( |
|---|
| 69 |
'The path to the PEM-encoded private key specified, %s, is not valid', |
|---|
| 70 |
$private_key_file |
|---|
| 71 |
); |
|---|
| 72 |
} |
|---|
| 73 |
if (!is_readable($private_key_file)) { |
|---|
| 74 |
throw new fEnvironmentException( |
|---|
| 75 |
'The PEM-encoded private key specified, %s, is not readable', |
|---|
| 76 |
$private_key_file |
|---|
| 77 |
); |
|---|
| 78 |
} |
|---|
| 79 |
|
|---|
| 80 |
$private_key = file_get_contents($private_key_file); |
|---|
| 81 |
$private_key_resource = openssl_pkey_get_private($private_key, $password); |
|---|
| 82 |
|
|---|
| 83 |
if ($private_key_resource === FALSE) { |
|---|
| 84 |
throw new fValidationException( |
|---|
| 85 |
'The private key file specified, %s, does not appear to be a valid private key or the password provided is incorrect', |
|---|
| 86 |
$private_key_file |
|---|
| 87 |
); |
|---|
| 88 |
} |
|---|
| 89 |
|
|---|
| 90 |
return $private_key_resource; |
|---|
| 91 |
} |
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
@param |
|---|
| 98 |
@return |
|---|
| 99 |
|
|---|
| 100 |
static private function createPublicKeyResource($public_key_file) |
|---|
| 101 |
{ |
|---|
| 102 |
if (!file_exists($public_key_file)) { |
|---|
| 103 |
throw new fProgrammerException( |
|---|
| 104 |
'The path to the X.509 certificate specified, %s, is not valid', |
|---|
| 105 |
$public_key_file |
|---|
| 106 |
); |
|---|
| 107 |
} |
|---|
| 108 |
if (!is_readable($public_key_file)) { |
|---|
| 109 |
throw new fEnvironmentException( |
|---|
| 110 |
'The X.509 certificate specified, %s, can not be read', |
|---|
| 111 |
$public_key_file |
|---|
| 112 |
); |
|---|
| 113 |
} |
|---|
| 114 |
|
|---|
| 115 |
$public_key = file_get_contents($public_key_file); |
|---|
| 116 |
$public_key_resource = openssl_pkey_get_public($public_key); |
|---|
| 117 |
|
|---|
| 118 |
if ($public_key_resource === FALSE) { |
|---|
| 119 |
throw new fProgrammerException( |
|---|
| 120 |
'The public key certificate specified, %s, does not appear to be a valid certificate', |
|---|
| 121 |
$public_key_file |
|---|
| 122 |
); |
|---|
| 123 |
} |
|---|
| 124 |
|
|---|
| 125 |
return $public_key_resource; |
|---|
| 126 |
} |
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 |
@param |
|---|
| 133 |
@return |
|---|
| 134 |
|
|---|
| 135 |
static public function hashPassword($password) |
|---|
| 136 |
{ |
|---|
| 137 |
$salt = self::randomString(10); |
|---|
| 138 |
|
|---|
| 139 |
return self::hashWithSalt($password, $salt); |
|---|
| 140 |
} |
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 |
@param |
|---|
| 147 |
@param |
|---|
| 148 |
@return |
|---|
| 149 |
|
|---|
| 150 |
static private function hashWithSalt($source, $salt) |
|---|
| 151 |
{ |
|---|
| 152 |
$sha1 = sha1($salt . $source); |
|---|
| 153 |
for ($i = 0; $i < 1000; $i++) { |
|---|
| 154 |
$sha1 = sha1($sha1 . (($i % 2 == 0) ? $source : $salt)); |
|---|
| 155 |
} |
|---|
| 156 |
|
|---|
| 157 |
return 'fCryptography::password_hash#' . $salt . '#' . $sha1; |
|---|
| 158 |
} |
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 |
@throws |
|---|
| 168 |
|
|---|
| 169 |
@param |
|---|
| 170 |
@param |
|---|
| 171 |
@param |
|---|
| 172 |
@return |
|---|
| 173 |
|
|---|
| 174 |
static public function publicKeyDecrypt($ciphertext, $private_key_file, $password) |
|---|
| 175 |
{ |
|---|
| 176 |
self::verifyPublicKeyEnvironment(); |
|---|
| 177 |
|
|---|
| 178 |
$private_key_resource = self::createPrivateKeyResource($private_key_file, $password); |
|---|
| 179 |
|
|---|
| 180 |
$elements = explode('#', $ciphertext); |
|---|
| 181 |
|
|---|
| 182 |
|
|---|
| 183 |
if (sizeof($elements) != 4 || $elements[0] != 'fCryptography::public') { |
|---|
| 184 |
throw new fProgrammerException( |
|---|
| 185 |
'The ciphertext provided does not appear to have been encrypted using %s', |
|---|
| 186 |
__CLASS__ . '::publicKeyEncrypt()' |
|---|
| 187 |
); |
|---|
| 188 |
} |
|---|
| 189 |
|
|---|
| 190 |
$encrypted_key = base64_decode($elements[1]); |
|---|
| 191 |
$ciphertext = base64_decode($elements[2]); |
|---|
| 192 |
$provided_hmac = $elements[3]; |
|---|
| 193 |
|
|---|
| 194 |
$plaintext = ''; |
|---|
| 195 |
$result = openssl_open($ciphertext, $plaintext, $encrypted_key, $private_key_resource); |
|---|
| 196 |
openssl_free_key($private_key_resource); |
|---|
| 197 |
|
|---|
| 198 |
if ($result === FALSE) { |
|---|
| 199 |
throw new fEnvironmentException( |
|---|
| 200 |
'There was an unknown error decrypting the ciphertext provided' |
|---|
| 201 |
); |
|---|
| 202 |
} |
|---|
| 203 |
|
|---|
| 204 |
$hmac = hash_hmac('sha1', $encrypted_key . $ciphertext, $plaintext); |
|---|
| 205 |
|
|---|
| 206 |
|
|---|
| 207 |
if ($hmac != $provided_hmac) { |
|---|
| 208 |
throw new fValidationException( |
|---|
| 209 |
'The ciphertext provided appears to have been tampered with or corrupted' |
|---|
| 210 |
); |
|---|
| 211 |
} |
|---|
| 212 |
|
|---|
| 213 |
return $plaintext; |
|---|
| 214 |
} |
|---|
| 215 |
|
|---|
| 216 |
|
|---|
| 217 |
|
|---|
| 218 |
|
|---|
| 219 |
|
|---|
| 220 |
|
|---|
| 221 |
|
|---|
| 222 |
|
|---|
| 223 |
@param |
|---|
| 224 |
@param |
|---|
| 225 |
@return |
|---|
| 226 |
|
|---|
| 227 |
static public function publicKeyEncrypt($plaintext, $public_key_file) |
|---|
| 228 |
{ |
|---|
| 229 |
self::verifyPublicKeyEnvironment(); |
|---|
| 230 |
|
|---|
| 231 |
$public_key_resource = self::createPublicKeyResource($public_key_file); |
|---|
| 232 |
|
|---|
| 233 |
$ciphertext = ''; |
|---|
| 234 |
$encrypted_keys = array(); |
|---|
| 235 |
$result = openssl_seal($plaintext, $ciphertext, $encrypted_keys, array($public_key_resource)); |
|---|
| 236 |
openssl_free_key($public_key_resource); |
|---|
| 237 |
|
|---|
| 238 |
if ($result === FALSE) { |
|---|
| 239 |
throw new fEnvironmentException( |
|---|
| 240 |
'There was an unknown error encrypting the plaintext provided' |
|---|
| 241 |
); |
|---|
| 242 |
} |
|---|
| 243 |
|
|---|
| 244 |
$hmac = hash_hmac('sha1', $encrypted_keys[0] . $ciphertext, $plaintext); |
|---|
| 245 |
|
|---|
| 246 |
return 'fCryptography::public#' . base64_encode($encrypted_keys[0]) . '#' . base64_encode($ciphertext) . '#' . $hmac; |
|---|
| 247 |
} |
|---|
| 248 |
|
|---|
| 249 |
|
|---|
| 250 |
|
|---|
| 251 |
|
|---|
| 252 |
|
|---|
| 253 |
|
|---|
| 254 |
|
|---|
| 255 |
|
|---|
| 256 |
@throws |
|---|
| 257 |
|
|---|
| 258 |
@param |
|---|
| 259 |
@param |
|---|
| 260 |
@param |
|---|
| 261 |
@return |
|---|
| 262 |
|
|---|
| 263 |
static public function publicKeySign($plaintext, $private_key_file, $password) |
|---|
| 264 |
{ |
|---|
| 265 |
self::verifyPublicKeyEnvironment(); |
|---|
| 266 |
|
|---|
| 267 |
$private_key_resource = self::createPrivateKeyResource($private_key_file, $password); |
|---|
| 268 |
|
|---|
| 269 |
$result = openssl_sign($plaintext, $signature, $private_key_resource); |
|---|
| 270 |
openssl_free_key($private_key_resource); |
|---|
| 271 |
|
|---|
| 272 |
if (!$result) { |
|---|
| 273 |
throw new fEnvironmentException( |
|---|
| 274 |
'There was an unknown error signing the plaintext' |
|---|
| 275 |
); |
|---|
| 276 |
} |
|---|
| 277 |
|
|---|
| 278 |
return base64_encode($signature); |
|---|
| 279 |
} |
|---|
| 280 |
|
|---|
| 281 |
|
|---|
| 282 |
|
|---|
| 283 |
|
|---|
| 284 |
|
|---|
| 285 |
|
|---|
| 286 |
|
|---|
| 287 |
|
|---|
| 288 |
@param |
|---|
| 289 |
@param |
|---|
| 290 |
@param |
|---|
| 291 |
@return |
|---|
| 292 |
|
|---|
| 293 |
static public function publicKeyVerify($plaintext, $signature, $public_key_file) |
|---|
| 294 |
{ |
|---|
| 295 |
self::verifyPublicKeyEnvironment(); |
|---|
| 296 |
|
|---|
| 297 |
$public_key_resource = self::createPublicKeyResource($public_key_file); |
|---|
| 298 |
|
|---|
| 299 |
$result = openssl_verify($plaintext, base64_decode($signature), $public_key_resource); |
|---|
| 300 |
openssl_free_key($public_key_resource); |
|---|
| 301 |
|
|---|
| 302 |
if ($result === -1) { |
|---|
| 303 |
throw new fEnvironmentException( |
|---|
| 304 |
'There was an unknown error verifying the plaintext and signature against the public key specified' |
|---|
| 305 |
); |
|---|
| 306 |
} |
|---|
| 307 |
|
|---|
| 308 |
return ($result === 1) ? TRUE : FALSE; |
|---|
| 309 |
} |
|---|
| 310 |
|
|---|
| 311 |
|
|---|
| 312 |
|
|---|
| 313 |
http://php.net/mt_rand |
|---|
| 314 |
|
|---|
| 315 |
@param |
|---|
| 316 |
@param |
|---|
| 317 |
@return |
|---|
| 318 |
|
|---|
| 319 |
static public function random($min=NULL, $max=NULL) |
|---|
| 320 |
{ |
|---|
| 321 |
self::seedRandom(); |
|---|
| 322 |
if ($min !== NULL || $max !== NULL) { |
|---|
| 323 |
return mt_rand($min, $max); |
|---|
| 324 |
} |
|---|
| 325 |
return mt_rand(); |
|---|
| 326 |
} |
|---|
| 327 |
|
|---|
| 328 |
|
|---|
| 329 |
|
|---|
| 330 |
|
|---|
| 331 |
|
|---|
| 332 |
@param |
|---|
| 333 |
@param |
|---|
| 334 |
@return |
|---|
| 335 |
|
|---|
| 336 |
static public function randomString($length, $type='alphanumeric') |
|---|
| 337 |
{ |
|---|
| 338 |
if ($length < 1) { |
|---|
| 339 |
throw new fProgrammerException( |
|---|
| 340 |
'The length specified, %1$s, is less than the minimum of %2$s', |
|---|
| 341 |
$length, |
|---|
| 342 |
1 |
|---|
| 343 |
); |
|---|
| 344 |
} |
|---|
| 345 |
|
|---|
| 346 |
switch ($type) { |
|---|
| 347 |
case 'alphanumeric': |
|---|
| 348 |
$alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; |
|---|
| 349 |
break; |
|---|
| 350 |
|
|---|
| 351 |
case 'alpha': |
|---|
| 352 |
$alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
|---|
| 353 |
break; |
|---|
| 354 |
|
|---|
| 355 |
case 'numeric': |
|---|
| 356 |
$alphabet = '0123456789'; |
|---|
| 357 |
break; |
|---|
| 358 |
|
|---|
| 359 |
case 'hexadecimal': |
|---|
| 360 |
$alphabet = 'abcdef0123456789'; |
|---|
| 361 |
break; |
|---|
| 362 |
|
|---|
| 363 |
default: |
|---|
| 364 |
throw new fProgrammerException( |
|---|
| 365 |
'The type specified, %1$s, is invalid. Must be one of: %2$s.', |
|---|
| 366 |
$type |
|---|
| 367 |
); |
|---|
| 368 |
} |
|---|
| 369 |
|
|---|
| 370 |
$alphabet_length = strlen($alphabet); |
|---|
| 371 |
$output = ''; |
|---|
| 372 |
|
|---|
| 373 |
for ($i = 0; $i < $length; $i++) { |
|---|
| 374 |
$output .= $alphabet[self::random(0, $alphabet_length-1)]; |
|---|
| 375 |
} |
|---|
| 376 |
|
|---|
| 377 |
return $output; |
|---|
| 378 |
} |
|---|
| 379 |
|
|---|
| 380 |
|
|---|
| 381 |
|
|---|
| 382 |
|
|---|
| 383 |
|
|---|
| 384 |
@return |
|---|
| 385 |
|
|---|
| 386 |
static private function seedRandom() |
|---|
| 387 |
{ |
|---|
| 388 |
static $seeded = FALSE; |
|---|
| 389 |
|
|---|
| 390 |
if ($seeded) { |
|---|
| 391 |
return; |
|---|
| 392 |
} |
|---|
| 393 |
|
|---|
| 394 |
$old_level = error_reporting(error_reporting() & ~E_WARNING); |
|---|
| 395 |
|
|---|
| 396 |
$bytes = NULL; |
|---|
| 397 |
|
|---|
| 398 |
|
|---|
| 399 |
if (!fCore::checkOS('windows') && $handle = fopen('/dev/urandom', 'rb')) { |
|---|
| 400 |
$bytes = fread($handle, 4); |
|---|
| 401 |
fclose($handle); |
|---|
| 402 |
|
|---|
| 403 |
|
|---|
| 404 |
} elseif (fCore::checkOS('windows') && class_exists('COM', FALSE)) { |
|---|
| 405 |
try { |
|---|
| 406 |
|
|---|
| 407 |
$capi = new COM('CAPICOM.Utilities.1'); |
|---|
| 408 |
$bytes = base64_decode($capi->getrandom(4, 0)); |
|---|
| 409 |
unset($capi); |
|---|
| 410 |
} catch (Exception $e) { } |
|---|
| 411 |
} |
|---|
| 412 |
|
|---|
| 413 |
|
|---|
| 414 |
if (!$bytes) { |
|---|
| 415 |
$string = microtime(TRUE) . uniqid('', TRUE) . join('', stat(__FILE__)) . disk_free_space(dirname(__FILE__)); |
|---|
| 416 |
$bytes = substr(pack('H*', md5($string)), 0, 4); |
|---|
| 417 |
} |
|---|
| 418 |
|
|---|
| 419 |
error_reporting($old_level); |
|---|
| 420 |
|
|---|
| 421 |
$seed = (int) (base_convert(bin2hex($bytes), 16, 10) - 2147483647); |
|---|
| 422 |
|
|---|
| 423 |
mt_srand($seed); |
|---|
| 424 |
|
|---|
| 425 |
$seeded = TRUE; |
|---|
| 426 |
} |
|---|
| 427 |
|
|---|
| 428 |
|
|---|
| 429 |
|
|---|
| 430 |
|
|---|
| 431 |
|
|---|
| 432 |
|
|---|
| 433 |
|
|---|
| 434 |
|
|---|
| 435 |
@throws |
|---|
| 436 |
|
|---|
| 437 |
@param |
|---|
| 438 |
@param |
|---|
| 439 |
@return |
|---|
| 440 |
|
|---|
| 441 |
static public function symmetricKeyDecrypt($ciphertext, $secret_key) |
|---|
| 442 |
{ |
|---|
| 443 |
self::verifySymmetricKeyEnvironment(); |
|---|
| 444 |
|
|---|
| 445 |
$elements = explode('#', $ciphertext); |
|---|
| 446 |
|
|---|
| 447 |
|
|---|
| 448 |
if (sizeof($elements) != 4 || $elements[0] != 'fCryptography::symmetric') { |
|---|
| 449 |
throw new fProgrammerException( |
|---|
| 450 |
'The ciphertext provided does not appear to have been encrypted using %s', |
|---|
| 451 |
__CLASS__ . '::symmetricKeyEncrypt()' |
|---|
| 452 |
); |
|---|
| 453 |
} |
|---|
| 454 |
|
|---|
| 455 |
$iv = base64_decode($elements[1]); |
|---|
| 456 |
$ciphertext = base64_decode($elements[2]); |
|---|
| 457 |
$provided_hmac = $elements[3]; |
|---|
| 458 |
|
|---|
| 459 |
$hmac = hash_hmac('sha1', $iv . '#' . $ciphertext, $secret_key); |
|---|
| 460 |
|
|---|
| 461 |
|
|---|
| 462 |
if ($hmac != $provided_hmac) { |
|---|
| 463 |
throw new fValidationException( |
|---|
| 464 |
'The ciphertext provided appears to have been tampered with or corrupted' |
|---|
| 465 |
); |
|---|
| 466 |
} |
|---|
| 467 |
|
|---|
| 468 |
|
|---|
| 469 |
$module = mcrypt_module_open('rijndael-192', '', 'cfb', ''); |
|---|
| 470 |
$key = substr(sha1($secret_key), 0, mcrypt_enc_get_key_size($module)); |
|---|
| 471 |
mcrypt_generic_init($module, $key, $iv); |
|---|
| 472 |
|
|---|
| 473 |
$old_level = error_reporting(error_reporting() & ~E_WARNING); |
|---|
| 474 |
$plaintext = mdecrypt_generic($module, $ciphertext); |
|---|
| 475 |
error_reporting($old_level); |
|---|
| 476 |
|
|---|
| 477 |
mcrypt_generic_deinit($module); |
|---|
| 478 |
mcrypt_module_close($module); |
|---|
| 479 |
|
|---|
| 480 |
return $plaintext; |
|---|
| 481 |
} |
|---|
| 482 |
|
|---|
| 483 |
|
|---|
| 484 |
|
|---|
| 485 |
|
|---|
| 486 |
|
|---|
| 487 |
|
|---|
| 488 |
|
|---|
| 489 |
|
|---|
| 490 |
@throws |
|---|
| 491 |
|
|---|
| 492 |
@param |
|---|
| 493 |
@param |
|---|
| 494 |
@return |
|---|
| 495 |
|
|---|
| 496 |
static public function symmetricKeyEncrypt($plaintext, $secret_key) |
|---|
| 497 |
{ |
|---|
| 498 |
if (strlen($secret_key) < 8) { |
|---|
| 499 |
throw new fValidationException( |
|---|
| 500 |
'The secret key specified does not meet the minimum requirement of being at least %s characters long', |
|---|
| 501 |
8 |
|---|
| 502 |
); |
|---|
| 503 |
} |
|---|
| 504 |
|
|---|
| 505 |
self::verifySymmetricKeyEnvironment(); |
|---|
| 506 |
|
|---|
| 507 |
|
|---|
| 508 |
|
|---|
| 509 |
|
|---|
| 510 |
$module = mcrypt_module_open('rijndael-192', '', 'cfb', ''); |
|---|
| 511 |
$key = substr(sha1($secret_key), 0, mcrypt_enc_get_key_size($module)); |
|---|
| 512 |
srand(); |
|---|
| 513 |
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($module), MCRYPT_RAND); |
|---|
| 514 |
|
|---|
| 515 |
|
|---|
| 516 |
mcrypt_generic_init($module, $key, $iv); |
|---|
| 517 |
|
|---|
| 518 |
$old_level = error_reporting(error_reporting() & ~E_WARNING); |
|---|
| 519 |
$ciphertext = mcrypt_generic($module, $plaintext); |
|---|
| 520 |
error_reporting($old_level); |
|---|
| 521 |
|
|---|
| 522 |
|
|---|
| 523 |
mcrypt_generic_deinit($module); |
|---|
| 524 |
mcrypt_module_close($module); |
|---|
| 525 |
|
|---|
| 526 |
|
|---|
| 527 |
$hmac = hash_hmac('sha1', $iv . '#' . $ciphertext, $secret_key); |
|---|
| 528 |
|
|---|
| 529 |
|
|---|
| 530 |
$encoded_iv = base64_encode($iv); |
|---|
| 531 |
$encoded_ciphertext = base64_encode($ciphertext); |
|---|
| 532 |
|
|---|
| 533 |
|
|---|
| 534 |
return 'fCryptography::symmetric#' . $encoded_iv . '#' . $encoded_ciphertext . '#' . $hmac; |
|---|
| 535 |
} |
|---|
| 536 |
|
|---|
| 537 |
|
|---|
| 538 |
|
|---|
| 539 |
|
|---|
| 540 |
|
|---|
| 541 |
@return |
|---|
| 542 |
|
|---|
| 543 |
static private function verifyPublicKeyEnvironment() |
|---|
| 544 |
{ |
|---|
| 545 |
if (!extension_loaded('openssl')) { |
|---|
| 546 |
throw new fEnvironmentException( |
|---|
| 547 |
'The PHP %s extension is required, however is does not appear to be loaded', |
|---|
| 548 |
'openssl' |
|---|
| 549 |
); |
|---|
| 550 |
} |
|---|
| 551 |
} |
|---|
| 552 |
|
|---|
| 553 |
|
|---|
| 554 |
|
|---|
| 555 |
|
|---|
| 556 |
|
|---|
| 557 |
@return |
|---|
| 558 |
|
|---|
| 559 |
static private function verifySymmetricKeyEnvironment() |
|---|
| 560 |
{ |
|---|
| 561 |
if (!extension_loaded('mcrypt')) { |
|---|
| 562 |
throw new fEnvironmentException( |
|---|
| 563 |
'The PHP %s extension is required, however is does not appear to be loaded', |
|---|
| 564 |
'mcrypt' |
|---|
| 565 |
); |
|---|
| 566 |
} |
|---|
| 567 |
if (!extension_loaded('hash')) { |
|---|
| 568 |
throw new fEnvironmentException( |
|---|
| 569 |
'The PHP %s extension is required, however is does not appear to be loaded', |
|---|
| 570 |
'hash' |
|---|
| 571 |
); |
|---|
| 572 |
} |
|---|
| 573 |
if (!function_exists('mcrypt_module_open')) { |
|---|
| 574 |
throw new fEnvironmentException( |
|---|
| 575 |
'The cipher used, %1$s (also known as %2$s), requires libmcrypt version 2.4.x or newer. The version installed does not appear to meet this requirement.', |
|---|
| 576 |
'AES-192', |
|---|
| 577 |
'rijndael-192' |
|---|
| 578 |
); |
|---|
| 579 |
} |
|---|
| 580 |
if (!in_array('rijndael-192', mcrypt_list_algorithms())) { |
|---|
| 581 |
throw new fEnvironmentException( |
|---|
| 582 |
'The cipher used, %1$s (also known as %2$s), does not appear to be supported by the installed version of libmcrypt', |
|---|
| 583 |
'AES-192', |
|---|
| 584 |
'rijndael-192' |
|---|
| 585 |
); |
|---|
| 586 |
} |
|---|
| 587 |
} |
|---|
| 588 |
|
|---|
| 589 |
|
|---|
| 590 |
|
|---|
| 591 |
|
|---|
| 592 |
|
|---|
| 593 |
@return |
|---|
| 594 |
|
|---|
| 595 |
private function __construct() { } |
|---|
| 596 |
} |
|---|
| 597 |
|
|---|
| 598 |
|
|---|
| 599 |
|
|---|
| 600 |
|
|---|
| 601 |
will@flourishlib.com |
|---|
| 602 |
|
|---|
| 603 |
|
|---|
| 604 |
|
|---|
| 605 |
|
|---|
| 606 |
|
|---|
| 607 |
|
|---|
| 608 |
|
|---|
| 609 |
|
|---|
| 610 |
|
|---|
| 611 |
|
|---|
| 612 |
|
|---|
| 613 |
|
|---|
| 614 |
|
|---|
| 615 |
|
|---|
| 616 |
|
|---|
| 617 |
|
|---|
| 618 |
|
|---|
| 619 |
|
|---|
| 620 |
|
|---|