sha-512在IOS Simulator上无法正常工作

时间:2013-01-03 08:08:07

标签: ios

我正在实现将在IOS上运行的移动应用程序sha-512功能,此功能在包括Safari在内的所有浏览器上都能正常运行。

我正在使用IOS Simulator测试我的应用程序。

我的应用程序从一页/单击调用sha-512函数三次。问题是,在第一次调用时sha-512函数产生正确的结果,但在第二次和第三次调用时它会产生错误的结果。

提前致谢

1 个答案:

答案 0 :(得分:0)

这是我的代码

//Creating Hash Value
NSString *hashkey = [NSString stringWithFormat:@"data"];
// PHP uses ASCII encoding, not UTF
NSLog(@"hashkey : %@",hashkey);
const char *s = [hashkey cStringUsingEncoding:NSASCIIStringEncoding];
NSData *keyData = [NSData dataWithBytes:s length:strlen(s)];

// This is the destination SHA512_Final
uint8_t digest[CC_SHA512_DIGEST_LENGTH] = {0};
// This one function does an unkeyed SHA1 hash of your hash data
CC_SHA512(keyData.bytes, keyData.length, digest);

// Now convert to NSData structure to make it usable again
NSData *out = [NSData dataWithBytes:digest length:CC_SHA512_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:@""];

希望它可以帮助你..