Java 生成文件MD5算法

代码:

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

public class MD5Util {
	private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5",
			"6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };

    private static ThreadLocal<MessageDigest> MD5 = new ThreadLocal<MessageDigest>()
    {
        @Override
        protected MessageDigest initialValue()
        {
            try
            {
                return MessageDigest.getInstance("MD5");
            } catch (NoSuchAlgorithmException e)
            {
                throw new IllegalStateException("no md5 algorythm found");
            }
        }
    };
    
	public static String byteArrayToHexString(byte[] b) {
		StringBuffer resultSb = new StringBuffer();
		for (int i = 0; i < b.length; i++) {
			resultSb.append(byteToHexString(b[i], true));
		}
		return resultSb.toString();
	}

	public static String byteArrayToHexStringLittleEnding(byte[] b) {
		StringBuffer resultSb = new StringBuffer();
		for (int i = 0; i < b.length; i++) {
			resultSb.append(byteToHexString(b[i], false));
		}
		return resultSb.toString();
	}
	
	private static String byteToHexString(byte b, boolean bigEnding) {
		int n = b;
		if (n < 0)
			n = 256 + n;
		int d1 = n / 16;
		int d2 = n % 16;
		return (bigEnding)?(hexDigits[d1] + hexDigits[d2]):(hexDigits[d2] + hexDigits[d1]);
	}

	public static String MD5Encode(String origin) {
		return MD5Encode(origin, null);
	}

	/**
	 * 把16进制字符串转换为byte数组
	 * @param s
	 * @return
	 */
	public static byte[] hexStringToByteArray(String s){
		if(s.length()%2 != 0){
			throw new RuntimeException("Error Hex String length");
		}
		byte[] result = new byte[s.length()/2];
		for(int i=0; i<s.length();){
			int bytepos = i/2;
			char c = s.charAt(i++);
			char c2 = s.charAt(i++);
			result[bytepos] = Integer.decode("0x"+c+c2).byteValue();
		}
		return result;
	}
	
	/**
	 * MD5摘要
	 * @param origin  摘要原文
	 * @param encoding  字符串origin 的编码
	 * @return
	 */
	public static String MD5Encode(String origin, String encoding) {
		String resultString = null;

		try {
			resultString = new String(origin);
			MessageDigest md = MD5.get();
			if (encoding == null) {
				resultString = byteArrayToHexString(md.digest(resultString
						.getBytes()));
			} else {
				resultString = byteArrayToHexString(md.digest(resultString
						.getBytes(encoding)));
			}

		} catch (Exception e) {
			throw new RuntimeException(e);
		}

		return resultString;
	}
	
	public static MessageDigest getMd5Digest(){
	    return MD5.get();
	}
	
	public static byte[] MD5Encode(byte origin[]) {
		try {
			MessageDigest md = MD5.get();
			return md.digest(origin);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

寒江蓑笠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值