1.
This function will hash a string using SHA512. The resulting string is a hex representation of the hash:
+ (NSString *) createSHA512:(NSString *)source {
const char *s = [source cStringUsingEncoding:NSASCIIStringEncoding];
NSData *keyData = [NSData dataWithBytes:s length:strlen(s)];
uint8_t digest[CC_SHA512_DIGEST_LENGTH] = {0};
CC_SHA512(keyData.bytes, keyData.length, digest);
NSData *out = [NSData dataWithBytes:digest length:CC_SHA512_DIGEST_LENGTH];
return [out description];
}
Don't forget to include the correct header:
#include <CommonCrypto/CommonDigest.h>
2.
I am using this one: It matches PHP SHA512 algorithm output.
(NSString *)createSHA512:(NSString *)string
{
const char *cstr = [string cStringUsingEncoding:NSUTF8StringEncoding];
NSData *data = [NSData dataWithBytes:cstr length:string.length];
uint8_t digest[CC_SHA512_DIGEST_LENGTH];
CC_SHA512(data.bytes, data.length, digest);
NSMutableString* output = [NSMutableString StringWithCapacity:CC_SHA512_DIGEST_LENGTH * 2];
for(int i = 0; i < CC_SHA512_DIGEST_LENGTH; i++)
[output appendFormat:@"%02x", digest[i]];
return output;
}
출처 :
http://stackoverflow.com/questions/3829068/hash-a-password-string-using-sha512-like-c-sharp
'iPhone dev. > Objective C general' 카테고리의 다른 글
UIImage 용량 줄이기 (0) | 2013.02.21 |
---|---|
“_OBJC_CLASS_$_”, referenced from: error in xcode 4.3.2(코드 옮겼을 때) (0) | 2012.07.02 |
Cyclic한 import 해결(xcode unknown type name) (0) | 2012.04.05 |