轉載鏈接:https://www.cnblogs.com/dcb3688/p/4608008.html

簽名算法如下:

  1. 對所有請求參數進行字典升序排列; 
    2. 將以上排序后的參數表進行字符串連接,如key1value1key2value2key3value3...keyNvalueN; 
    3. app secret作為后綴,對該字符串進行SHA-1計算,并轉換成16進制編碼; 
    4. 轉換為全大寫形式后即獲得簽名串


生成簽名代碼:
$serverstr = "";
foreach ($serverArray as $k => $v) {
 $serverstr.= $k.$v;
}
$reserverstr=$serverstr.$serverSecret;
$reserverSign = strtoupper(sha1($reserverstr));    // sha1  md5

if($clientSign!=$reserverSign){
    die('非法請求');
}else{
 //    your code continue;
}


2.hmac_md5 生成簽名 可逆向校驗

 function HmacMd5($data=array()) {
        $secret_key = 'lidianzjm666';
        /*兩種方式調用*/
        if (!$data) {
            $data = $this->request->post();
        }
        /*將數組轉為字符串*/
        $data = implode('&',$data);
        $key = iconv("GB2312","UTF-8",$secret_key);
        $data = iconv("GB2312","UTF-8",$data);
        $b = 64;
        if (strlen($key) > $b) {
            $key = pack("H*",md5($key));
        }
        $key = str_pad($key, $b, chr(0x00));
        $ipad = str_pad('', $b, chr(0x36));
        $opad = str_pad('', $b, chr(0x5c));
        $k_ipad = $key ^ $ipad ;
        $k_opad = $key ^ $opad;
        $sign['sign'] = md5($k_opad . pack("H*",md5($k_ipad . $data)));
        return json_encode(array('code' => 1000, 'msg' => 'success', 'data' =>$sign),JSON_UNESCAPED_UNICODE);
    }