Objective-c (iOS) 中的 MD5 哈希,基于共享密钥
我目前正在开发一个应用程序,需要将身份验证数据发送到提供的 API。基本上,它需要根据您要发送的数据、基于共享密钥生成哈希。
问题是,虽然我已经能够追踪到进行 MD5 散列的函数,但它们并不是基于密钥,而这一点绝对至关重要。
有什么方法可以在 iOS 平台的 Objective-C 中完成此操作吗?
该 API 通常与 PHP 一起使用,PHP 提供了类似以下方便的功能:
$key = hash_hmac('md5', $postdata , $sharedkey);
Is there any opportunity of Implement an equal in Objective-c?
I'm currently developing an app which needs to send authentication data with it to a provided API. Basically it needs to generate a hash based on the data you want to send, based on a shared key.
The problem is that while I have been able to track down functions that will do MD5 hashing, they are not based on a key, which is absolutely crucial.
Is there any way this can be done in objective-c for the iOS platform?
The API is usually used with PHP, which provides something like this handy function:
$key = hash_hmac('md5', $postdata , $sharedkey);
Is there any chance of implementing an equal in objective-c?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
MD5算法仅使用一个字符串作为输入。约定是您将密钥(也称为“盐”值)附加到您正在散列的字符串中。我的猜测是 PHP MD5 函数有第二个参数作为密钥,以使生活更轻松,但是如果您这样做,您应该得到相同的结果:
更新:
好的,我检查了Wikipedia 看起来您需要做一些额外的工作来实现 HMAC 风格的哈希。所以你有两个选择。
在您已经使用的 MD5 哈希之上实现 HMAC 算法(看起来不太难 - 我在下面粘贴了伪代码)。
HMAC算法
The MD5 algorithm only uses one string as input. The convention is that you append your key (aka "salt" value) to the string you are hashing. My guess is that the PHP MD5 function has a second parameter for the key to make life easier, but you should get the same result if you just do this:
UPDATE:
Okay, I checked Wikipedia and it looks like you need to do a bit of extra work to implement HMAC-style hashing. So you have two options.
Implement the HMAC algorithm on top of the MD5 hash you're already using (it doesn't look too hard - I've pasted the pseudocode below).
Don't bother with HMAC - just generate the hash at both ends using a regular MD5 by concatenating the message and the key - that should be pretty secure, it's what most people do.
HMAC algorithm
通常,您只需将密钥附加到要散列的字节中。
因此,如果共享密钥是“12345”并且您传递 username=jsd 和 password=test,您将构建类似“username=jsd&password=test&secret=12345”的字符串。然后接收端将根据用户名和信息构建自己的版本。密码+秘密,运行相同的md5,并收到相同的值。
Typically you just append the key to the bytes that you are hashing.
So if the shared secret is "12345" and you are passing username=jsd and password=test you would construct your string like "username=jsd&password=test&secret=12345". Then the receiving end would construct its own version from the username & password + the secret, run the same md5, and receive the same value.