Reputation: 107
as I'm very new to this the question may seem so trivial, so please don't call me by my parents name...I've unsigned char and NSString..i want to assign the char value to the string...this is the code snippet..
NSString *passwordDgst = @"somepassword";
unsigned char passwordDgstchar[CC_SHA512_DIGEST_LENGTH];
CC_SHA512(passwordDgst,
passwordDgst.length,
passwordDgstchar);
Now I want to assign the value of unsigned char passwordDgstchar to the string passwordDgst. How can I achieve that?
Upvotes: 1
Views: 6854
Reputation: 86651
You have some issues here. That function takes binary input and gives binary output. Your code computes the hash of the first 12 bytes of the string object. You need to first convert it to an NSData.
NSData* input = [passwordDgst dataUsingEncoding: NSUTF8StringEncoding]; // Could use UTF16 or other if you like
unsigned char passwordDgstchar[CC_SHA512_DIGEST_LENGTH];
CC_SHA512([input bytes],
[input length],
passwordDgstchar);
passwordDgstchar now contains the digiest in raw binary. To get it in (say) hex:
NSMutableString* sha512 = [[NSMutableString alloc] init];
for (int i = 0 ; i < CC_SHA512_DIGEST_LENGTH ; ++i)
{
[sha512 appendFormat: @"%02x", passwordDgstChar[i];
}
Upvotes: 6