org\transform\driver\Base64::decode PHP 메소드

decode() 공개 메소드

Base64解码函数
public decode ( string $data, string $target = '' )
$data string 欲解码的数据
$target string 解码目标
    public function decode($data, $target = '')
    {
        // 当函数没有特别指定解码目标时, 使用类自身解码目标
        if (empty($target)) {
            $target = self::$target;
        }
        // 根据解码目标替换字符
        switch ($target) {
            case 'url':
                $data = str_replace(['-', '_'], ['+', '/'], $data);
                break;
            case 'regex':
                $data = str_replace(['!', '-'], ['+', '/'], $data);
                break;
            case 'default':
            default:
                break;
        }
        // 原始解码,并返回结果
        return base64_decode($data);
    }

Usage Example

예제 #1
0
 /**
  * 解密字符串
  *
  * @param string $value  待加密的数据(数字, 字符串, 数组或对象等)
  * @param string $key    解密密钥
  * @param string $target 解码目标
  *
  * @return string
  */
 public static function decrypt($value, $key, $target = 'url')
 {
     // Base64解码
     $base = new Base64();
     $value = $base->decode($value, $target);
     // 拆分加密结果(校验码, 有效期, 初始化向量, 加密数据)
     $hmac = substr($value, 0, self::HMAC_SIZE);
     $expire = substr($value, self::HMAC_SIZE, self::EXPIRE_SIZE);
     $iv = substr($value, self::HMAC_SIZE + self::EXPIRE_SIZE, self::IV_SIZE);
     $value = substr($value, self::HMAC_SIZE + self::EXPIRE_SIZE + self::IV_SIZE);
     // 超出有效期
     if (time() > hexdec(bin2hex($expire))) {
         return false;
     }
     // 验证密文是否被篡改
     if (static::compareString(static::hmac($iv, $value, $key), bin2hex($hmac)) === false) {
         return false;
     }
     // 解密数据
     $value = openssl_decrypt($value, self::CIPHER_MODE, $key, OPENSSL_RAW_DATA, $iv);
     if (false === $value) {
         return false;
     }
     // 反序列化
     $value = static::unpacking($value);
     // 返回解密结果
     return $value;
 }