Cause of the problem:
At least there is special circumstances for which this unreliability kicks in.
Comparing [a hash] and [b hash] of different nsstring is safe when:
- The strings ' length is shorter or equal to characters.
- [a length] is different to [b length].
- The concatinated first, middle, and last from characters of a differ to the concatinated components of b.
Otherwise every difference between the first and the middle chars, as well as every difference between the middle and t He last characters, is not used while producing the [NSString hash] value.
Workaround:
For my case, the solution is pretty easy. I made use of a SHA1 hash, which produced
A fingerprint from the whole string instead of pieces. I got mine inspired by
The SHA1 method of Saurabh Sharma
LANG:OBJC SHA1
(NSString *) SHA1 {NSData *data = [Self dat Ausingencoding:nsutf8stringencoding]; Uint8 t digest[cc SHA1 Digest LENGTH];
cc_sha1 (Data.bytes, Data.length, Digest);
nsmutablestring *output = [nsmutablestring stringwithcapacity:cc SHA1 digest_length * 2];
for (int i = 0; i < CC SHA1 digest_length; i++) {[Output appendformat:@ '%02x ', Digest[i]];}
return output;}
The SHA1 is for the name of a cached file, and 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/
Ps:
How to get MD5 and SHA1 in Objective C (IOS SDK)
Calculating the MD5 and SHA1 hash in IOS SDK are pretty simple-
Step 1 , haven very first thing you need to does is import Commoncrypto ' s CommonDigest.h
#import <CommonCrypto/CommonDigest.h>
Step 2 – Here is the real code for calculating SHA1 and MD5 hash-
SHA1-
-(nsstring*) SHA1: (nsstring*) input{const char *CSTR = [input cstringusingencoding:nsutf8stringencoding]; NSData *data = [NSData datawithbytes:cstr length:input.length]; uint8_t Digest[cc_sha1_digest_length]; CC_SHA1 (Data.bytes, Data.length, Digest); nsmutablestring* output = [nsmutablestring stringwithcapacity:cc_sha1_digest_length * 2]; for (int i = 0; i < cc_sha1_digest_length; i++) [Output appendformat:@ "%02x", Digest[i]]; return output;}
MD5-
-(NSString *) MD5: (NSString *) input{const char *CSTR = [input utf8string]; unsigned char digest[16]; CC_MD5 (CStr, strlen (CSTR), Digest); The MD5 call nsmutablestring *output = [nsmutablestring stringwithcapacity:cc_md5_digest_length * 2]; for (int i = 0; i < cc_md5_digest_length; i++) [Output appendformat:@ "%02x", Digest[i]]; return output;}
Hope it would help someone!!
IOS: [NSString Hash] appears with the same hash value problem