如何在iPhone中将文本转换为MD5 64字节格式

时间:2012-02-07 12:00:36

标签: iphone ios xcode md5

我使用以下方法将文本转换为MD5格式。

- (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;
}

然而,它返回16字节的MD5字符串。我需要64字节。任何帮助表示赞赏。谢谢!!!

1 个答案:

答案 0 :(得分:0)

对于md5 :(在NSString上的导管上添加它)

+ (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];
}

将散列psw编码为64位格式:

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

下面的方法是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;
}