一、 简单的SHA512算法
// 散列函数--sha512对字符串加密
- (NSString *)sha512String {
const char *str = self.UTF8String;
uint8_t buffer[CC_SHA512_DIGEST_LENGTH];
CC_SHA512(str, (CC_LONG)strlen(str), buffer);
return [self stringFromBytes:buffer length:CC_SHA512_DIGEST_LENGTH];
}
二、使用salt的SHA512算法
@implementation NSData (CommonDigest)
- (NSData *)SHA512HashWithSalt:(NSData *)salt {
unsigned char hash[CC_SHA512_DIGEST_LENGTH];
CC_SHA512_CTX context;
CC_SHA512_Init(&context);
if ([salt length]) {
CC_SHA512_Update(&context, [salt bytes], (CC_LONG)[salt length]);
}
CC_SHA512_Update(&context, [self bytes], (CC_LONG)[self length]);
CC_SHA512_Final(hash, &context);
return [NSData dataWithBytes:hash length:CC_SHA512_DIGEST_LENGTH];
}
@end
三、使用salt并有iteration的SHA512算法
//第一种
NSData* createSymphonySHA512MessageDigest(const char* s, const char* salt ,int iteration) {
uint8_t digest[CC_SHA512_DIGEST_LENGTH];
char salted[strlen(s) + strlen(salt) + 3];
snprintf(salted, sizeof salted, "%s{%s}", s, salt);
CC_SHA512(salted, (CC_LONG) strlen(salted), digest);
for (int i = 1; i < iteration; i++) {
CC_SHA512_CTX context;
CC_SHA512_Init(&context);
CC_SHA512_Update(&context, digest, (CC_LONG) CC_SHA512_DIGEST_LENGTH);
CC_SHA512_Update(&context, salted, (CC_LONG)strlen(salted));
CC_SHA512_Final(digest, &context);
}
return [NSData dataWithBytes:digest length:CC_SHA512_DIGEST_LENGTH];
}
//第二种
- (NSData *)hashPassword:(NSString *)password ansSalt:(NSString *)salt iteration:(NSInteger)iteration{
NSString *passwordSalted = [NSString stringWithFormat:@"%@{%@}",password,salt];
NSData *passwordData = [passwordSalted dataUsingEncoding:NSUTF8StringEncoding];
uint8_t hash[CC_SHA512_DIGEST_LENGTH];
CC_SHA512([passwordData bytes], [passwordData length], hash);
NSMutableData *allData = [[NSMutableData alloc] init];
[allData appendBytes:hash length:CC_SHA512_DIGEST_LENGTH];
for (NSInteger i = 1; i < iteration; i++) {
[allData appendBytes:[passwordData bytes] length:[passwordData length]];
uint8_t hashLoop[CC_SHA512_DIGEST_LENGTH];
CC_SHA512([allData bytes], [allData length], hashLoop);
[allData setLength:0];
[allData appendBytes:hashLoop length:CC_SHA512_DIGEST_LENGTH];
}
return [NSData dataWithBytes:[allData bytes] length:[allData length]];
}
四、和java的org.apache.shiro.crypto.hash.Sha512Hash
中Sha512Hash(Object source, Object salt, int hashIterations)
一样的SHA512算法
@implementation NSData (degist)
- (NSData *)SHA512HashWithSalt:(NSData *)salt iteration:(NSInteger)iteration{
unsigned char hash[CC_SHA512_DIGEST_LENGTH];
CC_SHA512_CTX context;
CC_SHA512_Init(&context);
if ([salt length]) {
CC_SHA512_Update(&context, [salt bytes], (CC_LONG)[salt length]);
}
CC_SHA512_Update(&context, [self bytes], (CC_LONG)[self length]);
CC_SHA512_Final(hash, &context);
for (int i = 1; i < iteration; i++) {
CC_SHA512_CTX context;
CC_SHA512_Init(&context);
CC_SHA512_Update(&context, hash, (CC_LONG)CC_SHA512_DIGEST_LENGTH);
CC_SHA512_Final(hash, &context);
}
return [NSData dataWithBytes:hash length:CC_SHA512_DIGEST_LENGTH];
}
参考:
1、How to use WSSE in iOS with salted sha512 with more than 1 iteration
2、Symphony SHA512 Hashing in Objective C
3、Objective C SHA512 hash of two NSData