public static function _init() { static::$crypter = new Crypt_AES(); static::$hasher = new Crypt_Hash('sha256'); // load the config \Config::load('crypt', true); static::$config = \Config::get('crypt', array()); // generate random crypto keys if we don't have them or they are incorrect length $update = false; foreach (array('crypto_key', 'crypto_iv', 'crypto_hmac') as $key) { if (empty(static::$config[$key]) || strlen(static::$config[$key]) % 4 != 0) { $crypto = ''; for ($i = 0; $i < 8; $i++) { $crypto .= static::safe_b64encode(pack('n', mt_rand(0, 0xffff))); } static::$config[$key] = $crypto; $update = true; } } // update the config if needed if ($update === true) { try { \Config::save('crypt', static::$config); } catch (\FileAccessException $e) { // failed to write the config file, inform the user echo \View::factory('errors/crypt_keys', array('keys' => static::$config)); die; } } static::$crypter->enableContinuousBuffer(); static::$hasher->setKey(static::safe_b64decode(static::$config['crypto_hmac'])); }