I know SHA-1 is preferred, but this project requires I use MD5.
#include
- (NSString*) MD5Hasher: (NSString*) query {
NSData* hash
These answers are correct but confusing. I post a working sample, since I had issues with most other answers. The code is tested and works well for Mac OS X 10.12.x and iOS 10.1.x.
YourClass.h
#import
@interface YourClass : NSObject
+ (NSString *) md5:(NSString *) input;
@end
YourClass.m
#import YourClass.h
+ (NSString *) md5:(NSString *) input
{
const char *cStr = [input UTF8String];
unsigned char digest[CC_MD5_DIGEST_LENGTH];
CC_MD5(cStr, (uint32_t)strlen(cStr), digest);
NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
[output appendFormat:@"%02x", digest[i]]; //%02X for capital letters
return output;
}
Usage (e.g. in some other class):
SomeOtherClass.h
#import "YourClass.h"
SomeOtherClass.m
-(void) Test
{
//call by [self Test] or use somewhere in your code.
NSString *password = @"mypassword123";
NSString *md5 = [YourClass md5:password];
NSLog(@"%@", password);
NSLog(@"%@", md5);
}