Reputation: 505
i used the following code that i took from here.
- (NSData *)sha256:(NSData *)data {
unsigned char hash[CC_SHA256_DIGEST_LENGTH];
if ( CC_SHA256([data bytes], [data length], hash) ) {
NSData *sha256 = [NSData dataWithBytes:hash length:CC_SHA256_DIGEST_LENGTH];
return sha256;
}
return nil;
}
NSData *imageHash=[self sha256:imageData];
imageHashtag = [imageHash base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
I am having some problems with this though- the hash being created includes / and = that are not supposed to be there. Can someone help me figure out the mistake i am making here? and how can i solve it?
edit: I found the possible problem- i was converting the sha hash that was an base64 encoded data back into another base64string. but when i used the following code to convert the hashdata into string, i only get nil. so can someone help me with converting the sha hash into a string?
imageHashtag = [NSString stringWithUTF8String:[imageHash bytes]];
the imagehash does have 32 bits of data, but imagehashtag is nil.
Upvotes: 1
Views: 888
Reputation: 505
we added the following code to solve the problem:
NSString *hash=[sha256 description];
hash = [hash stringByReplacingOccurrencesOfString:@" " withString:@""];
hash = [hash stringByReplacingOccurrencesOfString:@"<" withString:@""];
hash = [hash stringByReplacingOccurrencesOfString:@">" withString:@""];
return hash;
to solve our problem.
Upvotes: 1