嘗試使用下面的代碼使用HMac SHA256
來獲取摘要,但每次都會給出不同的輸出。對於目標c中的相同輸入,每次都會生成不同的HMac摘要。
這裏的關鍵參數是Base64string
格式,而純文本參數沒有任何編碼。
+(NSString *)hmacWithIndicies:(NSString *)plaintext withKey:(NSString *)key {
NSLog(@"Input text::%@",plaintext);
NSLog(@"Input Key::%@",key);
NSData *keyData = [[NSData alloc] initWithBase64EncodedString:key options:0];
NSLog(@"Key Data is::%@",keyData);
const char *cKey = (char *)[keyData bytes];
NSLog(@"Key Length is::%lu",strlen(cKey));
NSData *keyInData = [NSData dataWithBytes:cKey length:sizeof(cKey)];
NSLog(@"Key data = %@", keyInData);
//Data here
const char *cData = [plaintext cStringUsingEncoding:NSUTF8StringEncoding];
NSLog(@"Input Length is::%lu",strlen(cData));
NSData *dataData = [NSData dataWithBytes:cData length:sizeof(cData)];
NSLog(@"Input data = %@", dataData);
uint8_t cHMAC[CC_SHA256_DIGEST_LENGTH];
CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC);
NSData *hMacInData =[[NSData alloc] initWithBytes:cHMAC length:sizeof(cHMAC)];
NSLog(@"Hash Mac data generated is %@", hMacInData);
NSString *b64EncStrHmac = [hMacInData base64EncodedStringWithOptions:0];
NSLog(@"Hash Mac generated is %@", b64EncStrHmac);
return b64EncStrHmac;
}
調用上述方法如下: -
NSString * hMacOutput= [KeyGeneration hmacWithIndicies:@"2SagarPra2983688" withKey:@"qDwki5t1SSuKER4mzSMBHXhtt+PRMCv0B2LgXaBZmgE="];
NSLog(@"Output of HMac digest::%@",hMacOutput);
hMacOutput
摘要是每次被調用時產生不同的輸出。
謝謝zaph。用你的解決方案它就像一個魅力:)。現在我可以每次看到相同的輸出,並感謝指出我正在提交的錯誤。 – Sagar