javascript - iOS 和 javascript 库之间的 HMAC SHA-512 生成差异
<p><p>我正在尝试使用引用此链接 <a href="https://stackoverflow.com/questions/756492/objective-c-sample-code-for-hmac-sha1" rel="noreferrer noopener nofollow">Objective-C sample code for HMAC-SHA1</a> 的 SHA-512 算法从我的 iOS 应用程序中复制 Hmac 生成行为.</p>
<p>为此,我尝试使用 <a href="https://code.google.com/p/crypto-js/#SHA-2" rel="noreferrer noopener nofollow">CrytoJS</a>和 <a href="http://caligatio.github.io/jsSHA/" rel="noreferrer noopener nofollow">jsSHA</a>使用 javascript 代码计算 Hmac 的库。我发现这些 javascript 库生成的 hmac 值与我使用 iOS 代码生成的值存在差异。 </p>
<p>有人可以帮助我了解我在这里做错了什么吗?我有一种感觉,我弄乱了传递给这两种方法的键和计数器值的格式。</p>
<p>例如:
key = "敏捷的棕狐跳过懒狗"
计数器 = 123</p>
<p>iOS代码生成的Hmac- 8d4b0f7c7f800ffd656829b98988048b49b08d0068f6fd33add8a02b6bce8097cdd3a69dc8292ec7cc04e15021afb4499afe4a292f8db082b2d253ddfe7d7015</p>
<p>由 javascript 库生成的 Hmac-
211935F67D87CBB6A98DE6A6D9D64F9AAF8DA5F09BF17F1B7E5BD46FCD9BEFBCD3585FB859BD042291AF5D79B6D92CF7B348CD6558A18AEF4328FAF344D63266</p>
<p>iOS 代码:</p>
<pre><code>NSData *key = ;
NSData *rawKeyData = ;
//encode the counter
uint8_t tosign;
for (int i = sizeof(tosign) - 1; i >= 0; i--) {
tosign = counter & 0xff;
counter >>= 8;
}
unsigned char cHMAC;
CCHmac(kCCHmacAlgSHA512, , , tosign, sizeof(tosign), cHMAC);
NSData *hmac = ;
return hmac;
</code></pre>
<p>javascript代码:</p>
<pre><code>var hash = CryptoJS.HmacSHA512("123", "The quick brown fox jumps over the lazy dog");
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>HMAC 接受一个数据键和一个键和数据参数,它们是字节,并返回由指定的哈希函数确定的长度的字节。</p>
<p>例子:</p>
<pre><code>+ (NSData *)doHmacSha512:(NSData *)dataIn
key:(NSData *)key
{
NSMutableData *macOut = ;
CCHmac( kCCHmacAlgSHA512,
key.bytes,
key.length,
dataIn.bytes,
dataIn.length,
macOut.mutableBytes);
return macOut;
}
</code></pre>
<p>测试:</p>
<pre><code>NSData *keyData= [@"MyTestKey" dataUsingEncoding:NSASCIIStringEncoding];
NSData *data = [@"Now is the time for all good computers to come to the aid of their masters." dataUsingEncoding:NSASCIIStringEncoding];
NSData *hamcData = ; // Where "Crypto" is the class "doHmacSha512" is defined in.
</code></pre></p>
<p style="font-size: 20px;">关于javascript - iOS 和 javascript 库之间的 HMAC SHA-512 生成差异,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/30387437/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/30387437/
</a>
</p>
页:
[1]