/** * 生成和保存密钥对 * @method init * @param boolean $return_pri [返回公钥或者私钥] * @return [string] [公钥或者私钥] * @author NewFuture */ private static function init($return_pri = false) { $res = openssl_pkey_new(); openssl_pkey_export($res, $pri); $d = openssl_pkey_get_details($res); $pub = $d['key']; $time = time() + Config::get('rsa.lifetime') ?: 604800; kv::set('RSA_life_time', $time); kv::set('RSA_pri_key', $pri); Kv::set('RSA_pub_key', $pub); return $return_pri ? $pri : $pub; }
/** * 获取cookie配置 * @method config * @param [string] $name [配置变量名] * @return [mixed] [description] * @author NewFuture */ private static function config($name) { if (!($config = self::$_config)) { $config = Config::get('cookie'); if (!($key = Kv::get('COOKIE_aes_key'))) { /*重新生成加密密钥*/ $key = Random::word(32); Kv::set('COOKIE_aes_key', $key); } $config['key'] = $key; self::$_config = $config; } return isset($config[$name]) ? $config[$name] : null; }
/** * cipher_table($key) * 获取密码表 * 现在缓存中查询,如果存在,则直接读取,否则重新生成 * @param $key 加密的密钥 * @return array 密码映射表 */ private static function _cipherTable($key) { $tableName = 'et_' . urlencode($key); //缓存表名称 if ($table = Kv::get($tableName)) { /*读取缓存中的密码表*/ $table = unserialize($table); } else { /*密码表不存在则重新生成*/ //对所有数字,逐个进行AES加密生成密码表 $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, ''); mcrypt_generic_init($td, $key, '0000000000000000'); for ($i = 0; $i < 10000; ++$i) { $table[] = mcrypt_generic($td, $i); } mcrypt_generic_deinit($td); sort($table); //根据加密后内容排序得到密码表 Kv::set($tableName, serialize($table)); //缓存密码表 } return $table; }
/** * cipher_table($key) * 获取密码表 * 现在缓存中查询,如果存在,则直接读取,否则重新生成 * @param $key 加密的密钥 * @return array 密码映射表 */ private static function _cipherTable($key) { $tableName = 'et_' . $key; //缓存表名称 $table = Kv::get($tableName); //读取缓存中的密码表 if (!$table) { //密码表不存在则重新生成 //对所有数字,逐个进行AES加密生成密码表 $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, ''); mcrypt_generic_init($td, $key, '0000000000000000'); for ($i = 0; $i < 10000; ++$i) { $table[] = mcrypt_generic($td, $i); } mcrypt_generic_deinit($td); sort($table); //根据加密后内容排序得到密码表 Kv::set($tableName, $table); //缓存密码表 } return $table; }
/** * 获取加密密钥 * @method key * @return [type] [description] * @author NewFuture */ public static function key() { if (!($key = Kv::get('COOKIE_aes_key'))) { /*重新生成加密密钥*/ $key = Random::word(32); Kv::set('COOKIE_aes_key', $key); } return $key; }