我目前正在开发一个需要将身份验证数据发送到提供的 API 的应用程序。基本上,它需要根据您要发送的数据,基于共享 key 生成散列。
问题是,虽然我已经能够追踪将执行 MD5 散列的函数,但它们不是基于 key ,这绝对是至关重要的。
有什么方法可以在 iOS 平台的 Objective-C 中完成吗?
该 API 通常与 PHP 一起使用,它提供了类似以下方便的功能:
$key = hash_hmac('md5', $postdata , $sharedkey);
有没有机会在objective-c中实现equal?
最佳答案
MD5 算法只使用一个字符串作为输入。惯例是您将您的 key (也称为“盐”值)附加到您正在散列的字符串中。我的猜测是 PHP MD5 函数有第二个参数作为 key 让生活更轻松,但如果你这样做,你应该得到相同的结果:
NSString *value = [data stringByAppendingString:key];
NSString *hashed = MD5HASH(value); //pseudocode
更新:
好的,我查了Wikipedia看起来你需要做一些额外的工作来实现 HMAC 风格的散列。所以你有两个选择。
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
关于objective-c - Objective-c (iOS) 中的 MD5 散列,基于共享 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9236664/