<?php

namespace app\pay\lib;

/**
 * 签名验证类
 * Class Sign
 * @package app\pay\common
 */
class Sign {

    /**
     * 签名生成
     */
    public static function create($params = [],$keycode) {

        ksort($params);

        $keyStr = '';
        foreach ($params as $key => $val) {
            if($key == "sign" || $key == "sign_type" || $val == "")continue;
            $keyStr .= "$key=$val&";
        }
        $keyStr = trim($keyStr,'&');

        $sign = md5($keyStr . $keycode);

        return $sign;

    }

    public static function validate($params, $preSign,$key) {
        if( Sign::create($params,$key) === $preSign ){
            return true ;
        }
        return false ;
    }
}
