iPhone和HMAC-SHA-1编码

6 iphone sha1 hmac

即时通讯试图接到亚马逊网络服务的电话,我仍然坚持获取签名,看着这个,但我仍然有一个问题.

用这个例子是什么

NSData *keyData;
NSData *clearTextData
Run Code Online (Sandbox Code Playgroud)

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

/*
  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]
Run Code Online (Sandbox Code Playgroud)

小智 33

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

    #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
Run Code Online (Sandbox Code Playgroud)

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

  • 肯定有效:-) - 但是有没有更优雅的方式将NSData转换为NSString? (3认同)

Ken*_*ner 5

如果您正在调用亚马逊网络服务,请查看价格或产品详细信息,您的亚马逊网络服务密钥将被禁用,您的应用将停止工作.

查看Amazon Web Services的服务条款,严格禁止移动客户端使用:

https://affiliate-program.amazon.com/gp/advertising/api/detail/agreement.html

当我的应用程序在生产应用程序中禁用了我的AWS密钥时,我发现了这一点.我已经阅读了TOS,但它并不是真的存在,因为您可以通过上面的链接看到其他一些模糊的使用细节.您不会认为联盟计划与API有任何关系,但确实如此.

您可以在TechCrunch文章中找到阻止的其他应用的详细信息:

http://www.techcrunch.com/2009/07/07/amazon-killing-mobile-apps-that-use-its-data/

只是给你一个抬头,希望能为你节省大量的工作.


cat*_*lan 0

查看CocoaCryptoHashing的 SHA1 编码