priore

SHA1 encode NSString

Aug 23rd, 2013
533
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // SHA1
  2. #import <CommonCrypto/CommonDigest.h>
  3. - (NSString*)getSHA1WithString:(NSString*)string
  4. {
  5.     NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
  6.     uint8_t digest[CC_SHA1_DIGEST_LENGTH];
  7.     CC_SHA1(data.bytes, (CC_LONG)data.length, digest);
  8.    
  9.     NSMutableString *output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
  10.     for (int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++)
  11.     {
  12.         [output appendFormat:@"%02x", digest[i]];
  13.     }
  14.    
  15.     return output;
  16. }
RAW Paste Data