package com.ai.da.common.utils;

import cn.hutool.core.exceptions.ExceptionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.DigestUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.math.BigInteger;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * MD5加密工具
 */
@Slf4j
public class MD5Utils {

    /**
     * MD5加密
     *
     * @param str
     * @return
     */
    public static String encrypt(String str) {
        byte[] mdBytes = null;
        try {
            mdBytes = MessageDigest.getInstance("MD5").digest(
                    str.getBytes());
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("MD5算法不存在！");
        }
        String mdCode = new BigInteger(1, mdBytes).toString(16);

        if (mdCode.length() < 32) {
            int a = 32 - mdCode.length();
            for (int i = 0; i < a; i++) {
                mdCode = "0" + mdCode;
            }
        }
        // 默认返回32位小写
        return mdCode;
    }

    /**
     * MD5加密文件
     *
     * @param file
     * @return
     */
    public static String encryptFile(MultipartFile file) {
        String md5 = null;
        try {
            md5 = DigestUtils.md5DigestAsHex(file.getInputStream());
        } catch (IOException ioException) {
            log.error("文件MultipartFile md5加密异常ioException##{}", ExceptionUtil.getThrowableList(ioException));
        }
        return md5;
    }

    /**
     * MD5加密文件
     *
     * @param inputStream
     * @return
     */
    public static String encryptFile(FileInputStream inputStream) {
        String md5 = null;
        try {
            md5 = DigestUtils.md5DigestAsHex(inputStream);
        } catch (IOException ioException) {
            log.error("文件MultipartFile md5加密异常ioException##{}", ExceptionUtil.getThrowableList(ioException));
        }
        return md5;
    }

    public static String encryptFile(InputStream inputStream) {
        String md5 = null;
        try {
            md5 = DigestUtils.md5DigestAsHex(inputStream);
        } catch (IOException ioException) {
            log.error("文件InputStream md5加密异常ioException##{}", ExceptionUtil.getThrowableList(ioException));
        }
        return md5;
    }

    public static void main(String[] args) throws FileNotFoundException, IOException {
        File file1 = new File("/Users/yanglei/Documents/阳磊日报2019-04-23.numbers");
        File file2 = new File("/Users/yanglei/Documents/7777.numbers");
        FileInputStream fileInputStream1 = new FileInputStream(file1);
        FileInputStream fileInputStream2 = new FileInputStream(file2);
        String md51 = DigestUtils.md5DigestAsHex(fileInputStream1);
        String md52 = DigestUtils.md5DigestAsHex(fileInputStream2);
        if (md51.equals(md52)) {
            System.out.println(md51);
        }
        System.out.println("md51==" + md51);
        System.out.println("md52==" + md52);

    }


    /**
     * MD5加密文件
     *
     * @param path
     * @return
     */
    public static String encryptFile(String path, Boolean isLocal) {
        String md5 = null;
        try {
            InputStream inputStream = null;
            if (isLocal) {
                File file = new File(path);
                inputStream = new FileInputStream(file);
            } else {
                //远程
                inputStream = FileUtil.getOriginFile(path);
            }
            md5 = DigestUtils.md5DigestAsHex(inputStream);
        } catch (IOException ioException) {
            log.error("文件File md5加密异常ioException##{}", ExceptionUtil.getThrowableList(ioException));
        }
        return md5;
    }

}
