English 中文(简体)
MD5 没有为 iOS 的在线文件给出正确的 hash
原标题:MD5 not giving correct hash for online file in iOS

我真的不知道这怎么可能 但我正在检查两个文件 以备他们的MD5杂货 所以我可以确定它们是否相同

问题在于本地文件( 保存在软件包中的文件) 给出了正确的值, 而存储在在线服务器上的文件则给出了错误的值 。 但是, 如果我在 Mac 终端上检查在线文件的 md5, 或者通过某种互联网工具, 它给出了正确的值 。

这怎么可能?

我基本上就是这么做的

NSData *currencyData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.iworldtip.com/appl/texts/files/CountryCurrency.plist"]];
NSData *localCurrencyData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"CountryCurrency" ofType:@"plist"]];

NSString *currencyDataHashValue = [currencyData md5];
NSString *localCurrencyDataHashValue = [localCurrencyData md5];

if (![currencyDataHashValue isEqual:localCurrencyDataHashValue]) {
            NSLog(@"Saving new version of currency plist

%@ - %@", currencyDataHashValue, localCurrencyDataHashValue);
...
...

以下是输出 :

""https://i.sstatic.net/wVagt.png" alt="此处输入图像描述"/ >


这是我正在使用的 md5 扩展名( 在网络上找到) :

.h 文件 :

@interface NSString (MyExtensions)
- (NSString *) md5;
@end

@interface NSData (MyExtensions)
- (NSString*)md5;
@end

.m 文件 :

#import "md5Extension.h"
#import <CommonCrypto/CommonDigest.h>

@implementation NSString (MyExtensions)
- (NSString *) md5
{
    const char *cStr = [self UTF8String];
    unsigned char result[16];
    CC_MD5( cStr, strlen(cStr), result ); // This is the md5 call
    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]
            ];  
}
@end

@implementation NSData (MyExtensions)
- (NSString*)md5
{
    unsigned char result[16];
    CC_MD5( self.bytes, self.length, result ); // This is the md5 call
    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]
            ];  
}
@end
最佳回答

所以,我发现哪里出了问题。

首先,我是哑巴。我在看输出,我以为问题在于远程文件,但当地文件给出了错误的散列!

接下来的是,这是由转换造成的, Xcode 在编译工程时会自动进行转换 - 它会将 XML 列表文件转换为二进制列表文件。 因此它有一个不同的 MD5 散列 。

所以最终的解决方案是把这些远程文件转换成二进制列表, 一切都很灵巧!


PS: I found a great tool for editing Plists - it is called PlistEdit Pro. It can do everything you can imagine with the Plists - converts them to binary and vice versa... (I know it can be done in Terminal by plutil, but this is a more convenient solution)

问题回答

暂无回答




相关问题
How to change out-of-focus text selection color in Xcode?

Okay, I ll bite. I ve got really pleasant code/window colors set up in Xcode. Ordinarily, my selection color is very visible. When I am doing a project search and iterating through the results, ...

Iphone NSTimer Issue

Hi I am new to objective c. I am trying to make an app for iphone. I have a button on my view, and the click on which the function playSound is called. This is working properly. It does plays the ...

Include a .txt file in a .h in C++?

I have a number of places where I need to re-use some template code. Many classes need these items In a .h could I do something like: #include <xxx.txt> and place all of this code in the ....

Iterating over string/strlen with umlauted characters

This is a follow-up to my previous question . I succeeded in implementing the algorithm for checking umlauted characters. The next problem comes from iterating over all characters in a string. I do ...

Xcode open two editor windows with same file

Is it possible to open the same file in two separate windows in Xcode. I can open a file in one window and the same file in the main Xcode editor window, but I wanted two separate fulltime editor ...

Forcing code signing refresh in Xcode

In our environment, we share resources across multiple projects and platforms. When building for iPhone, only a subset of those resources are needed. Since that subset is still considerable, we have ...

热门标签