Md5加密工具(附文件MD5值计算方法)
发布日期:2021-06-30 18:39:38
浏览次数:3
分类:技术文章
本文共 2469 字,大约阅读时间需要 8 分钟。
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); } 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 + ""; }}
转载地址:https://linqiarui.blog.csdn.net/article/details/51175064 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
关注你微信了!
[***.104.42.241]2024年04月23日 05时15分01秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
nginx访问控制、基于用户认证、https配置
2019-04-30
用zabbix监控nginx
2019-04-30
rewrite和if语句
2019-04-30
nginx实现负载均衡和动静分离
2019-04-30
SaltStack
2019-04-30
Packer 如何将 JSON 的配置升级为 HCL2
2019-04-30
Ubuntu 安装 NTP 服务
2019-04-30
NeoFetch - Linux 使用命令行查看系统信息
2019-04-30
Jenkins 控制台输出中的奇怪字符
2019-04-30
Linux添加系统调用
2019-04-30
ubuntu 18 CTF 环境搭建
2019-04-30
linux内存的寻址方式
2019-04-30
[off by null + tcache dup]lctf_easy_heap
2019-04-30
[pie+libc]national2021_pwny
2019-04-30
task_struct 结构分析
2019-04-30
Linux创建进程的源码分析
2019-04-30
ubunut16.04的pip3出现问题,重新安装pip3
2019-04-30
how2heap-double free
2019-04-30