Search code examples
iosobjective-chashtheos

Binary hash representation to HEX/Ascii in Objective-c


I would to log a binary hash representation in the console, using an hex or ascii representation. The algorithm is MD5, so the function is CC_MD5

I get the binary hash representation via a Theos tweak, which is working well.

EDIT: this tweak intercept the CC_MD5 call. The call is implemented in the method described below. When CC_MD5 is called, replaced_CC_MD5 intercept the call.

The app tested, is a simple app which i made myself and it's using this method to calculate MD5 Hash:

- (NSString *) md5:(NSString *) input
{
    const char *cStr = [input UTF8String];
    unsigned char digest[16];
    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;

}

The hashing it's ok, and the app returns to me the correct hash for the input

input = prova
MD5 Digest = 189bbbb00c5f1fb7fba9ad9285f193d1

The function in my Theos Tweak where i manipulate the CC_MD5 function is

EDIT: where data would be cStr, len would be strlen(cStr) and md would be digest.

static unsigned char * replaced_CC_MD5(const void *data, CC_LONG len, unsigned char *md) {
    CC_LONG dataLength = (size_t) len;
    NSLog(@"==== START CC_MD5 HOOK ====");

    // hex of digest
    NSData *dataDigest = [NSData dataWithBytes:(const void *)md length:(NSUInteger)CC_MD5_DIGEST_LENGTH];
    NSLog(@"%@", dataDigest);

    // hex of string
    NSData *dataString = [NSData dataWithBytes:(const void *)data length:(NSUInteger)dataLength];
    NSLog(@"%@", dataString);

    NSLog(@"==== END CC_MD5 HOOK ====");

    return original_CC_MD5(data, len, md);

}

The log of dataString it's ok: 70726f76 61 which is the HEX representation of prova The log of dataDigest is e9aa0800 01000000 b8c00800 01000000 which is, if i understood, the binary hash representation.

How can i convert this representation to have the MD5 Hash digest?


Solution

  • In replaced_CC_MD5 you are displaying md before the call to original_CC_MD5 which sets its value. What you are seeing is therefore random data (or whatever was last stored in md).

    Move the call to original_CC_MD5 to before the display statement and you should see the value you expect. (You'll of course need to save the result of the call in a local so you can return the value in the return statement.)