cipher.php
207 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package VikAppointments |
| 4 | * @subpackage core |
| 5 | * @author E4J s.r.l. |
| 6 | * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved. |
| 7 | * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 | * @link https://vikwp.com |
| 9 | */ |
| 10 | |
| 11 | // No direct access |
| 12 | defined('ABSPATH') or die('No script kiddies please!'); |
| 13 | |
| 14 | /** |
| 15 | * SecureCipher extends JCrypt for encryption, decryption and key generation/storage. |
| 16 | * |
| 17 | * @since 1.6 |
| 18 | */ |
| 19 | final class SecureCipher extends JCryptCipherCrypto |
| 20 | { |
| 21 | /** |
| 22 | * Encryption key object. |
| 23 | * |
| 24 | * @var JCryptKey |
| 25 | */ |
| 26 | private $key = null; |
| 27 | |
| 28 | /** |
| 29 | * The instance of the class, which can be instantiated only once. |
| 30 | * |
| 31 | * @var SecureCipher |
| 32 | */ |
| 33 | private static $instance = null; |
| 34 | |
| 35 | /** |
| 36 | * Class constructor. |
| 37 | * The very first time this class is used, it attempts to create a new encryption key, |
| 38 | * which will be retrieved for all the future useges. |
| 39 | * |
| 40 | * @uses JCryptCipherCrypto::generateKey() |
| 41 | */ |
| 42 | private function __construct() |
| 43 | { |
| 44 | // constructor not accessible |
| 45 | |
| 46 | // get config with debug enabled |
| 47 | $config = VAPFactory::getConfig(); |
| 48 | |
| 49 | // recover key from configuration (obtain JSON string) |
| 50 | $key = $config->get('securehashkey', null); |
| 51 | |
| 52 | if (!$key) |
| 53 | { |
| 54 | // if key is empty, generate a new one |
| 55 | $key = $this->generateKey(); |
| 56 | |
| 57 | // build standard class to represent secure key |
| 58 | $obj = new stdClass; |
| 59 | |
| 60 | if (method_exists($key, 'getType')) |
| 61 | { |
| 62 | // running Joomla 4, access properties through getter methods |
| 63 | $obj->type = $key->getType(); |
| 64 | $obj->public = $key->getPublic(); |
| 65 | $obj->private = $key->getPrivate(); |
| 66 | } |
| 67 | else |
| 68 | { |
| 69 | // running Joomla 3, use getter magic method |
| 70 | $obj->type = $key->type; |
| 71 | $obj->public = $key->public; |
| 72 | $obj->private = $key->private; |
| 73 | } |
| 74 | |
| 75 | // encode public and private keys in base 64 to avoid errors |
| 76 | $obj->public = base64_encode($obj->public); |
| 77 | $obj->private = base64_encode($obj->private); |
| 78 | |
| 79 | // store key in configuration |
| 80 | $config->set('securehashkey', $obj); |
| 81 | } |
| 82 | else |
| 83 | { |
| 84 | // key is not empty, decode stored key |
| 85 | $key = json_decode($key); |
| 86 | |
| 87 | // create a new JCryptKey instance with the stored keys |
| 88 | $key = new JCryptKey($key->type, base64_decode($key->private), base64_decode($key->public)); |
| 89 | } |
| 90 | |
| 91 | $this->key = $key; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Class cloner. |
| 96 | */ |
| 97 | private function __clone() |
| 98 | { |
| 99 | // cloning function not accessible |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Instantiates a new SecureCipher object. |
| 104 | * |
| 105 | * @return self The cipher object. |
| 106 | */ |
| 107 | public static function getInstance() |
| 108 | { |
| 109 | if (self::$instance === null) |
| 110 | { |
| 111 | self::$instance = new SecureCipher(); |
| 112 | } |
| 113 | |
| 114 | return self::$instance; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * @override |
| 119 | * Method used to encrypt a data string. |
| 120 | * |
| 121 | * @param string $data The data string to encrypt. |
| 122 | * @param JCryptKey $key The key object to use for encryption. |
| 123 | * In case the key is empty, get the existing one. |
| 124 | * |
| 125 | * @return string The encrypted data string. |
| 126 | * |
| 127 | * @throws RuntimeException |
| 128 | * |
| 129 | * @uses JCryptCipherCrypto::encrypt() |
| 130 | */ |
| 131 | public function encrypt($data, ?JCryptKey $key = null) |
| 132 | { |
| 133 | // the encryption key becomes optional |
| 134 | if ($key === null) |
| 135 | { |
| 136 | // when the encription key is not provided, get the existing one |
| 137 | $key = $this->key; |
| 138 | } |
| 139 | |
| 140 | // call the parent method to encrypt |
| 141 | return parent::encrypt($data, $key); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * @override |
| 146 | * Method used to decrypt a data string. |
| 147 | * |
| 148 | * @param string $data The encrypted string to decrypt. |
| 149 | * @param JCryptKey $key The key object to use for decryption. |
| 150 | * In case the key is empty, get the existing one. |
| 151 | * |
| 152 | * @return string The decrypted data string. |
| 153 | * |
| 154 | * @throws RuntimeException |
| 155 | * |
| 156 | * @uses JCryptCipherCrypto::decrypt() |
| 157 | */ |
| 158 | public function decrypt($data, ?JCryptKey $key = null) |
| 159 | { |
| 160 | // the encryption key becomes optional |
| 161 | if ($key === null) |
| 162 | { |
| 163 | // when the encription key is not provided, get the existing one |
| 164 | $key = $this->key; |
| 165 | } |
| 166 | |
| 167 | // call the parent method to decrypt |
| 168 | return parent::decrypt($data, $key); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Method used to encrypt safely a data string using a Base 64 encoding. |
| 173 | * |
| 174 | * @param string $data The data string to encrypt. |
| 175 | * @param JCryptKey $key The key object to use for encryption. |
| 176 | * In case the key is empty, get the existing one. |
| 177 | * |
| 178 | * @return string The encrypted data string in Base 64. |
| 179 | * |
| 180 | * @throws RuntimeException |
| 181 | * |
| 182 | * @uses SecureCipher::encrypt() |
| 183 | */ |
| 184 | public function safeEncodingEncryption($data, ?JCryptKey $key = null) |
| 185 | { |
| 186 | return base64_encode($this->encrypt($data, $key)); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Method used to decrypt a data string encoded in Base 64. |
| 191 | * |
| 192 | * @param string $data The encrypted Base 64 string to decrypt. |
| 193 | * @param JCryptKey $key The key object to use for decryption. |
| 194 | * In case the key is empty, get the existing one. |
| 195 | * |
| 196 | * @return string The decrypted data string. |
| 197 | * |
| 198 | * @throws RuntimeException |
| 199 | * |
| 200 | * @uses SecureCipher::decrypt() |
| 201 | */ |
| 202 | public function safeEncodingDecryption($data, ?JCryptKey $key = null) |
| 203 | { |
| 204 | return $this->decrypt(base64_decode($data), $key); |
| 205 | } |
| 206 | } |
| 207 |