[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * Joomla! Content Management System 5 * 6 * @copyright (C) 2017 Open Source Matters, Inc. <https://www.joomla.org> 7 * @license GNU General Public License version 2 or later; see LICENSE.txt 8 */ 9 10 namespace Joomla\CMS\Crypt\Cipher; 11 12 use Joomla\Crypt\CipherInterface; 13 use Joomla\Crypt\Key; 14 use ParagonIE\Sodium\Compat; 15 16 // phpcs:disable PSR1.Files.SideEffects 17 \defined('JPATH_PLATFORM') or die; 18 // phpcs:enable PSR1.Files.SideEffects 19 20 /** 21 * JCrypt cipher for sodium algorithm encryption, decryption and key generation. 22 * 23 * @since 3.8.0 24 */ 25 class SodiumCipher implements CipherInterface 26 { 27 /** 28 * The message nonce to be used with encryption/decryption 29 * 30 * @var string 31 * @since 3.8.0 32 */ 33 private $nonce; 34 35 /** 36 * Method to decrypt a data string. 37 * 38 * @param string $data The encrypted string to decrypt. 39 * @param Key $key The key object to use for decryption. 40 * 41 * @return string The decrypted data string. 42 * 43 * @since 3.8.0 44 * @throws \RuntimeException 45 */ 46 public function decrypt($data, Key $key) 47 { 48 // Validate key. 49 if ($key->getType() !== 'sodium') { 50 throw new \InvalidArgumentException('Invalid key of type: ' . $key->getType() . '. Expected sodium.'); 51 } 52 53 if (!$this->nonce) { 54 throw new \RuntimeException('Missing nonce to decrypt data'); 55 } 56 57 $decrypted = Compat::crypto_box_open( 58 $data, 59 $this->nonce, 60 Compat::crypto_box_keypair_from_secretkey_and_publickey($key->getPrivate(), $key->getPublic()) 61 ); 62 63 if ($decrypted === false) { 64 throw new \RuntimeException('Malformed message or invalid MAC'); 65 } 66 67 return $decrypted; 68 } 69 70 /** 71 * Method to encrypt a data string. 72 * 73 * @param string $data The data string to encrypt. 74 * @param Key $key The key object to use for encryption. 75 * 76 * @return string The encrypted data string. 77 * 78 * @since 3.8.0 79 * @throws \RuntimeException 80 */ 81 public function encrypt($data, Key $key) 82 { 83 // Validate key. 84 if ($key->getType() !== 'sodium') { 85 throw new \InvalidArgumentException('Invalid key of type: ' . $key->getType() . '. Expected sodium.'); 86 } 87 88 if (!$this->nonce) { 89 throw new \RuntimeException('Missing nonce to decrypt data'); 90 } 91 92 return Compat::crypto_box( 93 $data, 94 $this->nonce, 95 Compat::crypto_box_keypair_from_secretkey_and_publickey($key->getPrivate(), $key->getPublic()) 96 ); 97 } 98 99 /** 100 * Method to generate a new encryption key object. 101 * 102 * @param array $options Key generation options. 103 * 104 * @return Key 105 * 106 * @since 3.8.0 107 * @throws \RuntimeException 108 */ 109 public function generateKey(array $options = array()) 110 { 111 // Generate the encryption key. 112 $pair = Compat::crypto_box_keypair(); 113 114 return new Key('sodium', Compat::crypto_box_secretkey($pair), Compat::crypto_box_publickey($pair)); 115 } 116 117 /** 118 * Check if the cipher is supported in this environment. 119 * 120 * @return boolean 121 * 122 * @since 4.0.0 123 */ 124 public static function isSupported(): bool 125 { 126 return class_exists(Compat::class); 127 } 128 129 /** 130 * Set the nonce to use for encrypting/decrypting messages 131 * 132 * @param string $nonce The message nonce 133 * 134 * @return void 135 * 136 * @since 3.8.0 137 */ 138 public function setNonce($nonce) 139 { 140 $this->nonce = $nonce; 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 |