iOS: [NSString hash]出现同样的hash值问题
问题原因:
At least there are special circumstances for which this unreliability kicks in.
Comparing [a hash] and [b hash] of two different NSString is safe when:
- the strings‘ length is shorter or equal to 96 characters.
- [a length] is different to [b length].
- the concatinated first, middle, and last 32 characters of a differ to the concatinated components of b.
Otherwise every difference between the first and the middle 32 chars, as well as every difference between the middle and the last 32 characters, are not used while producing the [NSString hash] value.
解决方法:
For my case, the solution was pretty easy. I made use of a sha1 hash, which produced
a fingerprint from the whole string instead of pieces only. I got mine inspired by
the SHA1 method of Saurabh Sharma
(NSString *)sha1 { NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding]; uint8t digest[CCSHA1DIGESTLENGTH];
CC_SHA1(data.bytes, data.length, digest);
NSMutableString *output = [NSMutableString stringWithCapacity:CCSHA1DIGEST_LENGTH * 2];
for (int i = 0; i < CCSHA1DIGEST_LENGTH; i++) { [output appendFormat:@"%02x", digest[i]]; }
return output; }
The SHA1 was for the name of a cached file, generated by giving the complete URL of the resource to be cached. Works perfect. I already filed a pull request for
http://www.makebetterthings.com/iphone/how-to-get-md5-and-sha1-in-objective-c-ios-sdk/
How to get md5 and SHA1 in objective c (iOS sdk)
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。