Re: How to encrypt a String to a SHA-1 Encrypted in iPhone
Re: How to encrypt a String to a SHA-1 Encrypted in iPhone
- Subject: Re: How to encrypt a String to a SHA-1 Encrypted in iPhone
- From: Tharindu Madushanka <email@hidden>
- Date: Sat, 27 Nov 2010 14:09:15 +0530
Hi,
It was just I need to add <CommonCrypto/CommonHMAC.h> header. But above code
didn't work. But following encoded to SHA-1 correctly..
I would like to know whether it encodes right ? or Not ?
<CommonCrypto/CommonHMAC.h>
+(NSString *)stringToSha1:(NSString *)hashkey{
// Using UTF8Encoding
const char *s = [hashkey cStringUsingEncoding:NSUTF8StringEncoding];
NSData *keyData = [NSData dataWithBytes:s length:strlen(s)];
// This is the destination
uint8_t digest[CC_SHA1_DIGEST_LENGTH] = {0};
// This one function does an unkeyed SHA1 hash of your hash data
CC_SHA1(keyData.bytes, keyData.length, digest);
// Now convert to NSData structure to make it usable again
NSData *out = [NSData dataWithBytes:digest
length:CC_SHA1_DIGEST_LENGTH];
// description converts to hex but puts <> around it and spaces every 4
bytes
NSString *hash = [out description];
hash = [hash stringByReplacingOccurrencesOfString:@" " withString:@""];
hash = [hash stringByReplacingOccurrencesOfString:@"<" withString:@""];
hash = [hash stringByReplacingOccurrencesOfString:@">" withString:@""];
// hash is now a string with just the 40char hash value in it
return hash;
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden