如何让 PHP 像 Objective-C 一样创建 HMAC-SHA1 字符串?

发布于 2024-10-08 23:51:15 字数 651 浏览 0 评论 0原文

我正在尝试使用 PHP 和 Objective-C 实现身份验证解决方案。两种语言都使用相同的密钥和相同的秘密创建自己的 HMAC-SHA1 编码字符串。

显然,他们的做法似乎有所不同。

在 Objective-C 方面,我使用 OAuthCustomer 作为签名类,它会生成看起来正确的编码字符串:

/3n/d4sKN6k3I7nBm1qau59UukU=

在 PHP 方面,我使用带有 base64 编码的内置函数 hash_hmac('sha1',...) ,它会生成以下内容:

ZmY3OWZmNzc4YjBhMzdhOTM3MjNiOWMxOWI1YTlhYmI5ZjU0YmE0NQ==

然后我尝试使用另一个函数(此处提及),这会产生使用base64编码:

NWY1ODUwOWE3NGI4NWU5ZTIxMDYzMTNmNzk3NTYxMDQ4OWE1MmUzNQ==

我完全不知道如何解决这个问题,我什至不知道为什么会发生这种情况。

帮助

非常感谢保罗的

I am trying to implement an authentication solution with PHP and Objective-C. Both languages create their own HMAC-SHA1 encoded strings with the same key and the same secret.

Apparently they seem to differ in their way how they do it.

On Objective-C side I am using OAuthCustomer as signing class which produces the correct looking encoded string:

/3n/d4sKN6k3I7nBm1qau59UukU=

On PHP side I am using the built-in function hash_hmac('sha1',...) with base64 encoding which produces this:

ZmY3OWZmNzc4YjBhMzdhOTM3MjNiOWMxOWI1YTlhYmI5ZjU0YmE0NQ==

Then I have tried to use another function (mentioned here) and this produces with base64 encoding this:

NWY1ODUwOWE3NGI4NWU5ZTIxMDYzMTNmNzk3NTYxMDQ4OWE1MmUzNQ==

I have absolutely no idea how I can fix this issue and I don't even know why this happens.

Thanks a bunch for help,

Paul

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷迭香的记忆 2024-10-15 23:51:15

好吧,我会添加一个假答案。 (在 Stackoverflow 上,每个问题都应该用答案来装饰。)

PHP 中的哈希函数大多返回十六进制字符串,而不是真实数据。 (无论出于何种原因)。通常有一个函数参数使其与其他实现所期望的兼容:

 hash_hmac("sha1", $data, $key, $raw_output=TRUE);

 md5($str, $raw_output=TRUE);

 hash("sha1", $data, $raw_output=TRUE);

Okay, I'll add a faux answer. (On Stackoverflow every question should be decorated by an answer.)

The hash functions in PHP mostly return hex-strings, not the real data. (For whatever reason). There is usually a function parameter to make it compatible to what other implementations expect:

 hash_hmac("sha1", $data, $key, $raw_output=TRUE);

 md5($str, $raw_output=TRUE);

 hash("sha1", $data, $raw_output=TRUE);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文