Objective-c (iOS) 中的 MD5 哈希,基于共享密钥

发布于 2025-01-04 21:08:51 字数 365 浏览 1 评论 0原文

我目前正在开发一个应用程序,需要将身份验证数据发送到提供的 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 技术交流群。

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

发布评论

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

评论(2

兮子 2025-01-11 21:08:51

MD5算法仅使用一个字符串作为输入。约定是您将密钥(也称为“盐”值)附加到您正在散列的字符串中。我的猜测是 PHP MD5 函数有第二个参数作为密钥,以使生活更轻松,但是如果您这样做,您应该得到相同的结果:

NSString *value = [data stringByAppendingString:key];
NSString *hashed = MD5HASH(value); //pseudocode

更新:

好的,我检查了Wikipedia 看起来您需要做一些额外的工作来实现 HMAC 风格的哈希。所以你有两个选择。

  1. 在您已经使用的 MD5 哈希之上实现 HMAC 算法(看起来不太难 - 我在下面粘贴了伪代码)。

  2. 担心 HMAC - 只需通过连接消息和密钥,使用常规 MD5 在两端生成哈希 - 这应该非常安全,这就是大多数人所做的。

HMAC算法

function hmac (key, message)
    if (length(key) > blocksize) then
        key = hash(key) // keys longer than blocksize are shortened
    end if
    if (length(key) < blocksize) then
        key = key ∥ [0x00 * (blocksize - length(key))] // keys shorter than blocksize are zero-padded ('∥' is concatenation) 
    end if

    o_key_pad = [0x5c * blocksize] ⊕ key // Where blocksize is that of the underlying hash function
    i_key_pad = [0x36 * blocksize] ⊕ key // Where ⊕ is exclusive or (XOR)

    return hash(o_key_pad ∥ hash(i_key_pad ∥ message)) // Where '∥' is concatenation
end function

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:

NSString *value = [data stringByAppendingString:key];
NSString *hashed = MD5HASH(value); //pseudocode

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.

  1. 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).

  2. 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

function hmac (key, message)
    if (length(key) > blocksize) then
        key = hash(key) // keys longer than blocksize are shortened
    end if
    if (length(key) < blocksize) then
        key = key ∥ [0x00 * (blocksize - length(key))] // keys shorter than blocksize are zero-padded ('∥' is concatenation) 
    end if

    o_key_pad = [0x5c * blocksize] ⊕ key // Where blocksize is that of the underlying hash function
    i_key_pad = [0x36 * blocksize] ⊕ key // Where ⊕ is exclusive or (XOR)

    return hash(o_key_pad ∥ hash(i_key_pad ∥ message)) // Where '∥' is concatenation
end function
混浊又暗下来 2025-01-11 21:08:51

通常,您只需将密钥附加到要散列的字节中。

因此,如果共享密钥是“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.

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