gpt4 book ai didi

iPhone 和 HMAC-SHA-1 编码

转载 作者:行者123 更新时间:2023-12-03 18:40:32 27 4
gpt4 key购买 nike

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

使用这个例子,什么是

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com