package link.at17.mid.tushare.system.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.apache.commons.lang3.StringUtils;
import org.bouncycastle.crypto.digests.SHA3Digest;
import org.bouncycastle.util.encoders.Hex;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class EncryptUtils {

  
    private static final String EMPTY_SHA3_224 = sha3("", 224);
    
    /**
     * 判断密码是否空字符串，或经过 SHA_224 加密过的空字符串
     * @param password
     * @return
     */
    public static boolean passwordIsNotEmpty(String password) {
        return StringUtils.isNotEmpty(password) && !password.equalsIgnoreCase(EMPTY_SHA3_224);
    }
    

    /**
     * 字符串转 32 位小写 md5 字符串
     * 
     * @param str
     * @return
     */
    public static String toMD5String(String str) {
        if (StringUtils.isEmpty(str)) {
            return null;
        }
        try {
            return toMD5String(str.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e10) {
            e10.printStackTrace();
            return null;
        }
    }

    /**
     * 字节数组转 32 位小写 md5 字符串
     * @param bytes
     * @return
     */
    public static String toMD5String(byte[] bytes) {
        if (bytes == null) {
            return null;
        }
        try {
            MessageDigest instance = MessageDigest.getInstance("MD5");
            instance.update(bytes);
            return bytesToHex(instance.digest());
        } catch (NoSuchAlgorithmException e10) {
            e10.printStackTrace();
            return null;
        }
    }

    public static String bytesToHex(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (byte b : bytes) {
            // 将字节转换为无符号整数表示的16进制，并且保证每个字节输出两个字符
            sb.append(String.format("%02x", b & 0xFF));
        }
        return sb.toString();
    }
    
    public static byte[] sha3(byte[] bytes, int digit) {
        SHA3Digest digest = new SHA3Digest(digit);
        digest.update(bytes, 0, bytes.length);
        byte[] rsData = new byte[digest.getDigestSize()];
        digest.doFinal(rsData, 0);
        return rsData;
    }

    public static String sha256(String input) {
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-256");
            byte[] hash = digest.digest(input.getBytes());
            StringBuilder hexString = new StringBuilder();
            for (byte b : hash) {
                String hex = Integer.toHexString(0xff & b);
                if (hex.length() == 1) {
                    hexString.append('0');
                }
                hexString.append(hex);
            }
            return hexString.toString();
        } catch (Exception e) {
            log.error("SHA-256 Hash failed", e);
            return null;
        }
    }

    public static String sha3(String data, int digit) {
        return Hex.toHexString(sha3(data.getBytes(), digit));
    }

    public static String toHexString(byte[] data) {
        return Hex.toHexString(data);
    }


}
