Objective-C 中的 MD5 算法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1524604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MD5 algorithm in Objective-C
提问by Biranchi
How to calculate the MD5 in Objective-C?
如何计算Objective-C中的MD5?
回答by epatel
md5 is available on the iPhone and can be added as an addition for ie NSStringand NSDatalike below.
md5 可在 iPhone 上使用,并且可以作为 ie 的附加项添加NSString,NSData如下所示。
MyAdditions.h
MyAdditions.h
@interface NSString (MyAdditions)
- (NSString *)md5;
@end
@interface NSData (MyAdditions)
- (NSString*)md5;
@end
MyAdditions.m
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
EDIT
编辑
Added NSData md5 because I needed it myself and thought this is a good place to save this little snippet...
添加 NSData md5 因为我自己需要它并且认为这是保存这个小片段的好地方......
These methods are verified using the NIST MD5 test vectors in http://www.nsrl.nist.gov/testdata/
这些方法使用http://www.nsrl.nist.gov/testdata/ 中的 NIST MD5 测试向量进行验证
回答by Bruno Koga
You can use the built-in Common Crypto library to do so. Remember to import:
您可以使用内置的 Common Crypto 库来执行此操作。记得导入:
#import <CommonCrypto/CommonDigest.h>
and then:
进而:
- (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;
}
回答by Pavel Alexeev
If performance is important, you can use this optimized version.
It is about 5 times faster than the ones with stringWithFormator NSMutableString.
如果性能很重要,您可以使用此优化版本。它比带有stringWithFormat或 的快 5 倍NSMutableString。
This is a category of NSString.
这是 NSString 的一个类别。
- (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;
}
回答by vpathak
Any reason not to use the Apple implementation: https://developer.apple.com/library/mac/documentation/Security/Conceptual/cryptoservices/GeneralPurposeCrypto/GeneralPurposeCrypto.html#//apple_ref/doc/uid/TP40011172-CH9-SW1
任何不使用 Apple 实现的理由:https: //developer.apple.com/library/mac/documentation/Security/Conceptual/cryptoservices/GeneralPurposeCrypto/GeneralPurposeCrypto.html#//apple_ref/doc/uid/TP40011172-CH9-SW1
Search for Cryptographic Services Guide on Apple developer site.
在 Apple 开发者网站上搜索加密服务指南。
回答by Alexander W
Well since people asked for a file-stream version. I have modified a nice little snippet made by Joel Lopes Da Silva that works with MD5, SHA1 and SHA512 AND it is using streams. Its made for iOS but works with just minimal changes on OSX aswell (remove the ALAssetRepresentation method). It can make checksums for files given a filepath or ALAssets (using ALAssetRepresentation). It's chunking data into small packages making memory impact minimal regardless of the filesize/asset size.
好吧,因为人们要求提供文件流版本。我修改了 Joel Lopes Da Silva 制作的一个很好的小片段,它适用于 MD5、SHA1 和 SHA512,并且它正在使用流。它是为 iOS 制作的,但在 OSX 上也只需要很少的更改(删除 ALAssetRepresentation 方法)。它可以对给定文件路径或 ALAssets(使用 ALAssetRepresentation)的文件进行校验和。无论文件大小/资产大小如何,它都将数据分块到小包中,从而使内存影响最小。
It's currently located on github here: https://github.com/leetal/FileHash
它目前位于 github 上:https: //github.com/leetal/FileHash

