Not sure how to add two MD5 hashes to Objective-c

I am trying to add two MD5 hash values ​​in Objective-c.

I imported the CommonCrypto/CommonDigest.h header file and converted the two lines to an MD5 hash using the CC_MD5 function.

How to add these two hash values?

+4
source share
3 answers

As some people said in a related post, it’s not that MD5hash (part1 + part2) = MD5hash (part1) + MD5hash (part2). Perhaps try this solution: http://www.joel.lopes-da-silva.com/2010/09/07/compute-md5-or-sha-hash-of-large-file-efficiently-on-ios- and-mac-os-x / . I have not tested it, but the code should work for you. You load the subsequent parts of your file into CC_MD5_Update (...) and get the final hash, just call CC_MD5_Final (...). This is probably the only way to handle large files, if that is your goal.

Sorry if I missed something.

+1
source

I created an NSString category for hashing:

 - (NSString *)MD5 { // Create pointer to the string as UTF8 const char *ptr = [self UTF8String]; // Create byte array of unsigned chars unsigned char md5Buffer[CC_MD5_DIGEST_LENGTH]; // Create 16 byte MD5 hash value, store in buffer CC_MD5(ptr, strlen(ptr), md5Buffer); // Convert MD5 value in the buffer to NSString of hex values NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2]; for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) { [output appendFormat:@"%02x",md5Buffer[i]]; } return output; } 

I don’t know why you want to add two hashes together, but this can help you.

0
source

@PiotrK seems to be the most helpful answer. I looked at the code from the link and it looks pretty good (I haven’t tried it either). However, there are two points regarding the code by reference. Firstly, Lopez da Silva (author of PiotrK link) claims that he did not create a local pool of abstracts, as this would be inefficient. This myth has been debunked. Go ahead, create and destroy the pools of abstracts, it’s cheap (although this code is probably faster). Secondly, the associated code declares a “buffer” (which should be reused) inside the while loop; it should be limited to the limits of the while loop. Most likely, the compiler will cope with this by allocating a buffer on the stack stack only once, and not per cycle. But, paradoxically, this can create the very problem that the author hoped to avoid by coding around the autorun pool.

Finally, for OP, I hope that ADD you meant streaming data through the Hash MD5 algorithm and do not intend to invent your own security algorithm. This is usually a bad idea. If you are trying to find something new in the world of security algorithms, I would call you some cryptographic books such as Applied Cryptography by Bruce Schneier. Or just clarify your question above with what you are trying to accomplish.

0
source

Source: https://habr.com/ru/post/1386263/


All Articles