key = $config->key; $this->driver = $config->driver; $this->digest = $config->digest ?? 'SHA512'; // if any aren't there, bomb if ($this->driver === 'OpenSSL' && ! extension_loaded('openssl')) { // this should never happen in travis-ci // @codeCoverageIgnoreStart throw EncryptionException::forNoHandlerAvailable($this->driver); // @codeCoverageIgnoreEnd } } /** * Initialize or re-initialize an encrypter * * @param EncryptionConfig $config Configuration parameters * @return \CodeIgniter\Encryption\EncrypterInterface * * @throws \CodeIgniter\Encryption\Exceptions\EncryptionException */ public function initialize(EncryptionConfig $config = null) { // override config? if ($config) { $this->key = $config->key; $this->driver = $config->driver; $this->digest = $config->digest ?? 'SHA512'; } // Insist on a driver if (empty($this->driver)) { throw EncryptionException::forNoDriverRequested(); } // Check for an unknown driver if (! in_array($this->driver, $this->drivers, true)) { throw EncryptionException::forUnKnownHandler($this->driver); } if (empty($this->key)) { throw EncryptionException::forNeedsStarterKey(); } // Derive a secret key for the encrypter $this->hmacKey = bin2hex(\hash_hkdf($this->digest, $this->key)); $handlerName = 'CodeIgniter\\Encryption\\Handlers\\' . $this->driver . 'Handler'; $this->encrypter = new $handlerName($config); return $this->encrypter; } // -------------------------------------------------------------------- /** * Create a random key * * @param integer $length Output length * @return string */ public static function createKey($length = 32) { return random_bytes($length); } // -------------------------------------------------------------------- /** * __get() magic, providing readonly access to some of our protected properties * * @param string $key Property name * @return mixed */ public function __get($key) { if (in_array($key, ['key', 'digest', 'driver', 'drivers'], true)) { return $this->{$key}; } return null; } /** * __isset() magic, providing checking for some of our protected properties * * @param string $key Property name * @return boolean */ public function __isset($key): bool { return in_array($key, ['key', 'digest', 'driver', 'drivers'], true); } }