12

Possible Duplicate:
Sha256 in Objective-C for iPhone

Greetings,

I'm having terrible trouble generating a SHA256 string in Objective C (probably because I'm so new to the language).

In jQuery, all I have to do is this:

var sha256String=$.sha256("Hello");

which produces the hash as expected.

But in Objective-C, I've tried the following to no avail:

NSString *pword=[[NSString alloc]
initWithString:login_pword.text];
unsigned char result[64];
CC_SHA256([pword UTF8String], [pword lengthOfBytesUsingEncoding:NSASCIIStringEncoding],result);
UIAlertView *msg=[[UIAlertView alloc] initWithTitle:@"Hi" message:result delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[msg show];
[msg release];

Is there some function that I can call such as:

NSString *sha256String=[self getSHA256:pword];

This is what I'm trying to create and I'm finding it very difficult!

I hope someone can help.

Many thanks in advance,

Community
  • 1
  • 1
Eamorr
  • 9,872
  • 34
  • 125
  • 209

3 Answers3

56

After much playing around today, I finally came up with a function to get the SHA256:

-(NSString*) sha256:(NSString *)clear{
    const char *s=[clear cStringUsingEncoding:NSASCIIStringEncoding];
    NSData *keyData=[NSData dataWithBytes:s length:strlen(s)];

    uint8_t digest[CC_SHA256_DIGEST_LENGTH]={0};
    CC_SHA256(keyData.bytes, keyData.length, digest);
    NSData *out=[NSData dataWithBytes:digest length:CC_SHA256_DIGEST_LENGTH];
    NSString *hash=[out description];
    hash = [hash stringByReplacingOccurrencesOfString:@" " withString:@""];
    hash = [hash stringByReplacingOccurrencesOfString:@"<" withString:@""];
    hash = [hash stringByReplacingOccurrencesOfString:@">" withString:@""];
    return hash;
}

This gives the same output as PHP. It can easily be converted to SHA1 - just change 'SHA256' to 'SHA1'.

Hope it helps someone.

Eamorr
  • 9,872
  • 34
  • 125
  • 209
  • It helped someone else too - thanks! – barfoon Sep 22 '11 at 21:19
  • I helped me too - thank you Eamorr! – Tuyen Nguyen May 16 '12 at 21:17
  • 3
    making your code depend on `description` seem like a very bad idea to me. Description is not meant to be machine readable but only for debugging. – max Oct 03 '13 at 13:03
  • Thank you, helped after hours of struggle. – Shawn Frank Jun 06 '14 at 21:38
  • how can i use this with a key , i mean let say i have string and key , – Mr.G Feb 27 '16 at 06:47
  • 1
    What's CC_SHA256_DIGEST_LENGTH? Got it - need to `#include ` – CyberMew Mar 18 '16 at 09:27
  • This is a bad answer as you should never rely on description to process information. Description provides information for debugging purposes only, classes override the default NSObject method (that prints the memory address of the object) to provide useful information for debugging. A change on what this method actually does can happen at any time without notice. – Christophe Fondacci May 24 '16 at 23:54
  • Swift 3 format? – Jayprakash Dubey Jul 13 '17 at 09:36
  • This answer works. But it's better to use `[[NSString alloc] initWithData:out encoding:NSASCIIStringENcoding]` instead of using `NSData`'s `description` and doing replacements. Also, naming a variable as `out` probably is not a good idea. – Zhigang An Dec 13 '17 at 18:34
3

You are passing result into the UIAlertView's init method. result is a char[], and UIAlertView expects an NSString*. You need to convert your char[] to an NSString *.

Try this:

NSString *resultString = [NSString stringWithCString:result encoding:NSASCIIStringEncoding];
UIAlertView *msg=[[UIAlertView alloc] initWithTitle:@"Hi" message:resultString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

Also see this article on hashing on the iPhone.

Marc W
  • 19,083
  • 4
  • 59
  • 71
  • Hey, I did that, but I get gobbldy-gook on the result. It looks like UTF8, when in fact I want it base64 encoded... – Eamorr Feb 14 '11 at 12:35
  • Well you aren't Base64 encoding the result data anywhere in the code you posted. Have you tried doing that first? And again, read the article I posted the link to. It is for MD5 hashing but mentions adjustments you have to make for SHA256. – Marc W Feb 14 '11 at 12:38
1

You will need to use the OpenSSL C functions. See for example this question on how to do that. As input string, you'd use [myString UTFString] with length [myString lengthOfBytesUsingEncoding:NSUTF8StringEncoding].

Community
  • 1
  • 1
DarkDust
  • 90,870
  • 19
  • 190
  • 224