加签验签思路

一、加签

1、将报文内容+时间戳(timestamp)+随机字符串(nonce)+appid进行sha256加密,得到签名$sign

$sign = hash('sha256', body=string&timestamp=time()&nonce=asdasd&appid=sadasdw)

2、将签名$sign使用私钥进行加密,并将结果进行base64_encode转化下

openssl_private_encrypt($string, $encrypt, $privateKey);
$encrypt = base64_encode($encrypt);

3、将加密后的签名($encrypt)、时间戳(timestamp)、随机字符串(nonce)和appid放在Header中,进行传输

二、验签

1、从请求的Header中提取出加密后的签名($encrypt)、时间戳(timestamp)、随机字符串(nonce)和appid

2、同加签第一步,将报文内容+时间戳(timestamp)+随机字符串(nonce)+appid进行sha256加密,得到签名$sign

$sign = hash('sha256', body=string&timestamp=time()&nonce=asdasd&appid=sadasdw)

3、将加密后的签名($encrypt)base64_decode后使用公钥进行解密,得到签名($decrypt)

$encrypt = base64_decode($encrypt);
openssl_public_decrypt($encrypt, $decrypt, $publickKey);

4、将第二步中的签名($sign)和第三部中解密出来的签名($decrypt)进行比较,若相等则说明报文没有被篡改

 

 

 

 

 

posted @ 2022-07-15 17:18  风哀伤  阅读(477)  评论(0编辑  收藏  举报