md5加密工具类MD5
程序员文章站
2022-03-14 20:28:56
...
版权声明:本文为Mr.release原创文章,转载请标明出处
封装md5加密工具类 MD5
public class MD5 {
public static String getStringMD5(String value) {
if (value == null || value.trim().length() < 1) {
return null;
}
try {
return getMD5(value.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
public static String getMD5(byte[] source) {
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
return HexDump.toHex(md5.digest(source));
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
public static String getStreamMD5(String filePath) {
String hash = null;
byte[] buffer = new byte[4096];
BufferedInputStream in = null;
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
in = new BufferedInputStream(new FileInputStream(filePath));
int numRead = 0;
while ((numRead = in.read(buffer)) > 0) {
md5.update(buffer, 0, numRead);
}
in.close();
hash = HexDump.toHex(md5.digest());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
return hash;
}
}
上一篇: php单例模式的讲解(代码示例)
下一篇: 简单实现Md5加密的工具类