package cn.ksource.web.facade.timetask; import java.io.IOException; import java.net.URLEncoder; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.sql.Timestamp; import java.text.ParseException; import java.util.Arrays; import java.util.Date; import java.util.HashMap; import java.util.Map; public class LoginUtil { @SuppressWarnings("rawtypes") public static Map union400Login(String appver,String user,String pwd) throws ParseException, NoSuchAlgorithmException, IOException{ //接收并处理传递参数 Map map = new HashMap(); map.put("appver", appver); map.put("user", user); //获取当前时间 Timestamp endtime = new Timestamp(new Date().getTime()); String time = String.valueOf(endtime); String time1 = time.replaceAll("-", ""); String time2 = time1.replaceAll(":", ""); String time3 = time2.replaceAll(" ", ""); String time4 = time3.substring(0, time3.indexOf("."));//http请求时间为当前时间 //计算Secrect map.put("timestamp", time4); String secret = signTopRequest(map, pwd); map.put("secret", secret); return map; } public static String signTopRequest(Map params, String token) throws IOException, NoSuchAlgorithmException { //1 删除secret params.remove("secret"); //2检查参数是否已经排序 String[] keys = params.keySet().toArray(new String[0]); Arrays.sort(keys); //3把所有参数名和参数值串在一起 StringBuilder query = new StringBuilder(); for (String key : keys) { String value = params.get(key); if (!"".equals(key) && key!=null && !"".equals(value) && value!=null) { query.append(URLEncoder.encode(key,"utf8")).append(URLEncoder.encode(value,"utf8")); } } //4使用MD5加密 query.append(token); byte[] bytes=encryptMD5(query.toString()); //5转换为字符串 return byte2hex(bytes); } public static byte[] encryptMD5(String data) throws IOException, NoSuchAlgorithmException { MessageDigest md5=MessageDigest.getInstance("MD5"); return md5.digest(data.getBytes("utf-8")); } public static String byte2hex(byte[] bytes) { StringBuilder sign = new StringBuilder(); for (int i = 0; i < bytes.length; i++) { String hex = Integer.toHexString(bytes[i] & 0xFF); if (hex.length() == 1) { sign.append("0"); } sign.append(hex.toUpperCase()); } return sign.toString(); } }