Пример #1
0
echo sprintf("date : %s \n", date('Y-m-d H:i:s', $runtime));
$input = ['mobile' => 18612341234, 'partner' => $partner, 'user_id' => 1017, 'timestamp' => $runtime, 'channel' => $channel];
// 签名示范
$sign = HMAC::calculate($input, $key);
echo sprintf("sign : %s\n", $sign);
$queryData = array_merge($input, ['sign' => $sign]);
$queryString = http_build_query($queryData);
// 固定入口 URL 示范,其他接口类似
$fullUrl = $baseUrl . '/autoLogin?' . $queryString;
echo sprintf("fullUrl : %s\n", $fullUrl);
// 验签示范
if (HMAC::checkExpired($queryData['timestamp'])) {
    echo sprintf("链接已过期\n", $fullUrl);
    exit;
}
$result = HMAC::compare($queryData, $key, $sign);
echo sprintf("compare result : %s\n", intval($result));
echo "DONE\n";
class HMAC
{
    const TIME_SCOPE = 300;
    //second 5*60=300
    /**
     * 计算签名
     * @param $input
     * @param $key
     * @return string
     */
    public static function calculate($input, $key)
    {
        $signPars = "";