我需要使用 SHA512 加密的 cHMAC 我使用加密值这种方法和服务器端值不匹配他们使用 Java 的服务器端可以请帮助我解决这个问题。
服务器端 encrted 值为
85d86c928825ef85d5329893f2cf2cba9ba6354582d54b5f1c7aaf69b6d72f71b742ae67f3e400d2e4b367f62a45b9948b512ae9a8efc0bcd667f1cdb0a66c6d
Objective-c 加密值为
f242340a3664ea149717b943087cb8a5d92d6d25af5f5d8e0f51a6c4f0c1060830128e0798e6b300a81a1401612f0000d75d0000d3e27401e8d9ffbf221e1401
目标-C代码:
-(NSString *)createSHA512:(NSString *)string withKey:(NSString *)key
{
NSLog(@"key isss %@",key);
NSLog(@"string isss %@",string);
const char *cKey = [key cStringUsingEncoding:NSASCIIStringEncoding];
const char *cData = [string cStringUsingEncoding:NSASCIIStringEncoding];
unsigned char cHMAC[CC_SHA512_DIGEST_LENGTH];
CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC);
NSData *HMACData = [[NSData alloc] initWithBytes:cHMAC length:sizeof(cHMAC)];
const unsigned char *buffer = (const unsigned char *)[HMACData bytes];
NSLog(@"bytes data is %@",HMACData);
NSString *HMAC = [NSMutableString stringWithCapacity:HMACData.length * 2];
for (int i = 0; i < HMACData.length; ++i)
HMAC = [HMAC stringByAppendingFormat:@"%02lx", (unsigned long)buffer[i]];
return HMAC;
}
Java代码:
public static String SHA512(String ta, String key, String xtra){
Digest digest = new SHA512Digest ();
HMac hmac = new HMac(digest);
String temp =ta.concat(key.concat(xtra));
hmac.init(new KeyParameter(key.getBytes()));
hmac.update(temp.getBytes(), 0, temp.length());
byte[] resBuf = new byte[digest.getDigestSize()];
hmac.doFinal(resBuf, 0);
String resStr = convertToHex(resBuf);
return resStr;
}
我在哪里犯了错误?如果有人知道这件事,请分享我来解决这个问题