Skip to content

Instantly share code, notes, and snippets.

@Nitewriter
Created January 9, 2014 19:10
Show Gist options
  • Save Nitewriter/8340060 to your computer and use it in GitHub Desktop.
Save Nitewriter/8340060 to your computer and use it in GitHub Desktop.
#import "NSData+Hash.h"
#import <CommonCrypto/CommonDigest.h>
@implementation NSData (Hash)
- (NSData *)SHA1
{
unsigned char hash[CC_SHA1_DIGEST_LENGTH];
if (CC_SHA1([self bytes], [self length], hash))
{
return [NSData dataWithBytes:hash
length:CC_SHA1_DIGEST_LENGTH];
}
return nil;
}
- (NSString *)SHA1String
{
NSMutableString *output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH];
NSData *SHA1 = [self SHA1];
if (SHA1.length == CC_SHA1_DIGEST_LENGTH)
{
uint8_t digest[CC_SHA1_DIGEST_LENGTH];
[SHA1 getBytes:&digest length:CC_SHA1_DIGEST_LENGTH];
for (int index = 0; index < CC_SHA1_DIGEST_LENGTH; index++)
{
[output appendFormat:@"%02x", digest[index]];
}
return output;
}
return nil;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment