package com.sunwin.metro.utils;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;

/**
 * @author kane
 */
public class Md5Util {
    private static MessageDigest md5 = null;

    static {
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public static String getMd5(String str) {
        byte[] bs = null;
        synchronized (md5) {
            bs = md5.digest(str.getBytes());
        }
        StringBuilder sb = new StringBuilder();
        for (byte x : bs) {
            if ((x & 0xff) >> 4 == 0) {
                sb.append("0").append(Integer.toHexString(x & 0xff));
            } else {
                sb.append(Integer.toHexString(x & 0xff));
            }
        }
        return sb.toString();
    }

    public static String MD5(String str) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] array = md.digest(str.getBytes(StandardCharsets.UTF_8));
            StringBuilder sb = new StringBuilder();
            for (byte b : array) {
                sb.append(Integer.toHexString((b & 0xFF) | 0x100), 1, 3);
            }
            return sb.toString();
        } catch (java.security.NoSuchAlgorithmException ignored) {
        }
        return null;
    }

    public static void main(String[] args) {
        System.out.println(getMd5("aaaa"));
        System.out.println(MD5("aaaa"));
        System.out.println(MD5("1234"));
    }
}
