iPhone 和 HMAC-SHA-1 编码

标签 iphone sha1 hmac

我试图调用亚马逊网络服务,但我一直在获取签名,看了这个,但我仍然有一个问题。

使用这个例子,什么是

NSData *keyData;
NSData *clearTextData

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

/*
  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 上计算未加密的 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

希望这能帮助其他在 iPhone 上遇到 SHA1 问题的人

关于iPhone 和 HMAC-SHA-1 编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/735714/

相关文章:

rest - 使用基于 HMAC 的签名如何以及在何处存储 REST API 的 key

ios - YTPlayer View在iPhone中无法正常工作

iphone - 自动选择第一个 UITableView 文本框

java - 检测密码加密类型——我可以将它们导入 Django 吗?

javascript - 如何在浏览器中使用 javascript 计算大文件的 sha1 哈希而不造成内存过载

c# - 如何在数据库中存储散列密码

python - python加密的基础知识w/hashlib sha1

iphone - 全局队列内的计时器未在 iOS 中调用

iphone - 最大线程限制?

java - 用于在 Android 上生成 HMAC-SHA1 OAuth 签名的库?