[ 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; 10 11 use Joomla\Crypt\Cipher\Crypto; 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\Exception\UnsupportedCipherException; 17 18 /** 19 * Crypt is a Joomla Framework class for handling basic encryption/decryption of data. 20 * 21 * @since 1.0 22 */ 23 class Crypt 24 { 25 /** 26 * The encryption cipher object. 27 * 28 * @var CipherInterface 29 * @since 1.0 30 */ 31 private $cipher; 32 33 /** 34 * The encryption key[/pair)]. 35 * 36 * @var Key 37 * @since 1.0 38 */ 39 private $key; 40 41 /** 42 * Object Constructor takes an optional key to be used for encryption/decryption. If no key is given then the 43 * secret word from the configuration object is used. 44 * 45 * @param CipherInterface $cipher The encryption cipher object. 46 * @param Key $key The encryption key[/pair)]. 47 * 48 * @since 1.0 49 */ 50 public function __construct(?CipherInterface $cipher = null, ?Key $key = null) 51 { 52 // Set the encryption cipher. 53 $this->cipher = $cipher ?: new Crypto; 54 55 // Set the encryption key[/pair)]. 56 $this->key = $key ?: $this->generateKey(); 57 } 58 59 /** 60 * Method to decrypt a data string. 61 * 62 * @param string $data The encrypted string to decrypt. 63 * 64 * @return string The decrypted data string. 65 * 66 * @since 1.0 67 * @throws DecryptionException if the data cannot be decrypted 68 * @throws InvalidKeyTypeException if the key is not valid for the cipher 69 * @throws UnsupportedCipherException if the cipher is not supported on the current environment 70 */ 71 public function decrypt($data) 72 { 73 return $this->cipher->decrypt($data, $this->key); 74 } 75 76 /** 77 * Method to encrypt a data string. 78 * 79 * @param string $data The data string to encrypt. 80 * 81 * @return string The encrypted data string. 82 * 83 * @since 1.0 84 * @throws EncryptionException if the data cannot be encrypted 85 * @throws InvalidKeyTypeException if the key is not valid for the cipher 86 * @throws UnsupportedCipherException if the cipher is not supported on the current environment 87 */ 88 public function encrypt($data) 89 { 90 return $this->cipher->encrypt($data, $this->key); 91 } 92 93 /** 94 * Method to generate a new encryption key[/pair] object. 95 * 96 * @param array $options Key generation options. 97 * 98 * @return Key 99 * 100 * @since 1.0 101 * @throws InvalidKeyException if the key cannot be generated 102 * @throws UnsupportedCipherException if the cipher is not supported on the current environment 103 */ 104 public function generateKey(array $options = []) 105 { 106 return $this->cipher->generateKey($options); 107 } 108 109 /** 110 * Method to set the encryption key[/pair] object. 111 * 112 * @param Key $key The key object to set. 113 * 114 * @return Crypt Instance of $this to allow chaining. 115 * 116 * @since 1.0 117 */ 118 public function setKey(Key $key) 119 { 120 $this->key = $key; 121 122 return $this; 123 } 124 125 /** 126 * Generate random bytes. 127 * 128 * @param integer $length Length of the random data to generate 129 * 130 * @return string Random binary data 131 * 132 * @since 1.0 133 */ 134 public static function genRandomBytes($length = 16) 135 { 136 return random_bytes($length); 137 } 138 }
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 |