Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting MD5 and SHA-1

I am looking for some help in getting MD5 and SHA-1 in my iPhone app. Can anybody give me an idea on how to get these?

like image 359
user754229 Avatar asked May 15 '11 05:05

user754229


People also ask

Is SHA1 or MD5 better?

To conclude, MD5 generates a message digest of 128-bits, while SHA1 generates a message digest of 160-bit hash value. Hence, SHA1 is a relatively complex algorithm and provides better security than MD5.

What are MD5 and SHA1?

MD5 stands for Message Digest. While SHA1 stands for Secure Hash Algorithm. 2. MD5 can have 128 bits length of message digest. Whereas SHA1 can have 160 bits length of message digest.

Is SHA1 and MD5 same?

Whereas MD5 produces a 128-bit hash, SHA1 generates 160-bit hash (20 bytes). In hexadecimal format, it is an integer 40 digits long. Like MD5, it was designed for cryptology applications, but was soon found to have vulnerabilities also.


1 Answers

#include <CommonCrypto/CommonDigest.h>

-(NSString*) sha1:(NSString*)input
{

 NSData *data = [input dataUsingEncoding: NSUTF8StringEncoding]; 

 uint8_t digest[CC_SHA1_DIGEST_LENGTH];

 CC_SHA1(data.bytes, data.length, digest);

 NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];

 for(int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++)
 [output appendFormat:@"%02x", digest[i]];

 return output;

}

- (NSString *) md5:(NSString *) input
{
 const char *cStr = [input UTF8String];
 unsigned char digest[CC_MD5_DIGEST_LENGTH];
 CC_MD5( cStr, (CC_LONG)strlen(cStr), digest ); // This is the md5 call

 NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];

 for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
 [output appendFormat:@"%02x", digest[i]];

 return  output;

}

also have a look at my blog post here - http://www.makebetterthings.com/blogs/iphone/how-to-get-md5-and-sha1-in-objective-c-ios-sdk/

like image 190
Saurabh Avatar answered Sep 18 '22 18:09

Saurabh