Get checksum of md5 / sha1 file

I found two functions for counting md5 and sha 1 in Objective C. Here's the code:

-(void)md5HexDigest:(NSString*)input { NSData *data = [input dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; uint8_t digest[CC_MD5_DIGEST_LENGTH]; CC_MD5(data.bytes, data.length, digest); NSMutableString* ret = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2]; for(int i = 0; i<CC_MD5_DIGEST_LENGTH; i++) { [ret appendFormat:@"%02x",digest[i]]; } NSLog (@"%@",ret); } -(void) SHA1digest:(NSString*)input{ NSData *data = [input dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 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]]; NSLog (@"%@", output); } 

I get these checksums:

 2010-11-04 20:38:01.962 MD5 Counter[88118:a0f] c8142be71e8ed4625c4f27eb573835f5 2010-11-04 20:38:01.964 MD5 Counter[88118:a0f] ba7ff5f68edef52dd89a92c075b88f247f3ef9aa 

However, the real amounts are: SHA1: 1c0d5ea45464e336fcb38c644dc125c3a16b5493

MD5: e8f4d590c8fe62386844d6a2248ae609

Where is the mistake? Help me please!

+4
source share
2 answers

You must use the CommonCrypto C API. The functions are described in the 3CC man pages section. In particular, the CC_md5 and CC_sha1 family of functions will be of interest to you.

+2
source

I'm not sure what limitations exist for the Mac AppStore, but you can invoke the md5 command. It is installed by default in OSX and calculates the MD5 checksum specified as an argument.

0
source

All Articles