Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 253 Vote(s) - 3.48 Average
  • 1
  • 2
  • 3
  • 4
  • 5
MD5 algorithm in Objective-C

#1
How to calculate the MD5 in Objective-C?


Reply

#2
You can use the built-in Common Crypto library to do so.
Remember to import:

#import <CommonCrypto/CommonDigest.h>

and then:

- (NSString *) md5:(NSString *) input
{
const char *cStr = [input UTF8String];
unsigned char digest[CC_MD5_DIGEST_LENGTH];
CC_MD5( cStr, strlen(cStr), digest ); // This is 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;
}
Reply

#3
Well since people asked for a file-stream version. I have modified a nice little snippet made by Joel Lopes Da Silva that works with MD5, SHA1 and SHA512 AND it is using streams. Its made for iOS but works with just minimal changes on OSX aswell (remove the ALAssetRepresentation method). It can make checksums for files given a filepath or ALAssets (using ALAssetRepresentation). It's chunking data into small packages making memory impact minimal regardless of the filesize/asset size.

It's currently located on github here:

[To see links please register here]

Reply

#4
If performance is important, you can use this optimized version.
It is about 5 times faster than the ones with `stringWithFormat` or `NSMutableString`.

This is a category of NSString.

- (NSString *)md5
{
const char* cStr = [self UTF8String];
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5(cStr, strlen(cStr), result);

static const char HexEncodeChars[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
char *resultData = malloc(CC_MD5_DIGEST_LENGTH * 2 + 1);

for (uint index = 0; index < CC_MD5_DIGEST_LENGTH; index++) {
resultData[index * 2] = HexEncodeChars[(result[index] >> 4)];
resultData[index * 2 + 1] = HexEncodeChars[(result[index] % 0x10)];
}
resultData[CC_MD5_DIGEST_LENGTH * 2] = 0;

NSString *resultString = [NSString stringWithCString:resultData encoding:NSASCIIStringEncoding];
free(resultData);

return resultString;
}
Reply

#5
Any reason not to use the Apple implementation:

[To see links please register here]


Search for Cryptographic Services Guide on Apple developer site.
Reply

#6
md5 is available on the iPhone and can be added as an addition for ie `NSString` and `NSData` like below.

MyAdditions.h

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

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

MyAdditions.m

#import "MyAdditions.h"
#import <CommonCrypto/CommonDigest.h> // Need to import for CC_MD5 access

@implementation NSString (MyAdditions)
- (NSString *)md5
{
const char *cStr = [self UTF8String];
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5( cStr, (int)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 (MyAdditions)
- (NSString*)md5
{
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5( self.bytes, (int)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

##EDIT
Added NSData md5 because I needed it myself and thought this is a good place to save this little snippet...

These methods are verified using the NIST MD5 test vectors in

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through