9

我正在开发 Objective C Cocoa 应用程序。我在 CommonCrypto 中测试了 CC_MD5,它工作得很好;但是,当我给它 5 千兆字节的文件时,我的整个计算机冻结并崩溃了。MD5 算法将输入处理为 512 字节的块,并不真正需要一次所有输入。Objective C 或 C 中是否有一个库要求下一个 512 字节块而不是一次获取所有输入?

4

3 回答 3

14

在 obj-C 中计算大文件的 MD5 有一个很棒的线程: http ://www.iphonedevsdk.com/forum/iphone-sdk-development/17659-calculating-md5-hash-large-file.html

这是有人在那里提出的解决方案:

+(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;
}
于 2012-06-11T22:33:03.220 回答
3

CC_MD5()旨在一次处理其所有输入。5GB 可能比它在任何地方实际存储的容量都多。CC_MD5_CTX对于较大的数据,如果您使用、CC_MD5_Init()CC_MD5_Update()和,CommonCrypto 可以一次对数据块进行操作CC_MD5_Final()。查看 CommonCrypto 文档或 Google 以获取更多信息和示例代码。

于 2012-06-11T22:52:15.430 回答
2

这是使用调度 api 的更好方法,以提高效率。我在生产中使用它,它工作正常!

    #import "CalculateMD5.h"

// Cryptography
#include <CommonCrypto/CommonDigest.h>

@implementation CalculateMD5

- (id)init
{
    self = [super init];
    if (self)
    {
        MD5ChecksumOperationQueue = dispatch_queue_create("com.test.calculateMD5Checksum", DISPATCH_QUEUE_SERIAL);
    }
    return self;
}

- (void)closeReadChannel
{
    dispatch_async(MD5ChecksumOperationQueue, ^{
        dispatch_io_close(readChannel, DISPATCH_IO_STOP);
    });
}

- (void)MD5Checksum:(NSString *)pathToFile TCB:(void(^)(NSString *md5, NSError *error))tcb
{
    // Initialize the hash object
    __block CC_MD5_CTX hashObject;
    CC_MD5_Init(&hashObject);

    readChannel = dispatch_io_create_with_path(DISPATCH_IO_STREAM,
                                               pathToFile.UTF8String,
                                               O_RDONLY, 0,
                                               MD5ChecksumOperationQueue,
                                               ^(int error) {
                                                   [self closeReadChannel];
                                               });

    if (readChannel == nil)
    {
        NSError* e = [NSError errorWithDomain:@"MD5Error"
                                         code:-999 userInfo:@{
                   NSLocalizedDescriptionKey : @"failed to open file for calculating MD5."
                      }];
        tcb(nil, e);
        return;
    }

    dispatch_io_set_high_water(readChannel, 512*1024);

    dispatch_io_read(readChannel, 0, SIZE_MAX, MD5ChecksumOperationQueue, ^(bool done, dispatch_data_t data, int error) {
        if (error != 0)
        {
            NSError* e = [NSError errorWithDomain:@"ExamSoftMD5"
                                             code:error userInfo:@{
                       NSLocalizedDescriptionKey : @"failed to read from file for calculating MD5."
                          }];
            tcb(nil, e);
            [self closeReadChannel];
            return;
        }

        if (dispatch_data_get_size(data) > 0)
        {
            const void *buffer = NULL;
            size_t size = 0;
            data = dispatch_data_create_map(data, &buffer, &size);

            CC_MD5_Update(&hashObject, (const void *)buffer, (CC_LONG)size);
        }

        if (done == YES)
        {
            // Compute the hash digest
            unsigned char digest[CC_MD5_DIGEST_LENGTH];
            CC_MD5_Final(digest, &hashObject);

            // Compute the string result
            char *hash = calloc((2 * sizeof(digest) + 1), sizeof(char));
            for (size_t i = 0; i < sizeof(digest); ++i)
            {
                snprintf(hash + (2 * i), 3, "%02x", (int)(digest[i]));
            }

            tcb(@(hash), nil);

            [self closeReadChannel];
        }
    });
}


@end
于 2013-05-16T09:22:56.047 回答