package com.fdkankan.common.util;

import java.io.File;
import java.io.FileInputStream;
import java.security.MessageDigest;

public class FileMd5Util {

    public static final String KEY_MD5 = "MD5";
    public static final String CHARSET_ISO88591 = "ISO-8859-1";

    /**
     * Get MD5 of one file:hex string,test OK!
     *
     * @param file
     * @return
     */
    public static String getFileMD5(File file) {
        if (!file.exists() || !file.isFile()) {
            return null;
        }
        MessageDigest digest = null;
        FileInputStream in = null;
        byte buffer[] = new byte[1024];
        int len;
        try {
            digest = MessageDigest.getInstance("MD5");
            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;
        }
        byte by[] = digest.digest();
        int i;
        StringBuffer sbf = new StringBuffer();
        for (int j = 0; j < by.length; j++) {
            i = by[j];
            if (i < 0) {
                i += 256;
            } else if (i < 16) {
                sbf.append("0");    //因为大于16的有两位，因此小于16需要补位，
            }
            sbf.append(Integer.toHexString(i));

        }

        return sbf.toString();
//        BigInteger bigInt = new BigInteger(1, digest.digest());
//        return bigInt.toString(16);
    }

    /***
     * Get MD5 of one file！test ok!
     *
     * @param filepath
     * @return
     */
    public static String getFileMD5(String filepath) {
        File file = new File(filepath);
        return getFileMD5(file);
    }

    /**
     * MD5 encrypt,test ok
     *
     * @param data
     * @return byte[]
     * @throws Exception
     */
    public static byte[] encryptMD5(byte[] data) throws Exception {

        MessageDigest md5 = MessageDigest.getInstance(KEY_MD5);
        md5.update(data);
        return md5.digest();
    }

    public static byte[] encryptMD5(String data) throws Exception {
        return encryptMD5(data.getBytes(CHARSET_ISO88591));
    }

    /***
     * compare two file by Md5
     *
     * @param file1
     * @param file2
     * @return
     */
    public static boolean isSameMd5(File file1, File file2) {
        String md5_1 = FileMd5Util.getFileMD5(file1);
        String md5_2 = FileMd5Util.getFileMD5(file2);
        return md5_1.equals(md5_2);
    }

    /***
     * compare two file by Md5
     *
     * @param filepath1
     * @param filepath2
     * @return
     */
    public static boolean isSameMd5(String filepath1, String filepath2) {
        File file1 = new File(filepath1);
        File file2 = new File(filepath2);
        return isSameMd5(file1, file2);
    }

    public static void main(String[] args){
//        String path = "F:\\桌面\\";
//
//        StringBuffer sb = new StringBuffer(path + "20190925151119.h264");
//        File dbFile = new File(sb.toString());
//
//        String fileMD5 = FileMd5Util.getFileMD5(dbFile);
//        System.out.println(fileMD5);

//        String path1 = "F:\\文档\\WeChat Files\\Iove-bing\\FileStorage\\File\\2020-05\\1.3.4-update.zip";
//        File dbFile1 = new File(path1);
//
//        String fileMD51 = FileMd5Util.getFileMD5(dbFile1);
//        System.out.println(fileMD51);

//        BigInteger usedSpace = new BigInteger("0");
//        BigInteger space = new BigInteger("560800515");
//        usedSpace.add(space);
//        System.out.println(usedSpace.intValue());
        System.out.println("obj1: " + getFileMD5(new File("F:\\桌面\\c11m-T11-EA\\log\\i6VhiQ2Q-copy.obj")));
        System.out.println("obj: " + getFileMD5(new File("F:\\桌面\\c11m-T11-EA\\log\\i6VhiQ2Q.obj")));
    }
}
