我正在从 iphone 画廊的视频文件中计算 md5 总和。每次我选择同一个文件时,它都有不同的 md5 总和。我还检查了以字节为单位的数据长度,它保持不变。所以我的问题是——为什么?这是我尝试制作的多种方法中的一种代码。
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:@"public.movie"])
{
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
videoData = [NSData dataWithContentsOfURL:videoURL];
[videoData retain];
NSLog(@"VIDEO DATA MD5: %@", [videoData md5]);
NSLog(@"VIDEO DATA LEN: %d", videoData.length);
}
[self dismissModalViewControllerAnimated:YES];
}
#import <CommonCrypto/CommonDigest.h>
@implementation NSData(MD5)
- (NSString*)MD5
{
// Create byte array of unsigned chars
unsigned char md5Buffer[CC_MD5_DIGEST_LENGTH];
// Create 16 byte MD5 hash value, store in buffer
CC_MD5(self.bytes, self.length, md5Buffer);
// Convert unsigned char buffer to NSString of hex values
NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
[output appendFormat:@"%02x",md5Buffer[i]];
return output;
}
@end