0

Im using the following method to convert text to MD5 format.

- (NSString*)MD5
{
        const char *ptr = [txt_Password.text UTF8String];
        unsigned char md5Buffer[CC_MD5_DIGEST_LENGTH];
        CC_MD5(ptr, strlen(ptr), md5Buffer);
        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;
}

However, it returns me the MD5 string in 16 bytes. And i need it in 64 bytes. Any help is appreciated. Thank you!!!

Rushi
  • 4,553
  • 4
  • 33
  • 46
  • 2
    I think what you actually need is Base64Encode(MD5(yourString)) ? Or am i wrong ? – Vlad Feb 07 '12 at 12:09
  • possible duplicate of [MD5 algorithm in Objective C](http://stackoverflow.com/questions/1524604/md5-algorithm-in-objective-c) – trojanfoe Feb 07 '12 at 12:10
  • @Vlad: Can u tell me how do i do it exactly? Thank you. – Rushi Feb 07 '12 at 12:26
  • yeah, i'll add a response, i dont have enough characters here. – Vlad Feb 07 '12 at 12:28
  • just notice you're doing the md5 wrong too. I'll edit my post and add that as well. – Vlad Feb 07 '12 at 12:50
  • 2
    I think the answer needs to be updated, here it is a really nice piece code that do the magic: http://iphonedevelopertips.com/core-services/create-md5-hash-from-nsstring-nsdata-or-file.html – Jerry Tian Feb 14 '12 at 16:44

1 Answers1

0

For md5: (Add this on a cathegory over NSString as well)

+ (NSString *)hashForString:(NSString *)aString {
    NSData *data = [aString dataUsingEncoding:NSUTF8StringEncoding];
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    CC_MD5([data bytes], [data length], digest);
    NSString *md5String = [[NSString alloc] initWithBytes:digest length:CC_MD5_DIGEST_LENGTH encoding:NSUTF8StringEncoding];
    return [md5String autorelease];
}

For encoding your hashed psw to 64bit format:

    - (NSString*)base64MD5HashForString:(NSString *)string {
        NSString *md5Hash = [[[NSString hashForString:string] dataUsingEncoding:NSUTF8StringEncoding] encodeBase64];
return md5Hash;
    }

Method below is a cathegory over NSData;

static const char kBase64Alphabet[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                                        "abcdefghijklmnopqrstuvwxyz"
                                        "0123456789+/";

- (NSString*)encodeBase64 {
    NSMutableString *encodedData = [NSMutableString string];
    int i = 0, j = 0;
    unsigned char char_array_3[3];
    unsigned char char_array_4[5];

    memset(char_array_3, 0, 3*sizeof(char));
    memset(char_array_4, 0, 5*sizeof(char));

    int length = [self length]; 
    char *bytes = (char*)[self bytes];

    while(length--) {
        char_array_3[i++] = *(bytes++);
        if (i == 3) {
            char_array_4[0] = kBase64Alphabet[(char_array_3[0] & 0xfc)>>2];
            char_array_4[1] = kBase64Alphabet[((char_array_3[0] & 0x03) <<4) + ((char_array_3[1] & 0xf0) >>4)];
            char_array_4[2] = kBase64Alphabet[((char_array_3[1] & 0x0f) <<2) + ((char_array_3[2] & 0xc0) >>6)];
            char_array_4[3] = kBase64Alphabet[char_array_3[2]&0x3f];

            [encodedData appendString:[NSString stringWithUTF8String:(const char*)char_array_4]];

            i = 0;
        }
    }

    if (i) {
        for(j=i; j<3; j++)
            char_array_3[j] = '\0';

        char_array_4[0] = kBase64Alphabet[(char_array_3[0] & 0xfc)>>2];
        char_array_4[1] = kBase64Alphabet[((char_array_3[0] & 0x03) <<4) + ((char_array_3[1] & 0xf0) >>4)];
        char_array_4[2] = kBase64Alphabet[((char_array_3[1] & 0x0f) <<2) + ((char_array_3[2] & 0xc0) >>6)];
        char_array_4[3] = kBase64Alphabet[char_array_3[2]&0x3f];

        char_array_4[i+1] = 0;
        [encodedData appendString:[NSString stringWithUTF8String:(const char*)char_array_4]];

        while((i++<3))
            [encodedData appendString:[NSString stringWithUTF8String:"="]];
    }

    return encodedData;
}
Vlad
  • 3,346
  • 2
  • 27
  • 39
  • Thank you for the help. Can u please tell me what is "memset(char_array_3, 0, 3*sizeof(char));" and why it is used? – Rushi Feb 07 '12 at 12:48
  • it's just an initializer for the pointer. You can replace it with malloc() if you think it's more clear that way. What it does is it reserves in memory 3 * 1 byte locations for the array, respectivelly 5 * 1 byte for the other one. (char size is 1 byte) – Vlad Feb 07 '12 at 12:59
  • Ok. And this int length = [self length]; char *bytes = (char*)[self bytes]; two lines? – Rushi Feb 07 '12 at 13:01
  • int length = [self length] - length of data; [self bytes] - converts the data into a byte array. – Vlad Feb 07 '12 at 13:03
  • i just noticed a mistake: - (NSString*)encodeBase64 should be a cathegory over NSData, not NSString. My bad :) edited my post – Vlad Feb 07 '12 at 13:05
  • Do i have to write the function for this? – Rushi Feb 07 '12 at 13:05
  • nah, just make a normal cathegory over NSData and it will work – Vlad Feb 07 '12 at 13:06