Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Cocoa and SHA1 hashing

Cocoa and SHA1 hashing
Thread Tools
Thinine
Mac Elite
Join Date: Jul 2002
Status: Offline
Reply With Quote
Jul 9, 2005, 11:54 PM
 
I need to hash an NSString using SHA1 and return the result. Here's the method I have now, which doesn't seem to work:
Code:
- (NSMutableString *) hashInfoString: (NSString *) aString { NSMutableString *hashValue = [[NSMutableString alloc] initWithCapacity: 20]; uint8_t md[SHA_DIGEST_LENGTH]; unsigned char *stringToBeHashed[[aString length]]; NSLog(@"stringToBeHashed allocated, going to coversion loop."); for (int i = 0; i < [aString length]; i++) { stringToBeHashed[i] = [aString characterAtIndex:i]; } NSLog(@"Through conversion of NSString to char, string to be hashed is %C", stringToBeHashed); unsigned char *answer; answer = SHA1(stringToBeHashed, (unsigned long)6, md); NSLog(@"SHA1 function done, appending mutable string, answer is %C", answer); [hashValue appendFormat: @"%C", answer]; NSLog(@"Mutable string appended, returning."); return hashValue; }
Please be gentle, I really have no idea what I'm doing.

Basically, I need to know how to covert my NSString to a char, use SHA1 on that char, and turn the result back into an NSString. All this while I need to ensure I'm using the proper string formats.

Any thoughts on hashing a file in Cocoa are also appreciated.
     
Thinine  (op)
Mac Elite
Join Date: Jul 2002
Status: Offline
Reply With Quote
Jul 10, 2005, 02:21 AM
 
Okay, it looks like this now:
Code:
- (NSString *) hashInfoString: (NSString *) aString { uint8_t md[SHA_DIGEST_LENGTH]; const char* stringToBeHashed = [aString UTF8String]; NSLog(@"Through conversion of NSString to char, string to be hashed is %s", stringToBeHashed); char* answer; answer = SHA1(stringToBeHashed, (unsigned long)[aString length], md); NSLog(@"SHA1 function done, appending mutable string, answer is %s", answer); NSString *hashValue = [NSString stringWithUTF8String: answer]; NSLog(@"String created, returning."); return hashValue; }
Problem is, the returned NSString is null.
     
smeger
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status: Offline
Reply With Quote
Jul 10, 2005, 02:50 AM
 
The returned string 'answer' probably doesn't have its zero terminator, since there's no room for it. Also, "answer' and 'md' are probably the same pointer. Try these (untested) changes:
Code:
uint8_t md[SHA_DIGEST_LENGTH+1]; char *answer = SHA1(stringToBeHashed, strlen(stringToBeHashed), md); md[SHA_DIGEST_LENGTH] = 0;
Geekspiff - generating spiffdiddlee software since before you began paying attention.
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 06:41 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,