`
明子健
  • 浏览: 573606 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

MD5加密工具类

阅读更多
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Base64;
import java.util.Random;

/**
 * 
 * @author ming
 *
 */
public class MD5Util {

	private MD5Util() {
	}

	/**
	 * Used building output as Hex
	 */
	private static final char[] DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e',
			'f' };

	private static final String CHARSET_DEFAULT = "utf-8";
	private static final int SALT_LENGTH_DEFAULT = 8;

	/**
	 * md5
	 * 
	 * @param text
	 *            明文
	 * @param charset
	 *            字符集
	 * @param isUpperCase
	 *            是否将签名转化为大写字母
	 * @return
	 */
	public static String md5(String text, String charset, boolean isUpperCase) {
		if (text == null) {
			return null;
		}
		MessageDigest msgDigest = null;

		try {
			msgDigest = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			throw new IllegalStateException("不支持MD5算法");
		}

		if (charset == null) {
			charset = CHARSET_DEFAULT;
		}
		try {
			msgDigest.update(text.getBytes(charset));
		} catch (UnsupportedEncodingException e) {
			throw new IllegalStateException("不支持"+charset+"编码");
		}

		byte[] bytes = msgDigest.digest();

		String md5Str = new String(encodeHex(bytes));

		if (isUpperCase) {
			return md5Str.toUpperCase();
		}
		return md5Str;
	}

	/**
	 * 明文+盐 md5
	 * 
	 * @param text
	 *            明文
	 * @param salt
	 *            盐
	 * @return md5(text+salt),but if salt==null return md5(text)
	 */
	public static String saltedMd5(String text, String salt) {
		if (salt == null) {
			return md5(text);
		}
		return md5(text + salt);
	}

	/**
	 * 验证
	 * 
	 * @param text
	 *            明文
	 * @param salt
	 *            盐
	 * @param sign
	 *            签名
	 * @return
	 */
	public static boolean verify(String text, String salt, String sign) {
		if (sign == null || text == null) {
			return false;
		}
		return sign.equals(saltedMd5(text, salt));
	}

	/**
	 * 验证
	 * 
	 * @param text
	 *            明文
	 * @param sign
	 *            签名
	 * @return
	 */
	public static boolean verify(String text, String sign) {
		return verify(text, null, sign);
	}

	/**
	 * md5 ,小写字母+数字 , utf-8
	 * 
	 * @param text
	 *            明文
	 * @return
	 */
	public static String md5(String text) {
		return md5(text, CHARSET_DEFAULT, false);
	}

	/**
	 * 16进制编码
	 * 
	 * @param data
	 * @return
	 */
	private static char[] encodeHex(byte[] data) {

		int l = data.length;

		char[] out = new char[l << 1];

		// two characters form the hex value.
		for (int i = 0, j = 0; i < l; i++) {
			out[j++] = DIGITS[(0xF0 & data[i]) >>> 4];
			out[j++] = DIGITS[0x0F & data[i]];
		}

		return out;
	}

	/**
	 * 生成随机盐
	 * 
	 * @param length
	 *            随机盐的长度
	 * @return
	 */
	public static String generateRandomSalt(int length) {
		if (length <= 0) {
			return "";
		}
		Random random = new SecureRandom();
		byte[] saltByte = new byte[length];
		random.nextBytes(saltByte);
		String salt = Base64.getEncoder().encodeToString(saltByte);
		return salt.substring(0, length);
	}

	/**
	 * 生成一个8位长度的随机盐字符串
	 * 
	 * @return
	 */
	public static String generateRandomSalt() {
		return generateRandomSalt(SALT_LENGTH_DEFAULT);
	}

}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics