最近做了一个拍照应用,要将图片上传到服务器,因此需要用到md5校验文件,适用下面的代码就好:
#import
define CHUNK_SIZE 1024*8
(NSString )getFileMD5AtPath:(NSString )path{
NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:path];
if( handle== nil ) return @”ERROR GETTING FILE MD5”; // file didnt existCC_MD5_CTX md5;
CC_MD5_Init(&md5);
BOOL done = NO;
while(!done)
{
NSData* fileData = [handle readDataOfLength:CHUNK_SIZE];CC_MD5_Update(&md5, [fileData bytes], (CC_LONG)[fileData length]); if( [fileData length] == 0 ) done = YES;
}
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],
dig