Based on this question , I wrote in the category of copies NSStringto hash NSString, using SHA1. However, something is wrong with my implementation. The funny thing is that registering an NSData instance gives the expected hash, but when I want to create an NSString from that NSData instance, I just get null.
- (NSString *)sha1 {
NSData *dataFromString = [self dataUsingEncoding:NSUTF8StringEncoding];
unsigned char hashed[CC_SHA1_DIGEST_LENGTH];
if ( CC_SHA1([dataFromString bytes], [dataFromString length], hashed) ) {
NSData *dataFromDigest = [NSData dataWithBytes:hashed length:CC_SHA1_DIGEST_LENGTH];
NSString *result = [[NSString alloc] initWithBytes:[dataFromDigest bytes] length:[dataFromDigest length] encoding:NSUTF8StringEncoding];
return result;
} else {
return nil;
}
}
Thanks for the help!
source
share