SHA1 Encrypt Function for the iPhone (Objective-C)

Here is a little function that I found on the Web that I want share it with you.

First you need access the CommonCrypto library, so include the line below in your .h file,


#import <CommonCrypto/CommonDigest.h>

Then add the method below to the .m file.  It accepts a string and returns it as a SHA1 string.


-(NSString*) digest:(NSString*)input
{
  NSData *data = [input dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
  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;
}

That’s it!  I hope this helps.

Focus Mode

Contact Request

Close

We will call you right away. All information is kept private