[ 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 Joomla\Crypt\CipherInterface; 12 use Joomla\Crypt\Exception\DecryptionException; 13 use Joomla\Crypt\Exception\EncryptionException; 14 use Joomla\Crypt\Exception\InvalidKeyException; 15 use Joomla\Crypt\Exception\InvalidKeyTypeException; 16 use Joomla\Crypt\Key; 17 18 /** 19 * Joomla cipher for encryption, decryption and key generation via the openssl extension. 20 * 21 * @since 2.0.0 22 */ 23 class OpenSSL implements CipherInterface 24 { 25 /** 26 * Initialisation vector for key generator method. 27 * 28 * @var string 29 * @since 2.0.0 30 */ 31 private $iv; 32 33 /** 34 * Method to use for encryption. 35 * 36 * @var string 37 * @since 2.0.0 38 */ 39 private $method; 40 41 /** 42 * Instantiate the cipher. 43 * 44 * @param string $iv The initialisation vector to use 45 * @param string $method The encryption method to use 46 * 47 * @since 2.0.0 48 */ 49 public function __construct(string $iv, string $method) 50 { 51 $this->iv = $iv; 52 $this->method = $method; 53 } 54 55 /** 56 * Method to decrypt a data string. 57 * 58 * @param string $data The encrypted string to decrypt. 59 * @param Key $key The key object to use for decryption. 60 * 61 * @return string The decrypted data string. 62 * 63 * @since 2.0.0 64 * @throws DecryptionException if the data cannot be decrypted 65 * @throws InvalidKeyTypeException if the key is not valid for the cipher 66 */ 67 public function decrypt($data, Key $key) 68 { 69 // Validate key. 70 if ($key->getType() !== 'openssl') 71 { 72 throw new InvalidKeyTypeException('openssl', $key->getType()); 73 } 74 75 $cleartext = openssl_decrypt($data, $this->method, $key->getPrivate(), true, $this->iv); 76 77 if ($cleartext === false) 78 { 79 throw new DecryptionException('Failed to decrypt data'); 80 } 81 82 return $cleartext; 83 } 84 85 /** 86 * Method to encrypt a data string. 87 * 88 * @param string $data The data string to encrypt. 89 * @param Key $key The key object to use for encryption. 90 * 91 * @return string The encrypted data string. 92 * 93 * @since 2.0.0 94 * @throws EncryptionException if the data cannot be encrypted 95 * @throws InvalidKeyTypeException if the key is not valid for the cipher 96 */ 97 public function encrypt($data, Key $key) 98 { 99 // Validate key. 100 if ($key->getType() !== 'openssl') 101 { 102 throw new InvalidKeyTypeException('openssl', $key->getType()); 103 } 104 105 $encrypted = openssl_encrypt($data, $this->method, $key->getPrivate(), true, $this->iv); 106 107 if ($encrypted === false) 108 { 109 throw new EncryptionException('Unable to encrypt data'); 110 } 111 112 return $encrypted; 113 } 114 115 /** 116 * Method to generate a new encryption key object. 117 * 118 * @param array $options Key generation options. 119 * 120 * @return Key 121 * 122 * @since 2.0.0 123 * @throws InvalidKeyException if the key cannot be generated 124 */ 125 public function generateKey(array $options = []) 126 { 127 $passphrase = $options['passphrase'] ?? false; 128 129 if ($passphrase === false) 130 { 131 throw new InvalidKeyException('Missing passphrase file'); 132 } 133 134 return new Key('openssl', $passphrase, 'unused'); 135 } 136 137 /** 138 * Check if the cipher is supported in this environment. 139 * 140 * @return boolean 141 * 142 * @since 2.0.0 143 */ 144 public static function isSupported(): bool 145 { 146 return \extension_loaded('openssl'); 147 } 148 }
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 |