Created
September 3, 2010 20:32
-
-
Save developernotes/564519 to your computer and use it in GitHub Desktop.
This file contains 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 <Foundation/Foundation.h> | |
@interface NSString(Extensions) | |
-(NSString *) sha1Hash; | |
@end |
This file contains 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 "NSString(Extensions).h" | |
#import <CommonCrypto/CommonDigest.h> | |
@implementation NSString(Extensions) | |
-(NSString*) sha1Hash { | |
const char* cString = [self UTF8String]; | |
unsigned char sha1Hash[CC_SHA1_DIGEST_LENGTH]; | |
CC_SHA1(cString, strlen(cString), sha1Hash); | |
NSString *sha1HashString = [NSString stringWithFormat: | |
@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", | |
sha1Hash[0], sha1Hash[1], sha1Hash[2], sha1Hash[3], sha1Hash[4], | |
sha1Hash[5], sha1Hash[6], sha1Hash[7], sha1Hash[8], sha1Hash[9], | |
sha1Hash[10], sha1Hash[11], sha1Hash[12], sha1Hash[13], sha1Hash[14], | |
sha1Hash[15], sha1Hash[16], sha1Hash[17], sha1Hash[18], sha1Hash[19]]; | |
return [sha1HashString lowercaseString]; | |
} | |
@end |
This file contains 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 <SenTestingKit/SenTestingKit.h> | |
#define HC_SHORTHAND | |
#import <OCHamcrest/OCHamcrest.h> | |
#import "NSString(Extensions).h" | |
@interface NSStringTests : SenTestCase | |
@end | |
@implementation NSStringTests | |
-(void) test_sha1_hash_of_text { | |
NSString* text = @"hello"; | |
NSString* sha1Hash = [text sha1Hash]; | |
assertThat(sha1Hash, is(equalTo(@"aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d"))); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment