如何在 Java 中生成 MD5 哈希? [英] How can I generate an MD5 hash in Java?

查看:30
本文介绍了如何在 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:

  • Feed the entire input as a byte[] and calculate the hash in one operation with md.digest(bytes).
  • Feed the MessageDigest one byte[] chunk at a time by calling md.update(bytes). When you're done adding input bytes, calculate the hash with md.digest().

md.digest() 返回的 byte[] 是 MD5 哈希值.

The byte[] returned by md.digest() is the MD5 hash.

这篇关于如何在 Java 中生成 MD5 哈希?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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