encode() public method

Base64编码函数
public encode ( string $data, string $target = '' )
$data string 欲编码的数据
$target string 编码目标
Beispiel #1
0
 /**
  * 加密字符串
  *
  * @param mixed  $value  待加密的数据(数字, 字符串, 数组或对象等)
  * @param string $key    加密密钥
  * @param int    $expire 加密有效期(几秒后加密失效)
  * @param string $target 编码目标
  *
  * @return string
  */
 public static function encrypt($value, $key, $expire = 0, $target = 'url')
 {
     // 随机生成初始化向量, 增加密文随机性
     $iv = static::createIV(self::IV_SIZE);
     // 序列化待加密的数据(支持数组或对象的加密)
     $value = static::packing($value);
     // 加密数据
     $value = openssl_encrypt($value, self::CIPHER_MODE, $key, OPENSSL_RAW_DATA, $iv);
     if (false === $value) {
         return false;
     }
     // 加密有效期
     $expire = $expire ? dechex(time() + $expire) : 0;
     $expire = sprintf('%08s', $expire);
     // 生成密文校验码
     $hmac = static::hmac($iv, $value, $key);
     // 组合加密结果并base64编码
     $base = new Base64();
     return $base->encode(pack('H*', $hmac . $expire) . $iv . $value, $target);
 }