如何在 Java 中生成 MD5 哈希? [英] How can I generate an MD5 hash in Java?
本文介绍了如何在 Java 中生成 MD5 哈希?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有什么方法可以在 Java 中生成字符串的 MD5 哈希值?
Is there any method to generate MD5 hash of a string in Java?
推荐答案
您需要 java.security.MessageDigest
.
调用 MessageDigest.getInstance("MD5")
以获得您可以使用的 MessageDigest
的 MD5 实例.
Call MessageDigest.getInstance("MD5")
to get a MD5 instance of MessageDigest
you can use.
通过执行以下操作之一计算散列:
The compute the hash by doing one of:
- 将整个输入作为
byte[]
馈送,并使用md.digest(bytes)
. - 通过调用
md.update(bytes)
.当你完成添加输入字节后,计算散列<代码>md.digest().
- Feed the entire input as a
byte[]
and calculate the hash in one operation withmd.digest(bytes)
. - Feed the
MessageDigest
onebyte[]
chunk at a time by callingmd.update(bytes)
. When you're done adding input bytes, calculate the hash withmd.digest()
.
md.digest()
返回的 byte[]
是 MD5 哈希值.
The byte[]
returned by md.digest()
is the MD5 hash.
这篇关于如何在 Java 中生成 MD5 哈希?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文