从同一文件获得不同的MD5校验和

4

我正在从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];
}

MD5方法的实现:

#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

md5 的实现是什么? - Carl Veazey
MD5的实现 - Pitono
当您连续两次获取数据时,调用isEqualToData:会返回YES还是NO?我想知道是否有一些元数据通过访问文件进行更新(特别是像“上次访问”这样的内容),以便每次获取相同的视频但不同的元数据。 - Carl Veazey
你能发一下你进行比较的代码吗?你可能在使用“==”来比较两个字符串,而不是[string1 isEqualToString:string2]吗? - Eric G
1个回答

0

我可以确认这个行为。无论选择哪种哈希函数(我尝试了MD5、SHA1和SHA256),它都会发生。

问题似乎出在NSData上。我从完全相同的文件创建了两个NSData实例,然后使用[data1 isEqualTo:data2],结果返回true。然而,将它们通过CC_SHA1哈希算法运行会返回不同的哈希值。

为了解决这个问题,放弃使用NSData。相反,使用更低级别的C API打开文件。以下是一个C语言示例。请注意“create”约定——当您完成后,您需要负责释放返回的char*缓冲区。使用这种方法每次都会产生相同的哈希值。

char* _Nullable createBufferWithContentsOfFileAndReportLength(NSString* _Nonnull fileToScan, long* _Nullable length)
{
    char const *pathToFile = [fileToScan cStringUsingEncoding:NSUTF8StringEncoding];
    
    FILE *file = fopen(pathToFile, "r");
    if (file == NULL) {
        NSLog(@"Unable to open file: %@", fileToScan);
        return NULL;
    }
    
    //
    //  Get the file length.
    //  'fileLength' will be the number of bytes in the file. It will not include a null terminator or EOF. Some UTF8 characters require more than one byte, so we
    //  can't guarantee that fileLength is also the number of characters.
    //  NOTE: We don't use the fseek()/ftell()/rewind approach because it's not secure. See CERT FIO19-C advisory for details.
    //
    int fd = fileno(file);
    if (fd < 0) {
        NSLog(@"Unable to get a file descriptor for: %@", fileToScan);
        fclose(file);
        return NULL;
    }
    struct stat statBuffer;
    if (fstat(fd, &statBuffer) == -1) {
        NSLog(@"Unable to retrieve stats about this file: %@", fileToScan);
        fclose(file);
        return NULL;
    }
    long fileLength = statBuffer.st_size;
    
    char *buffer = malloc(sizeof(char) * (fileLength + 1));     // enough memory to read the entire file, plus a spot for the null terminator
    if (buffer == NULL) {
        NSLog(@"Unable to allocate memory for: %@", fileToScan);
        fclose(file);
        return NULL;
    }
    
    fread(buffer, fileLength, sizeof(char), file);
    buffer[fileLength] = '\0';                              // No +1; this is zero-indexed. If fileLength is 5 characters, the 5th slot in the array needs to be the null terminator.
    fclose(file);

    if (length != NULL) {
        *length = fileLength + 1;
    }
    
    return buffer;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接