在Java中使用MessageDigest类进行MD5, SHA1, SHA-256等加密
温馨提示:
本文最后更新于 2022年08月23日,已超过 604 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我。
/**
* @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);
}
正文到此结束
- 本文标签: Java 加密
- 本文链接: http://www.92cxy.cn/article/12
- 版权声明: 本文由王小东原创发布,转载请遵循《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权
热门推荐
相关文章
该篇文章的评论功能已被站长关闭