zoukankan      html  css  js  c++  java
  • Java 计算文件的MD5值和Hash值

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.nio.file.Files;
    import java.nio.file.StandardOpenOption;
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    
    public class FileMd5HashUtils {
        private static final char[] hexCode = "0123456789abcdef".toCharArray();
    
    
        /**
         * 计算文件的MD5
         * @param fileName 文件的绝对路径
         * @return
         */
        public static String getFileMD5(String fileName) {
            File file = new File(fileName);
            try (InputStream stream = Files.newInputStream(file.toPath(), StandardOpenOption.READ)) {
                MessageDigest digest = MessageDigest.getInstance("MD5");
                byte[] buf = new byte[8192];
                int len;
                while ((len = stream.read(buf)) > 0) {
                    digest.update(buf, 0, len);
                }
                return toHexString(digest.digest());
            } catch (IOException e) {
                e.printStackTrace();
                return "";
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
                return "";
            }
        }
    
        public static String toHexString(byte[] data) {
            StringBuilder r = new StringBuilder(data.length * 2);
            for (byte b : data) {
                r.append(hexCode[(b >> 4) & 0xF]);
                r.append(hexCode[(b & 0xF)]);
            }
            return r.toString();
        }
    
        /**
         * 计算文件的Hash256值
         * @param fileName 文件的绝对路径
         * @return
         */
    
        public static String getFileHash256(String fileName){
            File file = new File(fileName);
            FileInputStream fis = null;
            String sha256 = "";
            try {
                fis = new FileInputStream(file);
                MessageDigest md = MessageDigest.getInstance("SHA-256");
                byte buffer[] = new byte[1024];
                int length = -1;
                while ((length = fis.read(buffer, 0, 1024)) != -1) {
                    md.update(buffer, 0, length);
                }
                byte[] digest = md.digest();
                sha256 = byte2hexLower(digest);
            } catch (Exception e) {
                e.printStackTrace();
                return "";
            } finally {
                try {
                    if (fis != null) {
                        fis.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    return "";
                }
            }
            return sha256;
        }
    
        private static String byte2hexLower(byte[] b) {
            String hs = "";
            String stmp = "";
            for (int i = 0; i < b.length; i++) {
                stmp = Integer.toHexString(b[i] & 0XFF);
                if (stmp.length() == 1) {
                    hs = hs + "0" + stmp;
                } else {
                    hs = hs + stmp;
                }
            }
            return hs;
        }
    
    }
  • 相关阅读:
    linux ---用uniq实现文件的并集和交集
    vim的.vimrc文件设置
    CURL常用命令
    快速入门系列--MVC--03控制器和IOC应用
    linux下git安装
    openerp child_of操作符深度解析
    apache2 + virtualenv +djangocms
    django-cms安装
    pycharm3.4 下svn 项目checkout&配置
    在django项目外,使用django.db链接数据库(postgres)
  • 原文地址:https://www.cnblogs.com/guliang/p/14840097.html
Copyright © 2011-2022 走看看