import com.dxhy.common.util.MD5;
import com.dxhy.common.util.Security;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author Allen
 * @version 1.0.0
 * @ClassName MD5Utils.java
 * @Description TODO
 * @createTime 2024年08月09日 13:51:00
 */
public class MD5Utils {


    public static String getMD5(String input) throws NoSuchAlgorithmException {
        try {

            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] messageDigest = md.digest(input.getBytes());

            // Convert byte array into signum representation
            StringBuilder sb = new StringBuilder();
            for (byte b : messageDigest) {

                sb.append(String.format("%02x", b));
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {

            throw new RuntimeException(e);
        }
    }

    /**
     * String -> MD5
     */
//    public static String getMD5(String val) throws NoSuchAlgorithmException {
//        MessageDigest md5 = MessageDigest.getInstance("MD5");
//        md5.update(val.getBytes());
//        byte[] m = md5.digest();// 加密
//        return getString(m);
//    }

    // bArray -> String
    private static String getString(byte[] b) {
        StringBuffer hexString = new StringBuffer();
        for (int i=0; i<b.length; i++)
            hexString.append(Integer.toHexString(0xFF & b[i]));
        return hexString.toString();
    }

    public static void main(String[] args) throws NoSuchAlgorithmException {
        String str = "fsdjofjsdfjsodfjsdjfiosdjfosdjfsdfasdfjasiofjoasdifjosi";
        System.out.println(getMD5(str).equals("de0a40510d141dbf55d722439e6c0a94"));
        System.out.println(getMD5(str).toUpperCase());
        System.out.println(MD5.getEncode(str));
        System.out.println(Security.MD5(str));

    }

}
