签名生成校验


use think\facade\Config;

class Sign
{
    /**
     * 生成签名的参数.
     *
     * @var
     */
    protected $data;

    /**
     * 加密key.
     *
     * @var mixed
     */
    protected $key;

    public function __construct()
    {
        $this->key = Config::get('tcc.sign_key');
    }

    /**
     * 生成签名.
     *
     * @return null|array|false|string|string[]
     */
    public function create()
    {
        // 签名步骤一:按字典序排序参数
        ksort($this->data);
        $string = $this->toUrlParams();

        // 签名步骤二:在string后加入KEY
        $string = $string.'&key='.$this->key;

        // 签名步骤三:MD5加密
        $string = md5($string);

        // 签名步骤四:所有字符转为大写
        return mb_strtoupper($string);
    }

    /**
     * @param mixed $data
     */
    public function setData($data)
    {
        $this->data = $data;

        return $this;
    }

    /**
     * 设置key.
     *
     * @param mixed $key
     */
    public function setKey($key)
    {
        $this->key = $key;

        return $this;
    }

    /**
     * 校验签名.
     *
     * @return bool
     */
    public function verify()
    {
        if (!isset($this->data['sign']) || empty($this->data['sign'])) {
            return false;
        }

        return $this->create() == $this->data['sign'];
    }

    /**
     * 参数转为字符串.
     *
     * @return string
     */
    private function toUrlParams()
    {
        $data = $this->data;
        unset($data['sign']);

        return http_build_query($data, '', '&');
    }
}
发布时间 : 2023-02-28,阅读量:1057
本文链接:https://upwqy.com/details/32.html
函数注入 mysql 经纬度计算