content: package com.gpmall.pay.biz.payment.channel.alipay;
  
  import org.apache.commons.codec.digest.DigestUtils;
  
  import java.io.UnsupportedEncodingException;
  import java.security.MessageDigest;
  import java.security.SignatureException;
  
  public class MD5 {
  
  	private final static String[] hexDigits = {"0", "1", "2", "3", "4", "5", "6", "7",
  			"8", "9", "a", "b", "c", "d", "e", "f"};
  
  	/**
  	 * 签名字符串
  	 *
  	 * @param text          需要签名的字符串
  	 * @param key           密钥
  	 * @param input_charset 编码格式
  	 * @return 签名结果
  	 */
  	public static String sign(String text, String key, String input_charset) {
  		text = text + key;
  		return DigestUtils.md5Hex(getContentBytes(text, input_charset));
  	}
  
  	/**
  	 * 签名字符串
  	 *
  	 * @param text          需要签名的字符串
  	 * @param sign          签名结果
  	 * @param key           密钥
  	 * @param input_charset 编码格式
  	 * @return 签名结果
  	 */
  	public static boolean verify(String text, String sign, String key, String input_charset) {
  		text = text + key;
  		String mysign = DigestUtils.md5Hex(getContentBytes(text, input_charset));
  		if (mysign.equals(sign)) {
  			return true;
  		} else {
  			return false;
  		}
  	}
  
  	/**
  	 * @param content
  	 * @param charset
  	 * @return
  	 * @throws SignatureException
  	 * @throws UnsupportedEncodingException
  	 */
  	private static byte[] getContentBytes(String content, String charset) {
  		if (charset == null || "".equals(charset)) {
  			return content.getBytes();
  		}
  		try {
  			return content.getBytes(charset);
  		} catch (UnsupportedEncodingException e) {
  			throw new RuntimeException("MD5签名过程中出现错误,指定的编码集不对,您目前指定的编码集是:" + charset);
  		}
  	}
  
  
  	/**
  	 * MD5编码
  	 *
  	 * @param origin 原始字符串
  	 * @return 经过MD5加密之后的结果
  	 */
  	public static String MD5Encode(String origin) {
  		String resultString = null;
  		try {
  			resultString = origin;
  			MessageDigest md = MessageDigest.getInstance("MD5");
  			resultString = byteArrayToHexString(md.digest(resultString.getBytes()));
  		} catch (Exception e) {
  			e.printStackTrace();
  		}
  		return resultString;
  	}
  
  	/**
  	 * 转换字节数组为16进制字串
  	 *
  	 * @param b 字节数组
  	 * @return 16进制字串
  	 */
  	public static String byteArrayToHexString(byte[] b) {
  		StringBuilder resultSb = new StringBuilder();
  		for (byte aB : b) {
  			resultSb.append(byteToHexString(aB));
  		}
  		return resultSb.toString();
  	}
  
  	public static void main(String[] args) throws UnsupportedEncodingException {
  		System.out.println(string2Unicode("中文"));
  	}
  
  	static String string2Unicode(String s) {
  		char[] utfBytes = s.toCharArray();
  		String unicodeBytes = "";
  		for (int byteIndex = 0; byteIndex < utfBytes.length; byteIndex++) {
  			String hexB = Integer.toHexString(utfBytes[byteIndex]);
  			if (hexB.length() <= 2) {
  				hexB = "00" + hexB;
  			}
  			unicodeBytes = unicodeBytes + "\\u" + hexB;
  		}
  		System.out.println("unicodeBytes is: " + unicodeBytes);
  		return unicodeBytes;
  	}
  
  	/**
  	 * 转换byte到16进制
  	 *
  	 * @param b 要转换的byte
  	 * @return 16进制格式
  	 */
  	private static String byteToHexString(byte b) {
  		int n = b;
  		if (n < 0) {
  			n = 256 + n;
  		}
  		int d1 = n / 16;
  		int d2 = n % 16;
  		return hexDigits[d1] + hexDigits[d2];
  	}
  
  }