加入收藏 | 设为首页 | 会员中心 | 我要投稿 PHP编程网 - 黄冈站长网 (http://www.0713zz.com/)- 数据应用、建站、人体识别、智能机器人、语音技术!
当前位置: 首页 > 教程 > 正文

Python对PHP服务器hmac_sha1签名认证方法的匹配达成

发布时间:2021-12-14 21:46:22 所属栏目:教程 来源:互联网
导读:如果你写的PHP服务端API,在签名中使用了PHP的hash_hmac函数,并且使用了base64编码, 如下: //HMAC-SHA1加密 $hmac_sha1_str = base64_encode(hash_hmac(sha1, $string_to_sign, $secret_access_key)); //编码URL $signature = urlencode($hmac_sha1_str);
如果你写的PHP服务端API,在签名中使用了PHP的hash_hmac函数,并且使用了base64编码,
 
如下:
 
 
        //HMAC-SHA1加密
        $hmac_sha1_str = base64_encode(hash_hmac("sha1", $string_to_sign, $secret_access_key));
        //编码URL
 
        $signature = urlencode($hmac_sha1_str);
 
现在一个Python客户端程序想要来调用你的API,首先要通过安全认证,其签名方法可实现如下:
 
signature = urllib.quote(
    base64.b64encode(
        hmac.new(secret_access_key, string_to_sign, digestmod=hashlib.sha1)
        .hexdigest()
    ))
 
有两点需要注意:
 
1. 不能使用encodestring, 因为该函数会在字符串末尾添加上行结束符号%0A,即n

(编辑:PHP编程网 - 黄冈站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读