-
Notifications
You must be signed in to change notification settings - Fork 17
/
NSString+MD5.m
executable file
·34 lines (26 loc) · 1.16 KB
/
NSString+MD5.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//
// NSString+MD5.m
// FileShuttle
//
// Created by Michaël on 26/04/11.
//
#import "NSString+MD5.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
@implementation NSString (MD5)
///////////////////////////////////////////////////////////////////////////////////////////////////
- (NSString*)md5 {
NSData *md5data = [self dataUsingEncoding:NSUTF8StringEncoding];
const char *md5datastring = [md5data bytes];
// Create byte array of unsigned chars
unsigned char md5Buffer[CC_MD5_DIGEST_LENGTH];
// Create 16 byte MD5 hash value, store in buffer
CC_MD5(md5datastring, strlen(md5datastring), md5Buffer);
// Convert MD5 value in the buffer to NSString of hex values
NSMutableString *digestStr = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
[digestStr appendFormat:@"%02x",md5Buffer[i]];
return digestStr;
}
@end