Java生成文件hash值

本文介绍了一种使用Java实现文件的SHA-256和MD5哈希值的方法。提供了通过File或InputStream输入来计算文件的哈希值的实用工具类。此方法适用于校验文件完整性和一致性。

Java生成文件hash值(通过传入file或者InputStream)


package com.hczy.syncdata.common.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.security.MessageDigest;

public class FileHahUtil {

	 /**
     * 计算文件hash值
     */
    public static String hashFile(File file) throws Exception {
        FileInputStream fis = null;
        String sha256 = null;
        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();
            throw new Exception("计算文件hash值错误");
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        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;
    }
    
    public static String md5HashCode(InputStream fis) {  
        try {  
        	//拿到一个MD5转换器,如果想使用SHA-1或SHA-256,则传入SHA-1,SHA-256  
            MessageDigest md = MessageDigest.getInstance("MD5"); 
            
            //分多次将一个文件读入,对于大型文件而言,比较推荐这种方式,占用内存比较少。
            byte[] buffer = new byte[1024];  
            int length = -1;  
            while ((length = fis.read(buffer, 0, 1024)) != -1) {  
                md.update(buffer, 0, length);  
            }  
            fis.close();
            //转换并返回包含16个元素字节数组,返回数值范围为-128到127
  			byte[] md5Bytes  = md.digest();
            BigInteger bigInt = new BigInteger(1, md5Bytes);//1代表绝对值 
            return bigInt.toString(16);//转换为16进制
        } catch (Exception e) {  
            e.printStackTrace();  
            return "";  
        }  
    } 
    
    
    public static void main(String[] args) {
		
    	try {
    		File file = new File("D:\\file\\practice\\Downloads.rar");
    		File file1 = new File("D:\\file\\practice\\into\\Downloads.rar");
    		File file2 = new File("D:\\file\\practice\\into\\Downloads(1).rar");
//    		System.out.println(md5HashCode(new FileInputStream(file)));
//    		System.out.println(md5HashCode(new FileInputStream(file)).equals(md5HashCode(new FileInputStream(file))) );
//    		System.out.println(hashFile(file));
//			System.out.println(hashFile(file).equals(hashFile(file1)));
//			System.out.println(hashFile(file).equals(hashFile(file2)));
			File file3 = new File("D:\\file\\practice\\into\\Google.rar");
			String md5HashCode = FileHahUtil.md5HashCode(new FileInputStream(file));
			System.out.println(md5HashCode);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}




知是行之始,行是知之成

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值