| Total Complexity | 96 |
| Total Lines | 718 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 1 |
Complex classes like EncryptHelper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use EncryptHelper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | class EncryptHelper |
||
| 36 | { |
||
| 37 | use Configurable; |
||
| 38 | |||
| 39 | const DEFAULT_OUTPUT_SIZE = 15; |
||
| 40 | const DEFAULT_DOMAIN_SIZE = 127; |
||
| 41 | const BORING = "brng"; |
||
| 42 | const MODERN = "nacl"; |
||
| 43 | const FIPS = "fips"; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @config |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | private static $forced_encryption = null; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @config |
||
| 53 | * @var bool |
||
| 54 | */ |
||
| 55 | private static $fasthash = false; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @config |
||
| 59 | * @var bool |
||
| 60 | */ |
||
| 61 | private static $automatic_rotation = true; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var boolean |
||
| 65 | */ |
||
| 66 | private static $automatic_decryption = true; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var string |
||
| 70 | */ |
||
| 71 | private static $aad_source = "ID"; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var CipherSweet |
||
| 75 | */ |
||
| 76 | protected static $ciphersweet; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var EncryptedFile |
||
| 80 | */ |
||
| 81 | protected static $encryptedFile; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var array |
||
| 85 | */ |
||
| 86 | protected static $field_cache = []; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @return string |
||
| 90 | */ |
||
| 91 | public static function getForcedEncryption() |
||
| 92 | { |
||
| 93 | return self::config()->forced_encryption; |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @param string $forcedEncryption brng|nacl|fips |
||
| 98 | * @return void |
||
| 99 | */ |
||
| 100 | public static function setForcedEncryption($forcedEncryption) |
||
| 101 | { |
||
| 102 | if ($forcedEncryption && !in_array($forcedEncryption, ["brng", "nacl", "fips"])) { |
||
| 103 | throw new InvalidArgumentException("$forcedEncryption is not supported"); |
||
| 104 | } |
||
| 105 | self::config()->forced_encryption = $forcedEncryption; |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * This would only work if you changed from algorithm |
||
| 110 | * @return bool |
||
| 111 | */ |
||
| 112 | public static function getAutomaticRotation() |
||
| 113 | { |
||
| 114 | return self::config()->automatic_rotation; |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @param bool $setAutomaticRotation |
||
| 119 | * @return void |
||
| 120 | */ |
||
| 121 | public static function setAutomaticRotation($automaticRotation) |
||
| 122 | { |
||
| 123 | self::config()->automatic_rotation = $automaticRotation; |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @return bool |
||
| 128 | */ |
||
| 129 | public static function getAutomaticDecryption() |
||
| 130 | { |
||
| 131 | return self::config()->automatic_decryption; |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @param bool $automaticDecryption |
||
| 136 | * @return void |
||
| 137 | */ |
||
| 138 | public static function setAutomaticDecryption($automaticDecryption) |
||
| 139 | { |
||
| 140 | self::config()->automatic_decryption = $automaticDecryption; |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @return string |
||
| 145 | */ |
||
| 146 | public static function getAadSource() |
||
| 147 | { |
||
| 148 | return self::config()->aad_source; |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @param bool $aadSource |
||
| 153 | * @return void |
||
| 154 | */ |
||
| 155 | public static function setAadSource($aadSource) |
||
| 156 | { |
||
| 157 | self::config()->aad_source = $aadSource; |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @return bool |
||
| 162 | */ |
||
| 163 | public static function getFashHash() |
||
| 164 | { |
||
| 165 | return self::config()->fasthash; |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @param bool $fasthash |
||
| 170 | * @return void |
||
| 171 | */ |
||
| 172 | public static function setFastHash($fasthash) |
||
| 173 | { |
||
| 174 | self::config()->fasthash = $fasthash; |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @link https://ciphersweet.paragonie.com/php/blind-index-planning |
||
| 179 | * @return array |
||
| 180 | */ |
||
| 181 | public static function planIndexSizes() |
||
| 182 | { |
||
| 183 | $dataObjects = ClassInfo::subclassesFor(DataObject::class); |
||
| 184 | $indexes = []; |
||
| 185 | foreach ($dataObjects as $dataObject) { |
||
| 186 | if (!class_uses(HasEncryptedFields::class)) { |
||
| 187 | continue; |
||
| 188 | } |
||
| 189 | $index[$dataObject] = self::planIndexSizesForClass($dataObject); |
||
| 190 | } |
||
| 191 | return $indexes; |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @param string $dataObject |
||
| 196 | * @return array |
||
| 197 | */ |
||
| 198 | public static function planIndexSizesForClass($class) |
||
| 199 | { |
||
| 200 | $sng = singleton($class); |
||
| 201 | $encryptedFields = self::getEncryptedFields($class); |
||
| 202 | // By default, plan for a large number of rows |
||
| 203 | $estimatedPopulation = $class::config()->estimated_population ?? PHP_INT_MAX; |
||
| 204 | $planner = new FieldIndexPlanner(); |
||
| 205 | $planner->setEstimatedPopulation($estimatedPopulation); |
||
| 206 | $indexes = []; |
||
| 207 | foreach ($encryptedFields as $encryptedField => $encryptedClass) { |
||
| 208 | if (!is_subclass_of($encryptedClass, DBComposite::class)) { |
||
| 209 | continue; |
||
| 210 | } |
||
| 211 | $dbObject = $sng->dbObject($encryptedField); |
||
| 212 | $outputSize = $dbObject->getOutputSize() ?? self::DEFAULT_OUTPUT_SIZE; |
||
| 213 | $domainSize = $dbObject->getDomainSize() ?? self::DEFAULT_DOMAIN_SIZE; |
||
| 214 | $planner->addExistingIndex($encryptedField . "BlindIndex", $outputSize, $domainSize); |
||
| 215 | // The smaller of the two values will be used to compute coincidences |
||
| 216 | $indexes[] = ["L" => $outputSize, "K" => $domainSize]; |
||
| 217 | } |
||
| 218 | $coincidenceCount = round(self::coincidenceCount($indexes, $estimatedPopulation)); |
||
| 219 | $recommended = $planner->recommend(); |
||
| 220 | $recommended['indexes'] = count($indexes); |
||
| 221 | // If there is no coincidence, it means the index is not safe for use because it means |
||
| 222 | // that two identical plaintexts will give the same output |
||
| 223 | $recommended['coincidence_count'] = $coincidenceCount; |
||
| 224 | $recommended['coincidence_ratio'] = $coincidenceCount / $estimatedPopulation * 100; |
||
| 225 | $recommended['estimated_population'] = $estimatedPopulation; |
||
| 226 | return $recommended; |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @param DataObject $record |
||
| 231 | * @param string $field |
||
| 232 | * @param boolean $fasthash |
||
| 233 | * @return boolean |
||
| 234 | */ |
||
| 235 | public static function convertHashType($record, $field, $fasthash = true) |
||
| 236 | { |
||
| 237 | /** @var EncryptedDBField $EncryptedDBField */ |
||
| 238 | $EncryptedDBField = $record->dbObject($field); |
||
| 239 | $temp = (string)$record->$field; |
||
| 240 | |||
| 241 | $encryptedField = $EncryptedDBField->getEncryptedField(null, $fasthash); |
||
| 242 | |||
| 243 | $dataForStorage = $encryptedField->prepareForStorage($temp); |
||
| 244 | $encryptedValue = $dataForStorage[0]; |
||
| 245 | $blindIndexes = $dataForStorage[1]; |
||
| 246 | |||
| 247 | $indexSuffix = EncryptedDBField::INDEX_SUFFIX; |
||
| 248 | $valueSuffix = EncryptedDBField::VALUE_SUFFIX; |
||
| 249 | $valueField = $field . $valueSuffix; |
||
| 250 | $indexField = $field . $indexSuffix; |
||
| 251 | |||
| 252 | $prevIndex = $record->$indexField; |
||
| 253 | |||
| 254 | $newValue = $encryptedValue; |
||
| 255 | $newIndex = $blindIndexes[$field . $indexSuffix] ?? null; |
||
| 256 | |||
| 257 | if ($prevIndex != $newIndex) { |
||
| 258 | $table = $EncryptedDBField->getTable(); |
||
| 259 | if (!$table) { |
||
| 260 | throw new Exception("Table not set"); |
||
| 261 | } |
||
| 262 | DB::prepared_query("UPDATE $table SET $indexField = ? WHERE $indexField = ?", [$newIndex, $prevIndex]); |
||
| 263 | return true; |
||
| 264 | } |
||
| 265 | return false; |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @deprecated |
||
| 270 | * @link https://github.com/paragonie/ciphersweet/issues/62 |
||
| 271 | * @param array $ciphertext |
||
| 272 | * @return array |
||
| 273 | */ |
||
| 274 | public static function removeNulls($ciphertext) |
||
| 275 | { |
||
| 276 | foreach ($ciphertext as $k => $v) { |
||
| 277 | if ($v === null) { |
||
| 278 | $ciphertext[$k] = ''; |
||
| 279 | } |
||
| 280 | } |
||
| 281 | return $ciphertext; |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Attempting to pass a key of an invalid size (i.e. not 256-bit) will result in a CryptoOperationException being thrown. |
||
| 286 | * The recommended way to generate a key is to use this method |
||
| 287 | * |
||
| 288 | * @return string A 64 chars string like 4e1c44f87b4cdf21808762970b356891db180a9dd9850e7baf2a79ff3ab8a2fc |
||
| 289 | */ |
||
| 290 | public static function generateKey() |
||
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @return array Two 64 chars strings |
||
| 297 | */ |
||
| 298 | public static function generateKeyPair() |
||
| 299 | { |
||
| 300 | $key_pair = sodium_crypto_box_keypair(); |
||
| 301 | $public_key = sodium_crypto_box_publickey($key_pair); |
||
| 302 | $private_key = sodium_crypto_box_secretkey($key_pair); |
||
| 303 | |||
| 304 | return [ |
||
| 305 | 'public_key' => Hex::encode($public_key), |
||
| 306 | 'private_key' => Hex::encode($private_key), |
||
| 307 | ]; |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Get app encryption key |
||
| 312 | * Encryption key should be provided in your $_ENV or .env file |
||
| 313 | * |
||
| 314 | * @return string |
||
| 315 | */ |
||
| 316 | public static function getKey() |
||
| 317 | { |
||
| 318 | // Try our path variable |
||
| 319 | $keyPath = Environment::getEnv('ENCRYPTION_KEY_PATH'); |
||
| 320 | $key = null; |
||
| 321 | if ($keyPath) { |
||
| 322 | $key = file_get_contents($keyPath); |
||
| 323 | if (!$key || !is_string($key)) { |
||
| 324 | throw new Exception("Could not read key from $keyPath"); |
||
| 325 | } |
||
| 326 | } |
||
| 327 | // Try regular env key |
||
| 328 | if (!$key) { |
||
| 329 | $key = Environment::getEnv('ENCRYPTION_KEY'); |
||
| 330 | } |
||
| 331 | if (!$key) { |
||
| 332 | $key = self::generateKey(); |
||
| 333 | if (Director::isDev()) { |
||
| 334 | $envFile = rtrim(Director::baseFolder(), '/') . "/.env"; |
||
| 335 | if (is_file($envFile) && is_writable($envFile)) { |
||
| 336 | file_put_contents($envFile, 'ENCRYPTION_KEY="' . $key . '"', FILE_APPEND); |
||
| 337 | return $key; |
||
| 338 | } |
||
| 339 | } |
||
| 340 | throw new Exception("Please define an ENCRYPTION_KEY in your environment. You can use this one: $key"); |
||
| 341 | } |
||
| 342 | return $key; |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @return string |
||
| 347 | */ |
||
| 348 | public static function getOldKey() |
||
| 349 | { |
||
| 350 | return Environment::getEnv('OLD_ENCRYPTION_KEY'); |
||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * @param string $key |
||
| 355 | * @return StringProvider |
||
| 356 | */ |
||
| 357 | public static function getProviderWithKey($key = null) |
||
| 358 | { |
||
| 359 | if ($key === null) { |
||
| 360 | $key = self::getKey(); |
||
| 361 | } |
||
| 362 | return new StringProvider($key); |
||
| 363 | } |
||
| 364 | |||
| 365 | /** |
||
| 366 | * @return BackendInterface |
||
| 367 | */ |
||
| 368 | public static function getRecommendedBackend() |
||
| 369 | { |
||
| 370 | if (version_compare(phpversion(), '7.2', '<')) { |
||
| 371 | return new FIPSCrypto(); |
||
| 372 | } |
||
| 373 | return new BoringCrypto(); |
||
| 374 | } |
||
| 375 | |||
| 376 | /** |
||
| 377 | * @param string $encryption |
||
| 378 | * @return BackendInterface |
||
| 379 | */ |
||
| 380 | public static function getBackendForEncryption($encryption = null) |
||
| 381 | { |
||
| 382 | if (!$encryption) { |
||
| 383 | return self::getRecommendedBackend(); |
||
| 384 | } |
||
| 385 | switch ($encryption) { |
||
| 386 | case self::BORING: |
||
| 387 | return new BoringCrypto(); |
||
| 388 | case self::MODERN: |
||
| 389 | return new ModernCrypto(); |
||
| 390 | case self::FIPS: |
||
| 391 | return new FIPSCrypto(); |
||
| 392 | } |
||
| 393 | throw new Exception("Unsupported encryption $encryption"); |
||
| 394 | } |
||
| 395 | |||
| 396 | /** |
||
| 397 | * @param BackendInterface $backend |
||
| 398 | * @param string $key |
||
| 399 | * @return CipherSweet |
||
| 400 | */ |
||
| 401 | public static function getEngineForEncryption($encryption = null, $key = null) |
||
| 402 | { |
||
| 403 | return self::getEngine(self::getBackendForEncryption($encryption), $key); |
||
| 404 | } |
||
| 405 | |||
| 406 | /** |
||
| 407 | * @param BackendInterface $backend |
||
| 408 | * @param string $key |
||
| 409 | * @return CipherSweet |
||
| 410 | */ |
||
| 411 | public static function getEngine(BackendInterface $backend, $key = null) |
||
| 412 | { |
||
| 413 | $provider = self::getProviderWithKey($key); |
||
| 414 | return new CipherSweet($provider, $backend); |
||
| 415 | } |
||
| 416 | |||
| 417 | /** |
||
| 418 | * @param BackendInterface $backend |
||
| 419 | * @param KeyProviderInterface $provider |
||
| 420 | * @return CipherSweet |
||
| 421 | */ |
||
| 422 | public static function getEngineWithProvider(BackendInterface $backend, KeyProviderInterface $provider) |
||
| 425 | } |
||
| 426 | |||
| 427 | /** |
||
| 428 | * @return EncryptedFile |
||
| 429 | */ |
||
| 430 | public static function getEncryptedFileInstance() |
||
| 431 | { |
||
| 432 | if (!self::$encryptedFile) { |
||
| 433 | self::$encryptedFile = new EncryptedFile(self::getCipherSweet()); |
||
| 434 | } |
||
| 435 | return self::$encryptedFile; |
||
| 436 | } |
||
| 437 | |||
| 438 | /** |
||
| 439 | * @param int $ID |
||
| 440 | * @return bool |
||
| 441 | */ |
||
| 442 | public static function checkIfFileIsEncrypted($ID) |
||
| 443 | { |
||
| 444 | return (bool)DB::prepared_query("SELECT Encrypted FROM File WHERE ID = ?", [$ID])->value(); |
||
| 445 | } |
||
| 446 | |||
| 447 | /** |
||
| 448 | * @param KeyProviderInterface $provider |
||
| 449 | * @return CipherSweet |
||
| 450 | */ |
||
| 451 | public static function getCipherSweet($provider = null) |
||
| 452 | { |
||
| 453 | if (self::$ciphersweet) { |
||
| 454 | return self::$ciphersweet; |
||
| 455 | } |
||
| 456 | if ($provider === null) { |
||
| 457 | $provider = self::getProviderWithKey(); |
||
| 458 | } |
||
| 459 | if (self::getForcedEncryption()) { |
||
| 460 | $backend = self::getBackendForEncryption(self::getForcedEncryption()); |
||
| 461 | } else { |
||
| 462 | $backend = self::getRecommendedBackend(); |
||
| 463 | } |
||
| 464 | self::$ciphersweet = new CipherSweet($provider, $backend); |
||
| 465 | return self::$ciphersweet; |
||
| 466 | } |
||
| 467 | |||
| 468 | /** |
||
| 469 | * @return void |
||
| 470 | */ |
||
| 471 | public static function clearCipherSweet() |
||
| 472 | { |
||
| 473 | self::$ciphersweet = null; |
||
| 474 | self::$encryptedFile = null; |
||
| 475 | } |
||
| 476 | |||
| 477 | /** |
||
| 478 | * @return BackendInterface |
||
| 479 | */ |
||
| 480 | public static function getCipherSweetBackend() |
||
| 481 | { |
||
| 482 | return self::getCipherSweet()->getBackend(); |
||
| 483 | } |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Check if a value is encrypted |
||
| 487 | * |
||
| 488 | * @param string $value |
||
| 489 | * @return boolean |
||
| 490 | */ |
||
| 491 | public static function isEncrypted($value) |
||
| 492 | { |
||
| 493 | $prefix = substr($value, 0, 5); |
||
| 494 | return in_array($prefix, ["brng:", "nacl:", "fips:"]); |
||
| 495 | } |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Check if a json value is encrypted |
||
| 499 | * |
||
| 500 | * @param string|array $value |
||
| 501 | * @return boolean |
||
| 502 | */ |
||
| 503 | public static function isJsonEncrypted($value) |
||
| 504 | { |
||
| 505 | if (is_string($value)) { |
||
| 506 | $value = json_decode($value, JSON_OBJECT_AS_ARRAY); |
||
| 507 | } |
||
| 508 | // If any top level value is encrypted |
||
| 509 | foreach ($value as $v) { |
||
| 510 | if (self::isEncrypted($v)) { |
||
| 511 | return true; |
||
| 512 | } |
||
| 513 | } |
||
| 514 | return false; |
||
| 515 | } |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Convert map to a suitable DB field definition |
||
| 519 | * @param JsonFieldMap $map |
||
| 520 | * @return string |
||
| 521 | */ |
||
| 522 | public static function convertJsonMapToDefinition($map) |
||
| 523 | { |
||
| 524 | return str_replace("\"", "\\\"", (string)$map); |
||
| 525 | } |
||
| 526 | |||
| 527 | /** |
||
| 528 | * @param string $value |
||
| 529 | * @return boolean |
||
| 530 | */ |
||
| 531 | public static function isFips($value) |
||
| 532 | { |
||
| 533 | if (strpos($value, 'fips:') === 0) { |
||
| 534 | return true; |
||
| 535 | } |
||
| 536 | return false; |
||
| 537 | } |
||
| 538 | |||
| 539 | /** |
||
| 540 | * @param string $value |
||
| 541 | * @return boolean |
||
| 542 | */ |
||
| 543 | public static function isNacl($value) |
||
| 549 | } |
||
| 550 | |||
| 551 | /** |
||
| 552 | * @param string $value |
||
| 553 | * @return boolean |
||
| 554 | */ |
||
| 555 | public static function isBoring($value) |
||
| 556 | { |
||
| 557 | if (strpos($value, 'brng:') === 0) { |
||
| 558 | return true; |
||
| 559 | } |
||
| 560 | return false; |
||
| 561 | } |
||
| 562 | |||
| 563 | /** |
||
| 564 | * @param string $value |
||
| 565 | * @return string |
||
| 566 | */ |
||
| 567 | public static function getEncryption($value) |
||
| 568 | { |
||
| 569 | if (self::isBoring($value)) { |
||
| 570 | return self::BORING; |
||
| 571 | } |
||
| 572 | if (self::isNacl($value)) { |
||
| 573 | return self::MODERN; |
||
| 574 | } |
||
| 575 | if (self::isFips($value)) { |
||
| 576 | return self::FIPS; |
||
| 577 | } |
||
| 578 | return false; |
||
| 579 | } |
||
| 580 | |||
| 581 | /** |
||
| 582 | * Check if a field is encrypted on a class |
||
| 583 | * This relies on a field class starting with Encrypted |
||
| 584 | * |
||
| 585 | * @param string $class |
||
| 586 | * @param string $field |
||
| 587 | * @return boolean |
||
| 588 | */ |
||
| 589 | public static function isEncryptedField($class, $field) |
||
| 605 | } |
||
| 606 | |||
| 607 | public static function isEncryptedDbClass($dbClass) |
||
| 608 | { |
||
| 609 | return strpos($dbClass, 'Encrypted') !== false; |
||
| 610 | } |
||
| 611 | |||
| 612 | /** |
||
| 613 | * Filters parameters from database class config |
||
| 614 | * @return string |
||
| 615 | */ |
||
| 616 | protected static function filterDbClass($dbClass) |
||
| 617 | { |
||
| 618 | $pos = strpos($dbClass, '('); |
||
| 619 | if ($pos !== false) { |
||
| 620 | $dbClass = substr($dbClass, 0, $pos); |
||
| 621 | } |
||
| 622 | return $dbClass; |
||
| 623 | } |
||
| 624 | |||
| 625 | /** |
||
| 626 | * @param string $class |
||
| 627 | * @param bool $dbFields Return actual database field value instead of field name |
||
| 628 | * @return array An associative array with the name of the field as key and the class as value |
||
| 629 | */ |
||
| 630 | public static function getEncryptedFields($class, $dbFields = false) |
||
| 652 | } |
||
| 653 | |||
| 654 | /** |
||
| 655 | * A simple encryption |
||
| 656 | * @param string $value |
||
| 657 | * @return string |
||
| 658 | */ |
||
| 659 | public static function encrypt($value) |
||
| 660 | { |
||
| 661 | // Do not encrypt twice |
||
| 662 | $encryption = self::getEncryption($value); |
||
| 663 | if ($encryption) { |
||
| 664 | return $value; |
||
| 665 | } |
||
| 666 | $provider = self::getProviderWithKey(); |
||
| 667 | $backend = self::getBackendForEncryption($encryption); |
||
| 668 | return $backend->encrypt($value, $provider->getSymmetricKey()); |
||
| 669 | } |
||
| 670 | |||
| 671 | /** |
||
| 672 | * A simple decryption |
||
| 673 | * @param string $value |
||
| 674 | * @return string |
||
| 675 | */ |
||
| 676 | public static function decrypt($value) |
||
| 686 | } |
||
| 687 | |||
| 688 | /** |
||
| 689 | * Return a map of fields with their encrypted counterpart |
||
| 690 | * |
||
| 691 | * @return array |
||
| 692 | */ |
||
| 693 | public static function mapEncryptionDBField() |
||
| 694 | { |
||
| 695 | return [ |
||
| 696 | DBHTMLText::class => EncryptedDBHTMLText::class, |
||
| 697 | DBText::class => EncryptedDBText::class, |
||
| 698 | DBVarchar::class => EncryptedDBVarchar::class, |
||
| 699 | ]; |
||
| 700 | } |
||
| 701 | |||
| 702 | /** |
||
| 703 | * Compute Blind Index Information Leaks |
||
| 704 | * |
||
| 705 | * @link https://ciphersweet.paragonie.com/php/blind-index-planning |
||
| 706 | * @link https://ciphersweet.paragonie.com/security |
||
| 707 | * @param array $indexes an array of L (output size) / K (domaine size) pairs |
||
| 708 | * @param int $R the number of encrypted records that use this blind index |
||
| 709 | * @return float |
||
| 710 | */ |
||
| 711 | public static function coincidenceCount(array $indexes, $R) |
||
| 719 | } |
||
| 720 | |||
| 721 | /** |
||
| 722 | * Alias of sendDecryptedFile |
||
| 723 | * @deprecated |
||
| 724 | * @param File|EncryptedDBFile $file |
||
| 725 | * @return void |
||
| 726 | */ |
||
| 727 | public static function sendEncryptedFile(File $file) |
||
| 728 | { |
||
| 729 | self::sendDecryptedFile($file); |
||
| 730 | } |
||
| 731 | |||
| 732 | /** |
||
| 733 | * Send a decrypted file |
||
| 734 | * |
||
| 735 | * @param File|EncryptedDBFile $file |
||
| 736 | * @param string $fileName |
||
| 737 | * @param string $mimeType |
||
| 738 | * @return void |
||
| 739 | */ |
||
| 740 | public static function sendDecryptedFile(File $file, $fileName = null, $mimeType = null) |
||
| 753 | } |
||
| 754 | } |
||
| 755 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: