Good file integrity strategy

I have a code that downloads plist from a web server and saves it in the phone's document directory. My concern is that if the file becomes damaged, it will affect the stability and user interface of the application.

I protect coding in parts of reading application data, but I wonder what advice is there to check the integrity of the file first of all before the old one is written on top. I am thinking of implementing some sort of computed value, which is also stored as a key in plist, for example.

Any thoughts on making this as reliable as possible would be greatly appreciated.

Best wishes

Dave

+6
iphone md5
source share
1 answer

Take a look at CommonCrypto/CommonDigest.h .

Function CC_MD5(const void *data, CC_LONG len, unsigned char *md); computes an MD5 hash.

 @implementation NSData (MD5) -(NSString*)md5 { unsigned char digest[CC_MD5_DIGEST_LENGTH]; CC_MD5([self bytes], [self length], digest); NSString* s = [NSString stringWithFormat: @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", digest[0], digest[1], digest[2], digest[3], digest[4], digest[5], digest[6], digest[7], digest[8], digest[9], digest[10], digest[11], digest[12], digest[13], digest[14], digest[15]]; return s; } @end 

As part of deploying files to a server, you can use OpenSSL to calculate hashes. The openssl md5 filename command calculates the MD5 hash for the file. This can be integrated into the script.

Then, after your application has uploaded the file, it calculates the hash of the downloaded one and compares it with the hash stored on the server.

Obviously, if you want to ensure the integrity of the plist file, this plist cannot contain its own hash.

+5
source share

All Articles