Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器 软件资源

软件开发小程序制作系统集成与运维空间租用硬件开发视频监控技术咨询与支持——联系电话:0311-88999002/88999003

首页 / 操作系统 / Linux / Python对PHP服务器hmac_sha1签名认证方法的匹配实现

如果你写的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,即 参见说明文档:http://docs.python.org/library/base64.html2. 不能使用hmac的digest方法,而需要使用hexdigest才能和PHP的hash_hmac结果对上。