在Java中是否有任何方法可以生成一个字符串的MD5哈希?
当前回答
使用java.security.MessageDigest库的简单函数
public String stringMD5Hash(String text) throws NoSuchAlgorithmException {
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
byte[] bytes = messageDigest.digest(text.getBytes());
StringJoiner stringJoiner = new StringJoiner("");
for (byte b : bytes) {
stringJoiner.add(Integer.toHexString((b & 0xFF) | 0x100).substring(1, 3));
}
return stringJoiner.toString();
}
对于文件使用这个函数
public static String FileMD5Hash(File file) throws NoSuchAlgorithmException, IOException {
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
byte[] bytes = messageDigest.digest(getFileBytes(file));
StringJoiner stringJoiner = new StringJoiner("");
for (byte b : bytes) {
stringJoiner.add(Integer.toHexString((b & 0xFF) | 0x100).substring(1, 3));
}
return stringJoiner.toString();
}
public static byte[] getFileBytes(File file) throws IOException{
InputStream inputStream = new FileInputStream(file);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(bytes)) != -1) {
byteArrayOutputStream.write(bytes, 0, bytesRead);
}
return byteArrayOutputStream.toByteArray();
}
其他回答
Spring中也有一个DigestUtils类:
http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/util/DigestUtils.html
这个类包含md5DigestAsHex()方法来完成这项工作。
通过使用java中的MessageDigest类中的方法,可以为给定的文本生成MD5散列。安全包。下面是完整的代码片段,
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.xml.bind.DatatypeConverter;
public class MD5HashGenerator
{
public static void main(String args[]) throws NoSuchAlgorithmException
{
String stringToHash = "MyJavaCode";
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
messageDigest.update(stringToHash.getBytes());
byte[] digiest = messageDigest.digest();
String hashedOutput = DatatypeConverter.printHexBinary(digiest);
System.out.println(hashedOutput);
}
}
MD5函数的输出是由32个十六进制数表示的128位哈希。
如果你使用的是像MySQL这样的数据库,你也可以用一种更简单的方式来做到这一点。查询Select MD5(" text here ")将返回括号中文本的MD5哈希值。
Bombe的答案是正确的,但请注意,除非你绝对必须使用MD5(例如,为了互操作性而被迫使用),否则更好的选择是SHA1,因为MD5长期使用有缺点。
我应该补充一点,SHA1也有理论上的漏洞,但没有那么严重。目前的哈希技术水平是,有许多候选替换哈希函数,但还没有一个成为取代SHA1的标准最佳实践。因此,根据您的需要,建议您使您的哈希算法可配置,以便将来可以替换它。
我发现这是最清晰和简洁的方法:
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(StandardCharsets.UTF_8.encode(string));
return String.format("%032x", new BigInteger(1, md5.digest()));
我的回答不是很直白:
private String md5(String s) {
try {
MessageDigest m = MessageDigest.getInstance("MD5");
m.update(s.getBytes(), 0, s.length());
BigInteger i = new BigInteger(1,m.digest());
return String.format("%1$032x", i);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
推荐文章
- 接口方法的最终参数-有什么意义?
- Java中的@UniqueConstraint注释
- 如何在清洁模式下运行eclipse ?如果我们这样做会发生什么?
- 获取java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory异常
- Java中的正则表达式命名组
- c#和Java的主要区别是什么?
- 什么是NullPointerException,我如何修复它?
- 在Java中使用“final”修饰符
- 无法在Flutter上找到捆绑的Java版本
- 如何在Kotlin解析JSON ?
- 如何在新的材质主题中改变背面箭头的颜色?
- 如何设置OkHttp连接超时
- 用于桌面应用程序的Swing vs JavaFx
- 为什么这个Java程序会终止,尽管它显然不应该(也没有)终止?
- 使用split("|")按管道符号拆分Java字符串