是否有不需要同时输入全部内容的MD5库? [英] Is there a MD5 library that doesn't require the whole input at the same time?
问题描述
我正在研究Objective C可可应用.我在CommonCrypto中测试了 CC_MD5 ,它工作得很好.但是,当我给它5千兆字节文件时,我的整个计算机死机并崩溃了. MD5算法将输入作为512字节的块进行处理,实际上并不需要一次全部输入.在Objective C或C中是否有一个库要求下一个512字节块,而不是一次获取所有输入?
I'm working on Objective C Cocoa application. I tested CC_MD5 in CommonCrypto, and it worked just fine; however, when I gave 5 gygabyte file to it, my whole computer froze and crashed. MD5 algorithm processes input as 512-byte chunks and doesn't really require all the input at once. Is there an library in Objective C or C that asks for next 512-byte chunk instead of taking all input at once?
推荐答案
在obj-C中,有一个关于计算大型文件的MD5的强大线程: http://www.iphonedevsdk.com/forum/iphone-sdk-development/17659-calculating-md5-hash-large-file.html
There is a great thread on calculating MD5 of large files in obj-C here: http://www.iphonedevsdk.com/forum/iphone-sdk-development/17659-calculating-md5-hash-large-file.html
这是有人想出的解决方案:
Here is the solution someone came up with there:
+(NSString*)fileMD5:(NSString*)path
{
NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:path];
if( handle== nil ) return @"ERROR GETTING FILE MD5"; // file didnt exist
CC_MD5_CTX md5;
CC_MD5_Init(&md5);
BOOL done = NO;
while(!done)
{
NSAutoreleasePool * pool = [NSAutoreleasePool new];
NSData* fileData = [handle readDataOfLength: CHUNK_SIZE ];
CC_MD5_Update(&md5, [fileData bytes], [fileData length]);
if( [fileData length] == 0 ) done = YES;
[pool drain];
}
unsigned char digest[CC_MD5_DIGEST_LENGTH];
CC_MD5_Final(digest, &md5);
NSString* s = [NSString stringWithFormat: @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
digest[0], digest[1],
digest[2], digest[3],
digest[4], digest[5],
digest[6], digest[7],
digest[8], digest[9],
digest[10], digest[11],
digest[12], digest[13],
digest[14], digest[15]];
return s;
}
这篇关于是否有不需要同时输入全部内容的MD5库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!