All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sdn.api.utils.MD5 Maven / Gradle / Ivy

package org.sdn.api.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

/**
 * MD5工具类
 * @author sdc
 * 
 */
public class MD5 {

	private static Logger logger = LoggerFactory.getLogger(MD5.class);
	
	private MD5(){}
	
	private static final String[] hexDigits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };

	/**
	 * 转换字节数组为16进制字串
	 * 
	 * @param b
	 *            字节数组
	 * @return 16进制字串
	 */
	public static String byteArrayToHexString(byte[] b) {
		StringBuffer resultSb = new StringBuffer();
		for (byte e : b) {
			resultSb.append(byteToHexString(e));
		}
		return resultSb.toString();
	}

	/**
	 * 转换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];
	}

	/**
	 * MD5编码
	 * 
	 * @param origin 原始字符串
	 * @param charset 字符集
	 * @return 经过MD5加密之后的结果
	 */
	public static String encodeMD5(String origin, String charset) {
		char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
		try {
			byte[] btInput = origin.getBytes(charset);
			// 获得MD5摘要算法的 MessageDigest 对象
			MessageDigest md = MessageDigest.getInstance("MD5");
			// 使用指定的字节更新摘要
			md.update(btInput);
			// 获得密文
			byte[] tmp = md.digest();
			// 把密文转换成十六进制的字符串形式
			int j = tmp.length;
			char[] str = new char[j * 2];
			int k = 0;
			for (int i = 0; i < j; i++) {
				byte byte0 = tmp[i];
				str[k++] = hexDigits[byte0 >>> 4 & 0xf];
				str[k++] = hexDigits[byte0 & 0xf];
			}
			return new String(str);
		} catch (Exception e) {
			logger.info("MD5 encodeMD5() Exception {}",e);
		}
		return null;
	}

	/**
	 * MD5编码
	 * 
	 * @param origin 原始字符串
	 * @return 经过MD5加密之后的结果
	 */
	public static String encodeMD5(String origin) {
		String resultString = null;
		try {
			resultString = origin;
			MessageDigest md = MessageDigest.getInstance("MD5");
			resultString = byteArrayToHexString(md.digest(resultString.getBytes("UTF-8")));
		} catch (Exception e) {
			logger.info("MD5 encodeMD5 Exception {}",e);
		}
		return resultString;
	}

	/**
	 * 加盐MD5
	 * 
	 * @param origin 原始字符串
	 * @return 返回加盐后的md5值
	 */
	public static String encodeMD5WithSalt(String origin) {
		SecureRandom random;
		try {
			random = SecureRandom.getInstance("SHA1PRNG");
		} catch (NoSuchAlgorithmException e) {
			random = new SecureRandom();
		}
		StringBuffer sb = new StringBuffer(16);
		sb.append(random.nextInt(99999999)).append(random.nextInt(99999999));
		int len = sb.length();
		if (len < 16) {
			for (int i = 0; i < 16 - len; i++) {
				sb.append("0");
			}
		}
		String salt = sb.toString();
		origin = md5Hex(origin + salt);
		char[] cs = new char[48];
		for (int i = 0; i < 48; i += 3) {
			cs[i] = origin.charAt(i / 3 * 2);
			char c = salt.charAt(i / 3);
			cs[i + 1] = c;
			cs[i + 2] = origin.charAt(i / 3 * 2 + 1);
		}
		return new String(cs);
	}

	/**
	 * 校验加盐后是否和原文一致
	 * 
	 * @param origin 原字符串
	 * @param md5Cipher md5值
	 * @return 验证结果
	 */
	public static boolean verify(String origin, String md5Cipher) {
		char[] cs1 = new char[32];
		char[] cs2 = new char[16];
		for (int i = 0; i < 48; i += 3) {
			cs1[i / 3 * 2] = md5Cipher.charAt(i);
			cs1[i / 3 * 2 + 1] = md5Cipher.charAt(i + 2);
			cs2[i / 3] = md5Cipher.charAt(i + 1);
		}
		String salt = new String(cs2);
		return md5Hex(origin + salt).equals(new String(cs1));
	}

	/**
	 * 获取十六进制字符串形式的MD5摘要
	 */
	private static String md5Hex(String src) {
		try {
			MessageDigest md5 = MessageDigest.getInstance("MD5");
			byte[] bs = md5.digest(src.getBytes("UTF-8"));
			return byteArrayToHexString(bs);
		} catch (Exception e) {
			logger.info("MD5 md5Hex Exception {}",e);
		}
		return "";
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy