我正在尝试使用HMAC-SHA1算法为休息请求创建base64签名。我专门使用SinglePlatform API,步骤如下:
我目前的实施如下:
// Convert a modified Base64 key into Base64
NSString *modifiedKey = [SIGNING_KEY stringByReplacingOccurrencesOfString:@"-" withString:@"+"];
modifiedKey = [modifiedKey stringByReplacingOccurrencesOfString:@"_" withString:@"/"];
// Decode the Base64 key
NSData *key = [NSData dataFromBase64String:modifiedKey];
// Construct a url with params
NSString *data = [NSString stringWithFormat:@"/locations/%@?client=%@", _id, CLIENT_ID];
// Convert key and data to c chars
const char *keyBytes = [key bytes];
const char *baseStringBytes = [data cStringUsingEncoding:NSUTF8StringEncoding];
unsigned char digestBytes[CC_SHA1_DIGEST_LENGTH];
CCHmacContext ctx;
CCHmacInit(&ctx, kCCHmacAlgSHA1, keyBytes, strlen(keyBytes));
CCHmacUpdate(&ctx, baseStringBytes, strlen(baseStringBytes));
CCHmacFinal(&ctx, digestBytes);
NSData *digestData = [NSData dataWithBytes:digestBytes length:CC_SHA1_DIGEST_LENGTH];
// Reconvert the into Base64 modified
NSString *signingKey = [digestData base64EncodedString];
signingKey = [signingKey stringByReplacingOccurrencesOfString:@"+" withString:@"-"];
signingKey = [signingKey stringByReplacingOccurrencesOfString:@"/" withString:@"_"];
if ( [data hasSuffix:@"="] ) {
[signingKey substringToIndex:[hassigningKeyh length]-1];
}
NSLog(@"Signing Key: %@", signingKey);
我最初收到的签名密钥是修改后的Base64,这就是我要替换 - + _ /字符的原因。此实现已返回正确的签名密钥,但不一致。
在将Objective-C转换为C时,我做了一些明显的错误吗?有没有更好的方法来解决这个问题?
我的应用程序正在使用ARC。
答案 0 :(得分:1)
问题在于我是确定数据长度的C方法:
CCHmacContext ctx;
CCHmacInit(&ctx, kCCHmacAlgSHA1, keyBytes, strlen(keyBytes));
CCHmacUpdate(&ctx, baseStringBytes, strlen(baseStringBytes));
CCHmacFinal(&ctx, digestBytes);
当我使用NSString / NSData长度方法时,它一直工作:
CCHmacContext ctx;
CCHmacInit(&ctx, kCCHmacAlgSHA1, keyBytes, [key length]);
CCHmacUpdate(&ctx, baseStringBytes, [data length]);
CCHmacFinal(&ctx, digestBytes);
修改强>
正如评论中Ivo Beckers
所提到的,这也可以写成一行:
CCHmac(kCCHmacAlgSHA1, keyBytes, strlen(keyBytes), baseStringBytes, strlen(baseStringBytes), digestBytes);