Created
January 9, 2014 19:10
-
-
Save Nitewriter/8340060 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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