MD5 hashing casing issue

喜欢而已 提交于 2020-01-06 02:28:11

问题


I've got a problem where the MD5 string that my method returns has all uppercase letters. Is there a way other than using [string lowerCaseString] to make the output of the method return a lowercase string? The above method seems hackish to me.

Here is the method that I'm using:

- (NSString *)MD5String {
const char *cstr = [self UTF8String];
unsigned char result[16];
CC_MD5(cstr, strlen(cstr), result);

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

回答1:


Why hackish? The case distinction in an MD5 result makes no sense because in truth it's not a string it is returning but a couple of hexadecimal digits. The proper amend would be to make your comparison method (assuming you're comparing hashes later) ignore the case of it, as it should not matter.

In any case, you can make it lowercase by changing the format specifiers to lowercase:

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]
        ];


来源:https://stackoverflow.com/questions/24083909/md5-hashing-casing-issue

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!