获取 iPhone 中文件的 MD_5 哈希值
在我的项目中,我需要获取 iphone 中文件的 MD_5 哈希码。到目前为止,我找到了以下代码来获取任何图像/任何文件的 md_5。
-(NSString *)getMD5FromString:(NSString *)source{
const char *src = [source UTF8String];
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5(src, strlen(src), result);
return [[NSString
stringWithFormat: @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
result[0], result[1],
result[2], result[3],
result[4], result[5],
result[6], result[7],
result[8], result[9],
result[10], result[11],
result[12], result[13],
result[14], result[15]
]lowercaseString];
}
使用此代码获取图像的 ByteContent,然后获取该图像字节数组字符串的 md_5
UIImage *image = [UIImage imageNamed:@"sf_small.png"];
NSData *data = UIImagePNGRepresentation(image);
NSString *str = [NSString stringWithFormat:@"%@",data];
NSString *temp = [self getMD5FromString:str];
现在我成功获取了哈希代码但是当在网络端时我获取同一文件的 md_5 哈希代码然后它给了我不同的哈希代码。在网络端,我使用 PHP 代码,
md5_file(string $filename);
这个 PHP 代码为我提供了不同的哈希代码,而 iphone 代码为我提供了同一图像的不同哈希代码。请告诉我可能是什么问题..
非常感谢!
In my project i need to get the MD_5 hash code of the file in iphone. uptill now i have found the following code to get md_5 of any image/any file.
-(NSString *)getMD5FromString:(NSString *)source{
const char *src = [source UTF8String];
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5(src, strlen(src), result);
return [[NSString
stringWithFormat: @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
result[0], result[1],
result[2], result[3],
result[4], result[5],
result[6], result[7],
result[8], result[9],
result[10], result[11],
result[12], result[13],
result[14], result[15]
]lowercaseString];
}
using this code to get the ByteContent of the image and then get the md_5 of that image byte array string
UIImage *image = [UIImage imageNamed:@"sf_small.png"];
NSData *data = UIImagePNGRepresentation(image);
NSString *str = [NSString stringWithFormat:@"%@",data];
NSString *temp = [self getMD5FromString:str];
now i am getting a hash code succesfully But when on the web side i get the md_5 hash code of same file then it gives me diferent hash code. in Web side i am using PHP code
md5_file(string $filename);
this PHP code gives me differnet hash code and iphone code gives me different hash code for same image. Please tell me what can be the problem..
Thanks alot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有两个原因。第一个是因为原始字节 → 字符串 → UTF-8 过程损坏了一些非 ASCII 字符。请注意,您可以从 NSData:
第二个原因是因为PNG→原始图像→PNG过程。无法保证相同的图像会在不同的库中压缩为相同的 PNG 表示形式,当然您会有不同的 MD5。您可以完全避免将文件作为图像读取,因为可以直接将文件作为数据读取:
There are 2 causes. The first is because the raw bytes → string → UTF-8 process corrupted some non-ASCII characters. Note that you can get a pointer to the bytes from an NSData directly:
The second cause is because of the PNG → raw image → PNG process. There is no guarentee that the same image will compress to the same PNG representation in different libraries, and of course you'll have different MD5. You could just avoid reading the file as image altogether, as it's possible read the file directly as data:
它不使用
UTF8String
,而使用NSMacOSRomanStringEncoding
,而是接受 8 位字符。更好的是,使用 NSData 指针而不进行转换,请参阅:@KennyTM。
Instead of outing
UTF8String
useNSMacOSRomanStringEncoding
, it accepts 8 bit chars.Better, use the NSData pointer without conversion, see: @KennyTM.