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: Aaron Burghardt <email@hidden>
- Date: Sun, 28 Nov 2010 06:36:18 -0500
Hi,
Your implementation should work, but I have a few suggestions for you.
* Unless you need HMAC specifically, the function you are using is in <CommonCrypto/CommonDigest.h>.
* In general, I think Apple discourages relying on the output of the "description", although I see that the documentation for NSData states that description will produce a string in property list format, so it is unlikely to change. For your needs, there is a better way anyway.
* While there is nothing wrong with using a class method, consider adding a category method to the class.
* You can verify your results with "openssl" on the command line. In Terminal:
$ echo -n "Some text to hash" | openssl dgst -sha1
or
$ openssl dgst -sha1 /path/to/file/to/hash
* NSString has a method that returns an NSData, so you don't have to create an intermediate C string.
* SHA1 and MD5 are hashes, not encryption. Strictly speaking it is incorrect to refer to them as encryption.
Here is an alternate implementation with an example usage:
#import <Foundation/Foundation.h>
#include <CommonCrypto/CommonDigest.h>
@interface NSData (NSDataDigestCategory)
- (NSString *)sha1;
@end
@implementation NSData (NSDataDigestCategory)
- (NSString *)sha1 {
uint8_t digest[CC_SHA1_DIGEST_LENGTH];
CC_SHA1([self bytes], [self length], digest);
// use an uppercase X to get an uppercase hash
NSString *hash = [NSString stringWithFormat:@"xxxxxxxxxxxxxxxxxxxx",
digest[0], digest[1], digest[2], digest[3], digest[4], digest[5], digest[6], digest[7], digest[8], digest[9],
digest[10], digest[11], digest[12], digest[13], digest[14], digest[15], digest[16], digest[17], digest[18], digest[19], digest[20]];
// or, if you prefer:
NSMutableString *hash2 = [NSMutableString stringWithCapacity:40];
for (int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++)
[hash2 appendFormat:@"x", digest[i]];
return hash; // or hash2
}
@end
@interface NSString (NSStringDigestCategory)
- (NSString *)sha1;
@end
@implementation NSString (NSStringDigestCategory)
- (NSString *)sha1 {
NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding];
return [data sha1];
}
@end
int main(int argc, char *argv[]) {
NSAutoreleasePool *pool = [NSAutoreleasePool new];
NSLog(@"SHA1: %@", [@"Some text to hash" sha1]);
[pool drain];
return 0;
}
On Nov 27, 2010, at 3:39 AM, Tharindu Madushanka wrote:
> 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
_______________________________________________
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