import java.io.*;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.*;
import java.util.zip.*;
/**
* 此算法主要是针对文件或者字符串进行加密的算法,可以获取文件或者字符串的MD5值或sha1,SHA-256, SHA-384, SHA-512 的加密值
* 整理了一些网上的代码留着备用
* @author 张永志 1279938986@qq.com 相关密码的加密解密查询可以参考 https://www.cmd5.com/
* 参考文章:https://blog.youkuaiyun.com/lichao_ustc/article/details/30457273
* 参考文章:https://blog.youkuaiyun.com/plean/article/details/40187347
*
*/
public class DigestUtil {
/**
*
* @param file
* @param algorithm
* 所请求算法的名称 for example: MD5, SHA1, SHA-256, SHA-384, SHA-512
* etc.
* @return
*/
public static String getFileMD5(File file, String algorithm) {
if (!file.isFile()) {
return null;
}
MessageDigest digest = null;
FileInputStream in = null;
byte buffer[] = new byte[1024];
int len;
try {
digest = MessageDigest.getInstance(algorithm);
in = new FileInputStream(file);
while ((len = in.read(buffer, 0, 1024)) != -1) {
digest.update(buffer, 0, len);
}
in.close();
} catch (Exception e) {
e.printStackTrace();
return null;
}
BigInteger bigInt = new BigInteger(1, digest.digest());
return bigInt.toString(16);
}
/**
* 获取文件夹中文件的MD5值
*
* @param dirFile
* @param algorithm
* 所请求算法的名称 for example: MD5, SHA1, SHA-256, SHA-384, SHA-512
* etc.
* @param listChild
* 是否递归子目录中的文件
* @return
*/
public static Map<String, String> getDirMD5(File dirFile, String algorithm,
boolean listChild) {
if (!dirFile.isDirectory()) {
return null;
}
// <filepath,algCode>
Map<String, String> pathAlgMap = new HashMap<String, String>();
String algCode;
File files[] = dirFile.listFiles();
for (int i = 0; i < files.length; i++) {
File file = files[i];
if (file.isDirectory() && listChild) {
pathAlgMap.putAll(getDirMD5(file, algorithm, listChild));
} else {
algCode = getFileMD5(file, algorithm);
if (algCode != null) {
pathAlgMap.put(file.getPath(), algCode);
}
}
}
return pathAlgMap;
}
public static String getCRC32(String string) {
CRC32 crc32 = new CRC32();
crc32.update(string.getBytes());
return Long.toHexString(crc32.getValue()).toUpperCase();
}
/**
*
* @param s
* 需要加密的字符串
* @param algorithm
* 所请求算法的名称 for example: MD5, SHA1, SHA-256, SHA-384, SHA-512
* etc.
* @return 返回加密后的字符串
*/
public static String getStringMd5(String s, String algorithm) {
try {
// 返回实现指定摘要算法的 MessageDigest对象 参数可以是sha或者MD5
MessageDigest md = MessageDigest.getInstance(algorithm);
// 使用指定的 byte 数组对摘要进行最后更新,然后完成摘要计算。
byte[] bytes = md.digest(s.getBytes("utf-8"));
return toHex(bytes);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private static String toHex(byte[] bytes) {
// 个人在设计秘钥时可以更改字符串“0123456789ABCDEF”
final char[] HEX_DIGITS = "0123456789ABCDEF".toCharArray();
StringBuilder ret = new StringBuilder(bytes.length * 2);
for (int i = 0; i < bytes.length; i++) {
// 取byte数组元素的向右移动4位后与0x0f 即将元素的高四位变为第四位作为下标取HEX_DIGITS的元素
ret.append(HEX_DIGITS[(bytes[i] >> 4) & 0x0f]);
// 取byte数组元素后与0x0f 即将元素的底四位作为下标取HEX_DIGITS的元素
ret.append(HEX_DIGITS[bytes[i] & 0x0f]);
}
return ret.toString();
}
/**
* 获取文件的 CRC32校验码
*
* @param fileUri
* @return
*/
public static String getCRC32(File file) {
CRC32 crc32 = new CRC32();
// MessageDigest.get
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(file);
byte[] buffer = new byte[8192];
int length;
while ((length = fileInputStream.read(buffer)) != -1) {
crc32.update(buffer, 0, length);
}
return Long.toHexString(crc32.getValue()).toLowerCase();
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
try {
if (fileInputStream != null)
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}