iPhone和HMAC-SHA-1编码 [英] iPhone and HMAC-SHA-1 encoding

查看:82
本文介绍了iPhone和HMAC-SHA-1编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图接到一个亚马逊网络服务的电话而且我坚持获得签名,看着这个,但我仍然有一个问题。

im trying to get a call to amazon web service and im stuck on getting the signature, looked at this but i still have a question on it.

使用这个例子什么是

NSData *keyData;
NSData *clearTextData

?我需要为这两个值传递什么?

? what do i need to pass for these two values?

/*
  inputs:
  NSData *keyData;
  NSData *clearTextData
*/

uint8_t digest[CC_SHA1_DIGEST_LENGTH] = {0};

CCHmacContext hmacContext;
CCHmacInit(&hmacContext, kCCHmacAlgSHA1, keyData.bytes, keyData.length);
CCHmacUpdate(&hmacContext, clearTextData.bytes, clearTextData.length);
CCHmacFinal(&hmacContext, digest);

NSData *out = [NSData dataWithBytes:digest length:CC_SHA1_DIGEST_LENGTH]


推荐答案

我只花了4个小时谷歌搜索并寻找方法来计算iPhone上的unkeyed SHA1,它与php中的sha1()函数的结果相匹配。结果如下:

I just spent like 4 hours Googling and looking for ways to calculate an unkeyed SHA1 on the iPhone that would match the results of the sha1() function in php. Here was the result:

    #import <CommonCrypto/CommonDigest.h>

    NSString *hashkey = <your data here>;
// PHP uses ASCII encoding, not UTF
const char *s = [hashkey cStringUsingEncoding:NSASCIIStringEncoding];
NSData *keyData = [NSData dataWithBytes:s length:strlen(s)];

// This is the destination
uint8_t digest[CC_SHA1_DIGEST_LENGTH] = {0};
// This one function does an unkeyed SHA1 hash of your hash data
CC_SHA1(keyData.bytes, keyData.length, digest);

// Now convert to NSData structure to make it usable again
NSData *out = [NSData dataWithBytes:digest length:CC_SHA1_DIGEST_LENGTH];
// description converts to hex but puts <> around it and spaces every 4 bytes
NSString *hash = [out description];
hash = [hash stringByReplacingOccurrencesOfString:@" " withString:@""];
hash = [hash stringByReplacingOccurrencesOfString:@"<" withString:@""];
hash = [hash stringByReplacingOccurrencesOfString:@">" withString:@""];
// hash is now a string with just the 40char hash value in it

希望这个将帮助其他在iPhone上与SHA1抗争的人

Hopefully this will help others who are struggling with SHA1 on the iPhone

这篇关于iPhone和HMAC-SHA-1编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆