MD5哈希大小写问题 [英] MD5 hashing casing issue

查看:308
本文介绍了MD5哈希大小写问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,我的方法返回的MD5字符串全部为大写字母.除了使用[string lowerCaseString]使方法的输出返回小写字符串之外,还有其他方法吗?上面的方法对我来说似乎有点黑.

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

推荐答案

为什么入侵? MD5结果中的区分大小写是没有意义的,因为实际上它不是返回的字符串,而是几个十六进制数字.适当的修改是使您的比较方法(假设您稍后要比较散列)忽略这种情况,因为这无关紧要.

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

这篇关于MD5哈希大小写问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆