Created
August 10, 2018 08:17
-
-
Save Viveron/1767eceedf43b69d4449a661d2739eac to your computer and use it in GitHub Desktop.
NSString md5 hash category
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 <Foundation/Foundation.h> | |
| @interface NSString (Hash) | |
| - (NSString *)md5; | |
| + (NSString *)stringWithHash:(unsigned char *)hash | |
| digestLength:(int32_t)digestLength; | |
| @end |
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 "NSString+Hash.h" | |
| #import <CommonCrypto/CommonCrypto.h> | |
| @implementation NSString (Hash) | |
| - (NSString *)md5 { | |
| const char * pointer = [self UTF8String]; | |
| unsigned char md5Buffer[CC_MD5_DIGEST_LENGTH]; | |
| CC_MD5(pointer, (CC_LONG)strlen(pointer), md5Buffer); | |
| return [NSString stringWithHash:md5Buffer digestLength:CC_MD5_DIGEST_LENGTH]; | |
| } | |
| + (NSString *)stringWithHash:(unsigned char *)hash | |
| digestLength:(int32_t)digestLength { | |
| NSMutableString *string = [NSMutableString stringWithCapacity:digestLength * 2]; | |
| for (int i = 0; i < digestLength; i++) { | |
| [string appendFormat:@"%02x", hash[i]]; | |
| } | |
| return [string copy]; | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment