package cn.quantgroup.xyqb.util.encrypt;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

/**
 * Created by tums on 2015/11/30.
 */
@Slf4j
public final class Md5Util {

    public static String build(String content) {
        MessageDigest messageDigest;
        try {
            messageDigest = MessageDigest.getInstance("md5");
        } catch (NoSuchAlgorithmException e) {
            log.error("MessageDigest获取实例失败", e);
            return null;
        }
        messageDigest.update(content.getBytes());
        byte[] domain = messageDigest.digest();
        StringBuilder md5StrBuff = new StringBuilder();
        // converting domain to String
        for (int i = 0; i < domain.length; i++) {
            if (Integer.toHexString(0xFF & domain[i]).length() == 1) {
                md5StrBuff.append("0").append(
                        Integer.toHexString(0xFF & domain[i]));
            } else {
                md5StrBuff.append(Integer.toHexString(0xFF & domain[i]));
            }
        }
        return md5StrBuff.toString();
    }

    public static void main(String[] args){
        System.out.println(Md5Util.build("13718656985"));
    }
}
