TLinx2Util.java 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. package com.happy.Unitil_nsh;
  2. //~--- non-JDK imports --------------------------------------------------------
  3. import com.alibaba.fastjson.JSON;
  4. import com.alibaba.fastjson.JSONObject;
  5. import java.util.HashMap;
  6. import java.util.Iterator;
  7. import java.util.Map;
  8. import java.util.TreeMap;
  9. import java.util.regex.Matcher;
  10. import java.util.regex.Pattern;
  11. //~--- JDK imports ------------------------------------------------------------
  12. //~--- classes ----------------------------------------------------------------
  13. /**
  14. * Class TLinx2Util
  15. * Description
  16. * Create 2017-03-07 14:01:23
  17. *
  18. * @author Benny.YEE
  19. */
  20. public class TLinx2Util {
  21. /**
  22. * 签名
  23. *
  24. * @param postMap
  25. * @return
  26. */
  27. public static String sign(Map<String, String> postMap) {
  28. String sign = null;
  29. try {
  30. /**
  31. * 1 A~z排序(加上open_key)
  32. */
  33. String sortStr = sort(postMap);
  34. System.out.println("====排序后的待签名字符串= " + sortStr);
  35. /**
  36. * 2 sha1加密(小写)
  37. */
  38. String sha1 = com.happy.Unitil_nsh.TLinxSHA1.SHA1(sortStr).toLowerCase();
  39. System.out.println("====sha1加密后的待签名字符串= " + sha1);
  40. /**
  41. * 3 md5加密(小写)
  42. */
  43. sign = com.happy.Unitil_nsh.TLinxMD5.MD5Encode(sha1).toLowerCase();
  44. } catch (Exception e) {
  45. e.printStackTrace();
  46. }
  47. return sign;
  48. }
  49. /**
  50. * 验签
  51. *
  52. * @param respObject
  53. * @return
  54. */
  55. public static Boolean verifySign(JSONObject respObject) {
  56. String respSign = respObject.get("sign").toString();
  57. respObject.remove("sign"); // 删除sign节点
  58. respObject.put("open_key", com.happy.Unitil_nsh.TestParams.OPEN_KEY);
  59. System.out.println("==========开始验签==========");
  60. String veriSign = sign(JSONObject.toJavaObject(respObject, Map.class)); // 按A~z排序,串联成字符串,先进行sha1加密(小写),再进行md5加密(小写),得到签名
  61. if (respSign.equals(veriSign)) {
  62. System.out.println("==========验签成功==========");
  63. return true;
  64. }
  65. return false;
  66. }
  67. /**
  68. * AES加密,再二进制转十六进制(bin2hex)
  69. *
  70. * @param postmap 说明:
  71. * @throws Exception
  72. */
  73. public static void handleEncrypt(TreeMap<String, ?> datamap, TreeMap<String, String> postmap) throws Exception {
  74. JSONObject dataobj = JSONObject.parseObject(JSON.toJSONString(datamap));
  75. System.out.println(dataobj);
  76. String data = com.happy.Unitil_nsh.TLinxAESCoder.encrypt(dataobj.toString(), com.happy.Unitil_nsh.TestParams.OPEN_KEY); // AES加密,并bin2hex
  77. System.out.println("====加密后的data= " + data);
  78. postmap.put("data", data);
  79. }
  80. /**
  81. * 签名
  82. *
  83. * @param postmap
  84. */
  85. public static void handleSign(TreeMap<String, String> postmap) {
  86. Map<String, String> veriDataMap = new HashMap<String, String>();
  87. veriDataMap.putAll(postmap);
  88. veriDataMap.put("open_key", com.happy.Unitil_nsh.TestParams.OPEN_KEY);
  89. // 签名
  90. String sign = sign(veriDataMap);
  91. System.out.println("====已签名字符串= " + sign);
  92. postmap.put("sign", sign);
  93. }
  94. /**
  95. * 签名(商户对账单下载接口)
  96. *
  97. * @param postmap
  98. */
  99. public static void handleSign(TreeMap<String, String> postmap, String pass) {
  100. Map<String, String> veriDataMap = new HashMap<String, String>();
  101. veriDataMap.putAll(postmap);
  102. String sha1Str = com.happy.Unitil_nsh.TLinxSHA1.SHA1(pass);
  103. String md5Str = com.happy.Unitil_nsh.TLinxMD5.MD5Encode(sha1Str).toLowerCase();
  104. System.out.println("====sha1Str= " + sha1Str);
  105. System.out.println("====md5Str= " + md5Str);
  106. veriDataMap.put("pass", md5Str);
  107. // 签名
  108. String sign = sign(veriDataMap);
  109. System.out.println("====已签名字符串= " + sign);
  110. postmap.put("sign", sign);
  111. }
  112. /**
  113. * 根据返回格式来选择post请求处理方式
  114. *
  115. * @param postmap
  116. * @param interfaceName
  117. * @param tarType
  118. * @return
  119. */
  120. public static String handlePostbyTarType(TreeMap<String, String> postmap, String interfaceName, String tarType) {
  121. if ("gzip".equals(tarType)) {
  122. return handlePostGZIP(postmap, interfaceName);
  123. } else {
  124. return handlePost(postmap, interfaceName);
  125. }
  126. }
  127. /**
  128. * 请求接口
  129. *
  130. * @param postmap
  131. * @return 响应字符串
  132. */
  133. public static String handlePost(TreeMap<String, String> postmap, String interfaceName) {
  134. String url = com.happy.Unitil_nsh.TestParams.OPEN_URL + interfaceName;
  135. System.out.println("====请求地址= " + url);
  136. if (url.contains("https")) {
  137. return HttpsUtil.httpMethodPost(url, postmap, "UTF-8");
  138. } else {
  139. return com.happy.Unitil_nsh.HttpUtil.httpMethodPost(url, postmap, "UTF-8");
  140. }
  141. }
  142. public static String handlePostGZIP(TreeMap<String, String> postmap, String interfaceName) {
  143. String url = com.happy.Unitil_nsh.TestParams.OPEN_URL + interfaceName;
  144. if (url.contains("https")) {
  145. return HttpsUtil.httpMethodPostGZIP(url, postmap, "UTF-8");
  146. } else {
  147. return com.happy.Unitil_nsh.HttpUtil.httpMethodPostGZIP(url, postmap, "UTF-8");
  148. }
  149. }
  150. /**
  151. * 十六进制字符串转byte数组
  152. */
  153. public static byte[] hex2byte(String strhex) {
  154. if (strhex == null)
  155. return null;
  156. int l = strhex.length();
  157. if (l % 2 == 1)
  158. return null;
  159. byte[] b = new byte[l / 2];
  160. for (int i = 0; i != l / 2; ++i)
  161. b[i] = (byte) Integer.parseInt(strhex.substring(i * 2, i * 2 + 2), 16);
  162. return b;
  163. }
  164. /**
  165. * byte数组转十六进制字符串
  166. */
  167. public static String byte2hex(byte[] result) {
  168. StringBuffer sb = new StringBuffer(result.length * 2);
  169. for (int i = 0; i < result.length; i++) {
  170. int hight = ((result[i] >> 4) & 0x0f);
  171. int low = result[i] & 0x0f;
  172. sb.append(hight > 9 ? (char) ((hight - 10) + 'a') : (char) (hight + '0'));
  173. sb.append(low > 9 ? (char) ((low - 10) + 'a') : (char) (low + '0'));
  174. }
  175. return sb.toString();
  176. }
  177. /**
  178. * 排序
  179. *
  180. * @param paramMap
  181. * @return
  182. * @throws Exception
  183. */
  184. public static String sort(Map paramMap) throws Exception {
  185. String sort = "";
  186. com.happy.Unitil_nsh.TLinxMapUtil signMap = new com.happy.Unitil_nsh.TLinxMapUtil();
  187. if (paramMap != null) {
  188. String key;
  189. for (Iterator it = paramMap.keySet().iterator(); it.hasNext(); ) {
  190. key = (String) it.next();
  191. String value = ((paramMap.get(key) != null) && (!("".equals(paramMap.get(key).toString())))) ? paramMap.get(key).toString() : "";
  192. signMap.put(key, value);
  193. }
  194. signMap.sort();
  195. for (Iterator it = signMap.keySet().iterator(); it.hasNext(); ) {
  196. key = (String) it.next();
  197. sort = sort + key + "=" + signMap.get(key).toString() + "&";
  198. }
  199. if ((sort != null) && (!("".equals(sort)))) {
  200. sort = sort.substring(0, sort.length() - 1);
  201. }
  202. }
  203. return sort;
  204. }
  205. /**
  206. * unicode转中文
  207. *
  208. * @param unicode
  209. * @return
  210. */
  211. public static String unicodeToCn(String unicode) {
  212. /** 以 \ u 分割,因为java注释也能识别unicode,因此中间加了一个空格*/
  213. Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");
  214. Matcher matcher = pattern.matcher(unicode);
  215. char ch;
  216. while (matcher.find()) {
  217. ch = (char) Integer.parseInt(matcher.group(2), 16);
  218. unicode = unicode.replace(matcher.group(1), ch + "");
  219. }
  220. return unicode;
  221. }
  222. /**
  223. * Method main
  224. * Description 说明:
  225. *
  226. * @param args 说明:
  227. */
  228. public static void main(String[] args) {
  229. }
  230. }
  231. //~ Formatted by Jindent --- http://www.jindent.com