package com.hai.common.utils;

import javax.xml.bind.annotation.adapters.HexBinaryAdapter;
import java.security.MessageDigest;

/**
 * 
 * @ClassName:  MD5Util   
 * @Description:TODO(MD5 加密工具)   
 * @author: 胡锐 
 * @date:   2019年5月6日 上午10:13:26   
 *     
 * @Copyright: 2019 www.shinwoten.com Inc. All rights reserved.
 */
public class MD5Util {

	
	/**
	 * 
	 * @Title: encode   
	 * @Description: TODO(MD5 32位加密) 
	 * @author: 胡锐
	 * @param: @param data
	 * @param: @return
	 * @param: @throws Exception      
	 * @return: String      
	 * @throws
	 */
	public static String encode(byte[] data) throws Exception {
		 // 初始化MessageDigest
        MessageDigest md = MessageDigest.getInstance("MD5");
        // 执行摘要信息
        byte[] digest = md.digest(data);
        // 将摘要信息转换为32位的十六进制字符串
        return new String(new HexBinaryAdapter().marshal(digest));
	}
	/* public static String EncoderByMd5(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException{
        //确定计算方法
        MessageDigest md5=MessageDigest.getInstance("MD5");
        Encoder base64en = Base64.getEncoder();
        //加密后的字符串
        String newstr=base64en.encodeToString(md5.digest(str.getBytes("utf-8")));
        return newstr;
	}*/

	public static String encodeS(byte[] data) throws Exception {
		// 初始化MessageDigest
		MessageDigest md = MessageDigest.getInstance("MD5");
		// 执行摘要信息
		byte[] digest = md.digest(data);
		// 将摘要信息转换为32位的十六进制字符串
		return new HexBinaryAdapter().marshal(digest);
	}
	/* public static String EncoderByMd5(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException{
        //确定计算方法
        MessageDigest md5=MessageDigest.getInstance("MD5");
        Encoder base64en = Base64.getEncoder();
        //加密后的字符串
        String newstr=base64en.encodeToString(md5.digest(str.getBytes("utf-8")));
        return newstr;
	}*/


	public static void main(String[] args) throws Exception {
		String str = "123456";
		System.out.println(MD5Util.encode(str.getBytes("UTF-8")));
		//String ee = "29AD0E3FD3DB681FB9F8091C756313F7";
	}
}
