MessageDigest MD5算法未返回我期望的结果 [英] MessageDigest MD5 Algorithm not returning what I expect

查看:138
本文介绍了MessageDigest MD5算法未返回我期望的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我脑海中有些东西告诉我,我在这里遗漏了一些明显的东西.

我正在将现有的Java项目与第三方api集成在一起,后者使用api密钥的md5哈希进行身份验证.它对我不起作用,在调试期间,我意识到我生成的哈希值与它们提供的示例不匹配.我发现一些网站从字符串创建MD5哈希值来检查其示例,据我所知我是错的,他们是正确的.

例如,根据此网站,字符串"hello"会生成"5d41402abc4b2a76b9719d911017c592". (FWIW,我对这个网站一无所知,只不过它似乎正确地散列了我所举的例子).当我通过代码运行它时,我得到:

XUFAKrxLKna5cZ2REBfFkg ==

这是我用来生成md5哈希/字符串的简单方法.

private String md5(String md5Me) throws Exception {
    MessageDigest md = MessageDigest.getInstance("MD5");
    md.reset();
    md.update(md5Me.getBytes("UTF-8"));

    return Base64.encodeBase64String(md.digest());
}

上周,我使用非常相似的方法使用SHA1算法成功验证了不同的API.我想知道问题是否与org.apache.commons.net.util.Base64.encodeBase64String ...有关.如果仅通过一些测试来查看byteArray是否正确但转换后的字符串错误,对您的帮助将不胜感激. .

解决方案

例如,根据此网站,字符串"hello"生成的哈希为"5d41402abc4b2a76b9719d911017c592". (FWIW,我对这个网站一无所知,只不过它似乎正确地散列了我的例子).当我通过代码运行它时,我得到:

XUFAKrxLKna5cZ2REBfFkg ==

这都是表示相同的16字节哈希的正确方法. 5d41402abc4b2a76b9719d911017c592将哈希的每个字节表示为两个十六进制数字,而XUFAKrxLKna5cZ2REBfFkg==使用Base-64将哈希的每个三个字节表示为四个字符.

要生成此第三方API期望的十六进制版本,您可以更改以下内容:

Base64.encodeBase64String(md.digest());

对此:

String.format("%032x", new BigInteger(1, md.digest()));

(主要摘自此StackOverflow答案).

但是,您可能需要考虑为此使用外部库.在上面的评论中, Perception 提到了Apache Commons DigestUtils.如果您使用它,则需要this website, the string "hello" generates a hash of "5d41402abc4b2a76b9719d911017c592". (FWIW I don't know anything about this website except that it seems to correctly hash the examples that I have). When I run it through my code I get:

XUFAKrxLKna5cZ2REBfFkg==

Here is the simple method I'm using to generate the md5 hash/string.:

private String md5(String md5Me) throws Exception {
    MessageDigest md = MessageDigest.getInstance("MD5");
    md.reset();
    md.update(md5Me.getBytes("UTF-8"));

    return Base64.encodeBase64String(md.digest());
}

I used a very similar method to successfully authenticate a different API using the SHA1 algorithm last week. I'm wondering if the problem is related to the org.apache.commons.net.util.Base64.encodeBase64String... Any help is greatly appreciated, if only some tests to see if the byteArray is correct but the converted string is wrong.

解决方案

for example, according to this website, the string "hello" generates a hash of "5d41402abc4b2a76b9719d911017c592". (FWIW I don't know anything about this website except that it seems to correctly hash the examples that I have). When I run it through my code I get:

XUFAKrxLKna5cZ2REBfFkg==

Both are correct ways of representing the same sixteen-byte hash. 5d41402abc4b2a76b9719d911017c592 represents each byte of the hash as two hexadecimal digits, whereas XUFAKrxLKna5cZ2REBfFkg== uses Base-64 to represent every three bytes of the hash as four characters.

To generate the hexadecimal-version that this third-party API is expecting, you can change this:

Base64.encodeBase64String(md.digest());

to this:

String.format("%032x", new BigInteger(1, md.digest()));

(mostly taken from this StackOverflow answer).

However, you might want to consider using an external library for this. Perception, in a comment above, mentions Apache Commons DigestUtils. If you use that, you'll want the md5hex method.

这篇关于MessageDigest MD5算法未返回我期望的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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