content: package com.open.boot.core.utils;
  
  import java.io.UnsupportedEncodingException;
  import java.net.InetAddress;
  import java.net.UnknownHostException;
  import java.security.MessageDigest;
  import java.security.NoSuchAlgorithmException;
  
  import javax.servlet.http.HttpServletRequest;
  
  import org.apache.shiro.crypto.hash.Md5Hash;
  
  /**
   * MD5工具类和ip地址
   */
  public class DigestUtil {
  
  	public static String MD5(String s) {
  		char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
  		try {
  			byte[] btInput = s.getBytes();
  			// 获得MD5摘要算法的 MessageDigest 对象
  			MessageDigest mdInst = MessageDigest.getInstance("MD5");
  			// 使用指定的字节更新摘要
  			mdInst.update(btInput);
  			// 获得密文
  			byte[] md = mdInst.digest();
  			// 把密文转换成十六进制的字符串形式
  			int j = md.length;
  			char str[] = new char[j * 2];
  			int k = 0;
  			for (int i = 0; i < j; i++) {
  				byte byte0 = md[i];
  				str[k++] = hexDigits[byte0 >>> 4 & 0xf];
  				str[k++] = hexDigits[byte0 & 0xf];
  			}
  			return new String(str);
  		} catch (Exception e) {
  			e.printStackTrace();
  			return null;
  		}
  	}
  
  	public static String getMd5String(String str) {
  		MessageDigest messageDigest = null;
  
  		try {
  			messageDigest = MessageDigest.getInstance("MD5");
  
  			messageDigest.reset();
  
  			messageDigest.update(str.getBytes("UTF-8"));
  		} catch (NoSuchAlgorithmException e) {
  			System.out.println("NoSuchAlgorithmException caught!");
  			System.exit(-1);
  		} catch (UnsupportedEncodingException e) {
  			e.printStackTrace();
  		}
  
  		byte[] byteArray = messageDigest.digest();
  
  		StringBuffer md5StrBuff = new StringBuffer();
  
  		for (int i = 0; i < byteArray.length; i++) {
  			if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
  				md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
  			else
  				md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
  		}
  
  		return md5StrBuff.toString();
  	}
  
  	/**
  	 * 得到IP地址
  	 * 
  	 * @param request request请求
  	 * @return IP地址
  	 */
  	public static String getIpAddress(HttpServletRequest request) {
  		String ipAddress = null;
  		ipAddress = request.getHeader("x-forwarded-for");
  		if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
  			ipAddress = request.getHeader("Proxy-Client-IP");
  		}
  		if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
  			ipAddress = request.getHeader("WL-Proxy-Client-IP");
  		}
  		if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
  			ipAddress = request.getRemoteAddr();
  
  			if (ipAddress.equals("0:0:0:0:0:0:0:1") || ipAddress.equals("127.0.0.1")) {
  				// 根据网卡取本机配置的IP
  				InetAddress inet = null;
  				try {
  					inet = InetAddress.getLocalHost();
  				} catch (UnknownHostException e) {
  					e.printStackTrace();
  				}
  				ipAddress = inet.getHostAddress();
  			}
  		}
  		// 对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
  		if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length()
  															// = 15
  			if (ipAddress.indexOf(",") > 0) {
  				ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
  			}
  		}
  		ipAddress = ipAddress.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ipAddress;
  		return ipAddress;
  	}
  
  	/**
  	 * 判断登录设备
  	 * 
  	 * @param requestHeader requestHeader
  	 * @return 登录设备
  	 */
  	public static boolean checkPcOrMobile(String requestHeader) {
  		String[] deviceArray = new String[] { "android", "mac os", "windows phone" };
  		if (requestHeader == null) {
  			return false;
  		}
  		requestHeader = requestHeader.toLowerCase();
  		for (int i = 0; i < deviceArray.length; i++) {
  			if (requestHeader.indexOf(deviceArray[i]) > 0) {
  				return true;
  			}
  		}
  		return false;
  	}
  
  	public static String MD5Encode(String source, String salt, boolean uppercase) {
  		String result = null;
  		try {
  			result = source;
  			// 使用apache.shiro进行带盐值的MD5加密
  			result = new Md5Hash(result, salt, 3).toString();
  		} catch (Exception e) {
  			e.printStackTrace();
  		}
  		return uppercase ? result.toUpperCase() : result;
  	}
  }
  