Skip to content

Instantly share code, notes, and snippets.

@priore
Created October 25, 2013 23:37
Show Gist options
  • Save priore/7163455 to your computer and use it in GitHub Desktop.
Save priore/7163455 to your computer and use it in GitHub Desktop.
SHA1 encode NSString
// SHA1
#import <CommonCrypto/CommonDigest.h>
- (NSString*)getSHA1WithString:(NSString*)string
{
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
uint8_t digest[CC_SHA1_DIGEST_LENGTH];
CC_SHA1(data.bytes, (CC_LONG)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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment