我目前正在开发一个应用程序,它需要发送身份验证数据与它提供的API。基本上,它需要基于您想要发送的数据、基于共享密钥来生成散列。
问题是,虽然我已经能够追踪到将执行MD5散列的函数,但它们不是基于键的,这是绝对关键的。
在iOS平台的objective-c中有什么方法可以做到这一点吗?
该API通常与PHP一起使用,PHP提供类似以下方便的函数:
$key = hash_hmac('md5', $postdata , $sharedkey);在objective-c中是否有机会实现相等?
发布于 2012-02-11 10:10:51
MD5算法只使用一个字符串作为输入。约定是将键(也称为"salt“值)附加到要散列的字符串。我猜测PHP键的MD5函数有第二个参数来简化操作,但是如果您这样做,应该会得到相同的结果:
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发布于 2012-02-11 09:04:47
通常,您只需将键附加到要散列的字节上。
因此,如果共享密钥是"12345“,并且您传递的是username=jsd和password=test,则可以像"username=jsd&password=test&secret=12345”一样构造字符串。然后,接收端将根据用户名和密码+密钥构建自己的版本,运行相同的md5,并接收相同的值。
https://stackoverflow.com/questions/9236664
复制相似问题