[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Part of the Joomla Framework Crypt Package 4 * 5 * @copyright Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved. 6 * @license GNU General Public License version 2 or later; see LICENSE 7 */ 8 9 namespace Joomla\Crypt\Cipher; 10 11 use Defuse\Crypto\Crypto as DefuseCrypto; 12 use Defuse\Crypto\Exception\EnvironmentIsBrokenException; 13 use Defuse\Crypto\Exception\WrongKeyOrModifiedCiphertextException; 14 use Defuse\Crypto\Key as DefuseKey; 15 use Defuse\Crypto\RuntimeTests; 16 use Joomla\Crypt\CipherInterface; 17 use Joomla\Crypt\Exception\DecryptionException; 18 use Joomla\Crypt\Exception\EncryptionException; 19 use Joomla\Crypt\Exception\InvalidKeyException; 20 use Joomla\Crypt\Exception\InvalidKeyTypeException; 21 use Joomla\Crypt\Key; 22 23 /** 24 * Joomla cipher for encryption, decryption and key generation via the php-encryption library. 25 * 26 * @since 2.0.0 27 */ 28 class Crypto implements CipherInterface 29 { 30 /** 31 * Method to decrypt a data string. 32 * 33 * @param string $data The encrypted string to decrypt. 34 * @param Key $key The key object to use for decryption. 35 * 36 * @return string The decrypted data string. 37 * 38 * @since 2.0.0 39 * @throws DecryptionException if the data cannot be decrypted 40 * @throws InvalidKeyTypeException if the key is not valid for the cipher 41 */ 42 public function decrypt($data, Key $key) 43 { 44 // Validate key. 45 if ($key->getType() !== 'crypto') 46 { 47 throw new InvalidKeyTypeException('crypto', $key->getType()); 48 } 49 50 // Decrypt the data. 51 try 52 { 53 return DefuseCrypto::decrypt($data, DefuseKey::loadFromAsciiSafeString($key->getPrivate())); 54 } 55 catch (WrongKeyOrModifiedCiphertextException $ex) 56 { 57 throw new DecryptionException('DANGER! DANGER! The ciphertext has been tampered with!', $ex->getCode(), $ex); 58 } 59 catch (EnvironmentIsBrokenException $ex) 60 { 61 throw new DecryptionException('Cannot safely perform decryption', $ex->getCode(), $ex); 62 } 63 } 64 65 /** 66 * Method to encrypt a data string. 67 * 68 * @param string $data The data string to encrypt. 69 * @param Key $key The key object to use for encryption. 70 * 71 * @return string The encrypted data string. 72 * 73 * @since 2.0.0 74 * @throws EncryptionException if the data cannot be encrypted 75 * @throws InvalidKeyTypeException if the key is not valid for the cipher 76 */ 77 public function encrypt($data, Key $key) 78 { 79 // Validate key. 80 if ($key->getType() !== 'crypto') 81 { 82 throw new InvalidKeyTypeException('crypto', $key->getType()); 83 } 84 85 // Encrypt the data. 86 try 87 { 88 return DefuseCrypto::encrypt($data, DefuseKey::loadFromAsciiSafeString($key->getPrivate())); 89 } 90 catch (EnvironmentIsBrokenException $ex) 91 { 92 throw new EncryptionException('Cannot safely perform encryption', $ex->getCode(), $ex); 93 } 94 } 95 96 /** 97 * Method to generate a new encryption key object. 98 * 99 * @param array $options Key generation options. 100 * 101 * @return Key 102 * 103 * @since 2.0.0 104 * @throws InvalidKeyException if the key cannot be generated 105 */ 106 public function generateKey(array $options = []) 107 { 108 // Generate the encryption key. 109 try 110 { 111 $public = DefuseKey::createNewRandomKey(); 112 } 113 catch (EnvironmentIsBrokenException $ex) 114 { 115 throw new InvalidKeyException('Cannot safely create a key', $ex->getCode(), $ex); 116 } 117 118 // Create the new encryption key object. 119 return new Key('crypto', $public->saveToAsciiSafeString(), $public->getRawBytes()); 120 } 121 122 /** 123 * Check if the cipher is supported in this environment. 124 * 125 * @return boolean 126 * 127 * @since 2.0.0 128 */ 129 public static function isSupported(): bool 130 { 131 try 132 { 133 RuntimeTests::runtimeTest(); 134 135 return true; 136 } 137 catch (EnvironmentIsBrokenException $e) 138 { 139 return false; 140 } 141 } 142 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |