獲取文件MD5 值,使用OC自帶工具,使用時候需要
#import
,然後調用傳值
- (NSString*) md5:(NSString*) path
{
NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:path];
if( handle== nil ) {
return nil;
}
CC_MD5_CTX md5;
CC_MD5_Init(&md5);
BOOL done = NO;
while(!done)
{
NSData* fileData = [handle readDataOfLength: 256 ];
CC_MD5_Update(&md5, [fileData bytes], [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],
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]];
NSLog(@"ssssss = %@",s);
return s;
}
NSString *filePath = [self inDocumentsDirectory:@"01-10-201417:06:39.mov"];
NSLog(@"--- filepath = /n%@",filePath);
NSLog(@"md5 = %@/n/n",[self md5:filePath]);
#pragma mark - NSFileManager Methods
////獲取文件路徑
- (NSString *)inDocumentsDirectory:(NSString *)path {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:path];
}
// 輸出結果為
2014-01-10 17:43:36.630 testPaint[16629:60b] --- filepath =
/var/mobile/Applications/0FC082D4-371B-4DD6-A45D-FB632650D93F/Documents/01-10-201417:06:39.mov
2014-01-10 17:43:36.719 testPaint[16629:60b] ssssss = 82066e46ebce1de3a14cbd4d0d7ebf3c
2014-01-10 17:43:36.720 testPaint[16629:60b] md5 = 82066e46ebce1de3a14cbd4d0d7ebf3c