package cn.com.taiji.beidou.framework.web.util;

import java.security.MessageDigest;

/**
 * MD5密码加密工具
 * @author lizhi
 */
public class Md5Utils {
	private static final char[] HEX_DIGITS = "0123456789abcdef".toCharArray();
	private static final char[] CHAR_ARRAY = "_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();

	public static String md5(String srcStr) {
		return hash("MD5", srcStr);
	}

	public static String hash(String algorithm, String srcStr) {
		try {
			MessageDigest md = MessageDigest.getInstance(algorithm);
			byte[] bytes = md.digest(srcStr.getBytes("utf-8"));
			return toHex(bytes);
		} catch (Exception var4) {
			throw new RuntimeException(var4);
		}
	}

	public static String toHex(byte[] bytes) {
		StringBuilder ret = new StringBuilder(bytes.length * 2);

		for(int i = 0; i < bytes.length; ++i) {
			ret.append(HEX_DIGITS[bytes[i] >> 4 & 15]);
			ret.append(HEX_DIGITS[bytes[i] & 15]);
		}

		return ret.toString();
	}
}
