简体   繁体   English

MD5消息摘要Java

[英]MD5 Message Digest Java

I'm trying to convert two strings from an String List into MD5 message digests. 我正在尝试将两个字符串从字符串列表转换为MD5消息摘要。

My String List is called "usernamepassword". 我的字符串列表称为“用户名密码”。

try {
            MessageDigest mdg = MessageDigest.getInstance("MD5");      

            mdg.update(usernamepassword.get(0).getBytes(), 0, usernamepassword.get(0).length());
            mdg.update(usernamepassword.get(1).getBytes(), 1, usernamepassword.get(0).length());     


        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(UPCheck.class.getName()).log(Level.SEVERE, null, ex);
        }

My question is - 我的问题是-

A: Is that the correct way of doing it? 答:那是正确的方法吗? B: How would I return it so I could use each individual MD5 hash in another class? B:如何返回它,以便可以在另一个类中使用每个单独的MD5哈希?

A: Is that the correct way of doing it? 答:那是正确的方法吗?

No, for four reasons: 否,原因有四个:

1) You're using the default character encoding, instead of specifying a particular encoding. 1)您使用的是默认字符编码,而不是指定特定的编码。 I'd recommend using UTF-8. 我建议使用UTF-8。

2) You're currently using the length of the string in characters to specify how many bytes to use 2)您目前正在使用字符串的长度(以字符单位)来指定要使用的字节数

3) If you want separate digests (one per string) you should use separate MessageDigest instance for each one, or call reset between calls 3)如果您想要单独的摘要(每个字符串一个),则应为每个摘要使用单独的MessageDigest实例, 或者MessageDigest调用之间进行调用reset

4) You're not actually doing anything with the digests at the moment. 4)目前,您实际上并未对摘要进行任何操作。

I suggest you extract the "MD5 of a string in a particular encoding" into a separate method: 我建议您将“采用特定编码的字符串的MD5”提取到单独的方法中:

public static byte[] getMd5OfUtf8(String text) {
    try {
        MessageDigest digest = MessageDigest.getInstance("MD5");      
        return digest.digest(text.getBytes("UTF-8"));
    } catch (NoSuchAlgorithmException ex) {
        throw new RuntimeException("No MD5 implementation? Really?");
    } catch (UnsupportedEncodingException ex) {
        throw new RuntimeException("No UTF-8 encoding? Really?");
    }
}

Then you can call it for each of the list elements you're interested in - it's not clear what you're trying to do with the digests afterwards, but you probably want them separately... 然后,您可以为感兴趣的每个列表元素调用它-尚不清楚以后您打算如何处理摘要,但是您可能希望分别将它们...

EDIT: As noted in comments, MD5 really isn't a great hash choice these days. 编辑:正如评论中指出的那样,如今,MD5确实不是一个很好的哈希选择。 Something like SHA-256 with a salt would be better , but for real secure applications you should probably read some modern literature on the topic. 像SHA-256这样的盐会更好 ,但是对于真正的安全应用程序,您可能应该阅读有关该主题的一些现代文献。 (I'm not an expert so don't want to sound too authoritative.) (我不是专家,所以不想听起来过于权威。)

Use the DigestUtils class from the apache commons. 使用来自Apache Commons的DigestUtils类。 Several utility method will help you to compute/display a md5 or some other common hash functions. 几种实用程序方法将帮助您计算/显示md5或其他一些常见的哈希函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM