driver = $config->driver; $this->key = $config->key; // determine what is installed $this->handlers = [ 'OpenSSL' => extension_loaded('openssl'), ]; // if any aren't there, bomb if (in_array(false, $this->handlers)) { // this should never happen in travis-ci // @codeCoverageIgnoreStart throw EncryptionException::forNoHandlerAvailable($this->driver); // @codeCoverageIgnoreEnd } } /** * Initialize or re-initialize an encrypter * * @param BaseConfig $config Configuration parameters * @return \CodeIgniter\Encryption\EncrypterInterface * * @throws \CodeIgniter\Encryption\Exceptions\EncryptionException */ public function initialize(BaseConfig $config = null) { // override config? if (! empty($config)) { $this->driver = $config->driver; $this->key = $config->key; } // Insist on a driver if (empty($this->driver)) { throw EncryptionException::forNoDriverRequested(); } // Check for an unknown driver if (! in_array($this->driver, $this->drivers)) { 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); } }