package com.fuzamei.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
 * 计算字符串的hash值工具类。 
 * @author dell
 *
 */
public class HashCodeUtil{

	private static final String[] hexDigits= {"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"};
   /**
     * 将字节数组转换为16进制的字符串 
     *  
     * @param byteArray 
     *            字节数组 
     * @return 16进制的字符串 
     */
	private static String byteArrayToHexString(byte[] byteArray) {
		StringBuffer sb = new StringBuffer();
		for (byte byt : byteArray) {
			sb.append(byteToHexString(byt));
		}
		return sb.toString();}
    /**
     * 将字节转换为16进制字符串
     * @param byt
     * @return 16进制字符串
     */
	private static String byteToHexString(byte b) {
	
		return hexDigits[(b >> 4) & 0x0f] + hexDigits[b & 0x0f];
	} 
	/**
	 * 将摘要信息转换为相应的编码
	 * @param code编码类型
	 * @param message摘要信息
	 * @return相应的编码字符串 
	 */
	private static String hash(String code,String message) {
		MessageDigest md;
		String encode=null;
		try {
			md=MessageDigest.getInstance(code);
			encode=byteArrayToHexString(md.digest(message.getBytes()));
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 计算message的MD5编码 
	 * @param message摘要信息
	 * @return MD5编码之后的字符串 
	 */
	public static String md5Hash(String  message) {
	 return hash("MD5",message);	
	}
	
	
	public static String  sha256Hash(String message) {
		return hash("sha256",message);
	}
	
	
	public static String sha512Hash(String message) {
		return hash("sha512",message);
	}
	
	public static void main(String[] args) {
		System.out.println("--MD55555555555555--:" + HashCodeUtil.md5Hash("C:\\Users\\dell\\Desktop"));
	}

	
}
