尝试使用 PHP 通过 HMAC-SHA1 进行数字签名
我正在尝试设置一些 Google Maps Premier API 操作,为此,我需要对我的网址进行签名以进行身份验证。如果您查看签名示例,可以看到一些 Python、C# 和 Java 代码向您展示如何通过 HMAC-SHA1 进行签名。还有一个示例,以便我可以测试我的 PHP 实现。但是,我似乎无法让它发挥作用。
这是我的代码:
$key = "vNIXE0xscrmjlyV-12Nj_BvUPaw=";
$data = "/maps/api/geocode/json?address=New+York&sensor=false&client=clientID";
$my_sign = hash_hmac("sha1", $data, base64_decode($key));
$my_sign = base64_encode($my_sign);
$valid_sign = "KrU1TzVQM7Ur0i8i7K3huiw3MsA=";
当我运行这个时,我得到一个签名:
ZDRlNGMwZjIyMTA1MWM1Zjk0Nzc4M2NkYjlmNDQzNDBkYzk4NDI4Zg==
完全不匹配。
我想到的事情:
- 关键是修改后的URL编码格式,因此将
-
和_
更改为+
和/ 也不起作用
- Python 示例代码确实起作用,所以这是一个有效的示例。
- 用 python 而不是 PHP 完全重写我们的代码库(我继承了它)。
I'm trying to setup some Google Maps Premier API action, and to do so, I need to sign my URLs to authenticate. If you go down to Signature examples, there is some Python, C# and Java code to show you how to do the signature via HMAC-SHA1. There is also an example so that I can to test my PHP implementation. However, I just can't seem to get it to work.
Here's my code:
$key = "vNIXE0xscrmjlyV-12Nj_BvUPaw=";
$data = "/maps/api/geocode/json?address=New+York&sensor=false&client=clientID";
$my_sign = hash_hmac("sha1", $data, base64_decode($key));
$my_sign = base64_encode($my_sign);
$valid_sign = "KrU1TzVQM7Ur0i8i7K3huiw3MsA=";
When, I run this, I get a signature of:
ZDRlNGMwZjIyMTA1MWM1Zjk0Nzc4M2NkYjlmNDQzNDBkYzk4NDI4Zg==
Which totally doesn't match.
Things I have thought about:
- The key is in Modified URL encoded format, so changing
-
and_
to+
and/
also doesn't work - The Python example code does indeed work, so this is a valid example.
- Completely rewriting our code-base in python instead of PHP (I inherited it).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我假设您尝试签署 OAuth 的网址?
试试这个库:http://code.google.com/p/oauth/
I assume your trying to sign the url for OAuth?
Try out this library: http://code.google.com/p/oauth/
PHP 示例位于 http://gmaps-samples.googlecode .com/svn/trunk/urlsigning/UrlSigner.php-source
A php example is available at http://gmaps-samples.googlecode.com/svn/trunk/urlsigning/UrlSigner.php-source
你至少有两个问题,
试试这个,
You have 2 problems at least,
Try this,