org\Crypt::encrypt PHP Method

encrypt() public static method

加密字符串
public static encrypt ( mixed $value, string $key, integer $expire, string $target = 'url' ) : string
$value mixed 待加密的数据(数字, 字符串, 数组或对象等)
$key string 加密密钥
$expire integer 加密有效期(几秒后加密失效)
$target string 编码目标
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);
    }