Skip to content

Instantly share code, notes, and snippets.

@Viveron
Created August 10, 2018 08:17
Show Gist options
  • Select an option

  • Save Viveron/1767eceedf43b69d4449a661d2739eac to your computer and use it in GitHub Desktop.

Select an option

Save Viveron/1767eceedf43b69d4449a661d2739eac to your computer and use it in GitHub Desktop.
NSString md5 hash category
#import <Foundation/Foundation.h>
@interface NSString (Hash)
- (NSString *)md5;
+ (NSString *)stringWithHash:(unsigned char *)hash
digestLength:(int32_t)digestLength;
@end
#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