如何在Objective-C中计算MD5?
如何在Objective-C中计算MD5?
在iPhone上,可以将MD5作为NSString
和NSData
的附加项添加,例如以下方法:
MyAdditions.h
@interface NSString (MyAdditions)
- (NSString *)md5;
@end
@interface NSData (MyAdditions)
- (NSString*)md5;
@end
MyAdditions.m
#import "MyAdditions.h"
#import <CommonCrypto/CommonDigest.h> // Need to import for CC_MD5 access
@implementation NSString (MyAdditions)
- (NSString *)md5
{
const char *cStr = [self UTF8String];
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5( cStr, (int)strlen(cStr), result ); // This is the md5 call
return [NSString stringWithFormat:
@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
result[0], result[1], result[2], result[3],
result[4], result[5], result[6], result[7],
result[8], result[9], result[10], result[11],
result[12], result[13], result[14], result[15]
];
}
@end
@implementation NSData (MyAdditions)
- (NSString*)md5
{
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5( self.bytes, (int)self.length, result ); // This is the md5 call
return [NSString stringWithFormat:
@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
result[0], result[1], result[2], result[3],
result[4], result[5], result[6], result[7],
result[8], result[9], result[10], result[11],
result[12], result[13], result[14], result[15]
];
}
@end
添加NSData md5,因为我自己需要,并认为这是保存这个小片段的好地方...
这些方法已经使用NIST MD5测试向量进行验证, http://www.nsrl.nist.gov/testdata/
strlen
函数会产生警告:“隐式转换导致整数精度丢失:'unsigned long'转换为'CC_LONG'(也称为'unsigned int')”。 - MaxGabrielUIViewController
实例上调用UTF8String
方法。在上面的代码中,@epatel在NSString
类别上实现了一个实例方法md5
。因此,一个NSString实例,比如说值为“Hello, World!”的myString
将会这样被调用:[myString md5]
,然后md5方法将返回“65a8e27d8879283831b664bd8b7f0ad4”。 - seeker12您可以使用内置的Common Crypto库来进行操作。
记得导入:
#import <CommonCrypto/CommonDigest.h>
接着:
- (NSString *) md5:(NSString *) input
{
const char *cStr = [input UTF8String];
unsigned char digest[CC_MD5_DIGEST_LENGTH];
CC_MD5( cStr, strlen(cStr), digest ); // This is the md5 call
NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
[output appendFormat:@"%02x", digest[i]];
return output;
}
strlen
之前加上(int)
转换,例如(int)strlen
... - brandonscriptstringWithFormat
或 NSMutableString
的版本快大约5倍。- (NSString *)md5
{
const char* cStr = [self UTF8String];
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5(cStr, strlen(cStr), result);
static const char HexEncodeChars[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
char *resultData = malloc(CC_MD5_DIGEST_LENGTH * 2 + 1);
for (uint index = 0; index < CC_MD5_DIGEST_LENGTH; index++) {
resultData[index * 2] = HexEncodeChars[(result[index] >> 4)];
resultData[index * 2 + 1] = HexEncodeChars[(result[index] % 0x10)];
}
resultData[CC_MD5_DIGEST_LENGTH * 2] = 0;
NSString *resultString = [NSString stringWithCString:resultData encoding:NSASCIIStringEncoding];
free(resultData);
return resultString;
}
既然有人要求文件流版本,我修改了Joel Lopes Da Silva的一个小代码片段,它可以使用MD5、SHA1和SHA512,并且使用流。它是为iOS设计的,但在OSX上只需进行最小的更改即可(删除ALAssetRepresentation方法)。它可以针对给定文件路径或ALAssets(使用ALAssetRepresentation)生成校验和。它将数据分成小包,使内存影响最小,无论文件大小/资产大小。
它目前位于github这里:https://github.com/leetal/FileHash