Home > php教程 > php手册 > Python对PHP服务器hmac

Python对PHP服务器hmac

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 19:51:03
Original
1335 people have browsed it

如果你写的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

参见说明文档:http://docs.python.org/library/base64.html


2. 不能使用hmac的digest方法,而需要使用hexdigest才能和PHP的hash_hmac结果对上。


iefreer

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template