《java入门第一季》之对文件和字符串进行MD5加密工具类

时间:2022-02-13 04:20:49

上一篇介绍了MD5加密算法,之前写的代码有些冗余,而且可读性很差。今天把对文本数据的加密,以及获取文件的md5值做一个封装类。代码如下:

package com.itydl.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; public class Md5Utils { /**
* @param path
* 文件的路径
* @return
* 文件的MD5值
*/
public static String getFileMD5(String path){
StringBuilder mess = new StringBuilder();
try {
FileInputStream fis = new FileInputStream(new File(path));
//获取MD5加密器
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] buffer = new byte[10240];
int len = fis.read(buffer);
while (len != -1) {
md.update(buffer, 0, len);
//继续读取
len = fis.read(buffer);
}
//读完文件,获取文件的md5值,完成获取MD5值。获取的值存储在返回的数组中
byte[] digest = md.digest();
for (byte b : digest){
//把每个字节转成16进制数
int d = b & 0xff;// 0x000000ff
String hexString = Integer.toHexString(d);
if (hexString.length() == 1) {//字节的高4位为0
hexString = "0" + hexString;
}
hexString = hexString.toUpperCase();
mess.append(hexString);//把每个字节对应的2位十六进制数当成字符串拼接一起 }
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return mess + "";
}
public static String md5(String str){
StringBuilder mess = new StringBuilder();
try {
//获取MD5加密器
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] bytes = str.getBytes();
byte[] digest = md.digest(bytes); for (byte b : digest){
//把每个字节转成16进制数
int d = b & 0xff;// 0x000000ff
String hexString = Integer.toHexString(d);
if (hexString.length() == 1) {//字节的高4位为0
hexString = "0" + hexString;
}
mess.append(hexString);//把每个字节对应的2位十六进制数当成字符串拼接一起 }
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return mess + "";
}
}