package com.younike.hlpay.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


/**
 * 
 * @ClassName: Disguiser
 * @Description: 哈希算法的工具类，提供SHA MD5 HMAC等算法
 *
 */
public class Disguiser {

	public static final String ENCODE = "UTF-8";
//	private static final String KEY = "3csZRcPEBYAz0tjLQvhl48zBvTe8EW9z";

	public static String disguiseMD5(String message) {

		if (null == message) {

			return null;
		}

		return disguiseMD5(message, ENCODE);
	}
	
//	public static String disguise(String message){
//    	return disguise(message+KEY,ENCODE);
//
//    }
	public static String disguise(String message, String encoding){
    	message = message.trim();
        byte value[];
        try{
            value = message.getBytes(encoding);
        }
        catch(UnsupportedEncodingException e){
            value = message.getBytes();
        }
        MessageDigest md = null;
        try{
            md = MessageDigest.getInstance("SHA");
        }catch(NoSuchAlgorithmException e){
        	e.printStackTrace();
            return null;
        }
        return ConvertUtils.toHex(md.digest(value));
    }

	public static String disguiseMD5(String message, String encoding) {

		if (null == message || null == encoding) {

			return null;
		}

		message = message.trim();
		byte value[];
		try {
			value = message.getBytes(encoding);
		} catch (UnsupportedEncodingException e) {
			value = message.getBytes();
		}
		MessageDigest md = null;
		try {
			md = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return null;
		}
		return ConvertUtils.toHex(md.digest(value));
	}

//	public static void main(String[] args) {
//           System.out.println(disguiseMD5("&MerchantSettlement&0003&E1800010627&p_20171222191544&" + "fd95Gv3ZAGE02kv8oTM6yFfMKeU63XW0"));
//	}

}
