Make Better Things



I like to make better things.

How to get md5 and SHA1 in objective c (iOS sdk)

Calculating the md5 and sha1 hash in iOS sdk is pretty simple -

Step 1 – The very first thing you need to do is import CommonCrypto’s CommonDigest.h

#import <CommonCrypto/CommonDigest.h>

Step 2 – Here is the real code for calculating SHA1 and MD5 hash -

SHA1 -

-(NSString*) sha1:(NSString*)input
{
 const char *cstr = [input cStringUsingEncoding:NSUTF8StringEncoding];
 NSData *data = [NSData dataWithBytes:cstr length:input.length];

 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;

}

MD5 -

- (NSString *) md5:(NSString *) input
{
 const char *cStr = [input UTF8String];
 unsigned char digest[16];
 CC_MD5( cStr, 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;

}

Hope it will help someone!!

Category: iPhone, Objective-C

Tagged:

17 Responses

  1. Shaon says:

    Thanks for putting this up and saving me a lot of time – i was looking for a simple demonstation of SHA-1 and this is perfect!

  2. Abhilash Vijayakumar says:

    Hi any example how to invoke your above function to calculate md5. My code is causing app to crash. Thanks

  3. Guybrush says:

    The output of the SHA1 function using the method above has spaces in it. It is therefore obviously not a valid SHA-1 hash.

    Please help… why are there spaces in the output?

  4. Dave says:

    Thank you. It saved a lot of time for me…

  5. John says:

    Thanks for the code. Saved me alot of time.

    Awesome!

    Thanks

  6. Amit says:

    hi,
    this code is only to convert text string into md5 string.but how we will decrypt the string from md5 string into original format.

    • Saurabh says:

      Well… md5 and sha1 is nondecrypting formats.. no one can decrypt string from md5 or sha1 hash…

    • Charles says:

      MD5 and SHA1 belong to class of functions known as secure ONE-WAY hash functions. This means that the input is digested into a “supposedly’ unique hash value. I say, “supposedly” as it has been demonstrated that more than one input can result in the same MD5 and/or SHA1 hash digest.

      The hash digest value serves as a “signature” of another document. So, the fact that the same signature can be generated for two different documents is a potential security threat – but, I digress.

      Frequently, the signature is used in conjunction with public key cryptography (i.e. RSA or elliptic curve) to sign the signature.

      So, a document is “signed by”
      1. Generating the secure hash (i.e. MD5 or SHA1).
      2. Decrypting the secure using the public key algorithm. This implies that both the public and private key is utilized to create the actual digital signature.
      3. The digital signature is passed along with the original document.

      To verify the signature…
      1) Extract the digital signature from the document.
      2) “Encrypt” the digital signature using the sender’s public key to obtain the secure hash value.
      3) Generate your own copy of the secure hash on the document.
      4) Compare your copy of the secure hash with the version extracted in step 2.

      If two hashes match, the signature is valid.

  7. Roman says:

    Hi.
    I have a question.
    If I have to code in my whole string some password, could I do it, not the whole code?
    https://www.w.eu/wikifood/en/struts/userfoodDB.do?method=getProductOverview&query=4009680362183&startAt=0&limit=5&filter=true&loginname=NNNNNN&password=PPPPPPPPPP(6f052c60e315a4c2813baf3775c651dsea1f4fe2)- and password I need to code with SHA1?
    [output appendFormat:@"%02x", digest[i]]; – should I do some changes hear?

  8. I found a bug in the sha1 encoder while using it to checksum data uploaded to a server: The length sent to dataWithBytes:Length: will in some cases be too short because of character encoding.

    The fix is to replace it with this:
    NSData *data = [NSData dataWithBytes:cstr length:[input lengthOfBytesUsingEncoding:NSUTF8StringEncoding]];

  9. Anonymous says:

    If string has non-ascii characters, SHA-1 will be calcualted wrong.

    To fix that replace those two lines
    const char *cstr = [input cStringUsingEncoding:NSUTF8StringEncoding];
    NSData *data = [NSData dataWithBytes:cstr length:input.length];

    with this one
    NSData *data = [input dataUsingEncoding:NSUTF8StringEncoding];

  10. [...] How to get md5 and SHA1 in objective c (iOS sdk) stackoverflow: MD5 algorithm in Objective [...]

  11. Chuy says:

    You, sir, deserve a beer.

  12. Hareesh says:

    Hello,

    In above code,

    - (NSString *) md5:(NSString *) input
    {
    const char *cStr = [input UTF8String];
    unsigned char digest[16];
    CC_MD5( cStr, 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;

    }

    What we need to pass as input?

    Please help me

Leave a Reply to Objective-C事始め - md5, sha1, sha2を作りたい « イナヅマTVログ × Cancel reply