Java tutorial
/** * * Licensed Property to China UnionPay Co., Ltd. * * (C) Copyright of China UnionPay Co., Ltd. 2010 * All Rights Reserved. * * * Modification History: * ============================================================================= * Author Date Description * ------------ ---------- --------------------------------------------------- * xshu 2014-05-28 MPI * ============================================================================= */ package com.flame.core.pay.union.common; import org.apache.commons.lang3.StringUtils; import java.io.UnsupportedEncodingException; import java.util.*; import java.util.Map.Entry; public class SDKUtil { /** * ???(SHA1?) * * @param data * ???Map? * @param encoding * ? * @return ???? */ public static boolean sign(Map<String, String> data, String encoding) { if (isEmpty(encoding)) { encoding = "UTF-8"; } // ????? data.put(SDKConstants.param_certId, CertUtil.getSignCertId()); // Map???key1=value1&key2=value2? String stringData = coverMap2String(data); LogUtil.writeLog("??:[" + stringData + "]"); /** * ??\base64? */ byte[] byteSign = null; String stringSign = null; try { // SHA1?16 byte[] signDigest = SecureUtil.sha1X16(stringData, encoding); byteSign = SecureUtil.base64Encode(SecureUtil.signBySoft(CertUtil.getSignCertPrivateKey(), signDigest)); stringSign = new String(byteSign); // ?? data.put(SDKConstants.param_signature, stringSign); return true; } catch (Exception e) { LogUtil.writeErrorLog("??", e); return false; } } /** * ????????????<br> * * @param data * ???Map? * @param encoding * ? * @param certPath * ?? * @param certPwd * ?? * @return ?? */ public static boolean signByCertInfo(Map<String, String> data, String certPath, String certPwd, String encoding) { if (isEmpty(encoding)) { encoding = "UTF-8"; } if (isEmpty(certPath) || isEmpty(certPwd)) { LogUtil.writeLog("Invalid Parameter:CertPath=[" + certPath + "],CertPwd=[" + certPwd + "]"); return false; } // ????? data.put(SDKConstants.param_certId, CertUtil.getCertIdByKeyStoreMap(certPath, certPwd)); // Map???key1=value1&key2=value2? String stringData = coverMap2String(data); /** * ??\base64? */ byte[] byteSign = null; String stringSign = null; try { byte[] signDigest = SecureUtil.sha1X16(stringData, encoding); byteSign = SecureUtil.base64Encode( SecureUtil.signBySoft(CertUtil.getSignCertPrivateKeyByStoreMap(certPath, certPwd), signDigest)); stringSign = new String(byteSign); // ?? data.put(SDKConstants.param_signature, stringSign); return true; } catch (Exception e) { LogUtil.writeErrorLog("??", e); return false; } } /** * Map???Keyascii???key1=value1&key2=value2? ????signature * * @param data * Map? * @return ? */ public static String coverMap2String(Map<String, String> data) { TreeMap<String, String> tree = new TreeMap<String, String>(); Iterator<Entry<String, String>> it = data.entrySet().iterator(); while (it.hasNext()) { Entry<String, String> en = it.next(); if (SDKConstants.param_signature.equals(en.getKey().trim())) { continue; } tree.put(en.getKey(), en.getValue()); } it = tree.entrySet().iterator(); StringBuffer sf = new StringBuffer(); while (it.hasNext()) { Entry<String, String> en = it.next(); sf.append(en.getKey() + SDKConstants.EQUAL + en.getValue() + SDKConstants.AMPERSAND); } return sf.substring(0, sf.length() - 1); } /** * ? key=value&key=value?Map * * @param result * @return */ public static Map<String, String> coverResultString2Map(String result) { return convertResultStringToMap(result); } /** * key=value&key=value?Map * * @param result * @return */ public static Map<String, String> convertResultStringToMap(String result) { Map<String, String> map = null; try { if (StringUtils.isNotBlank(result)) { if (result.startsWith("{") && result.endsWith("}")) { System.out.println(result.length()); result = result.substring(1, result.length() - 1); } map = parseQString(result); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return map; } /** * ??? * * @param str * ?? * @return ?map * @throws UnsupportedEncodingException */ public static Map<String, String> parseQString(String str) throws UnsupportedEncodingException { Map<String, String> map = new HashMap<String, String>(); int len = str.length(); StringBuilder temp = new StringBuilder(); char curChar; String key = null; boolean isKey = true; boolean isOpen = false;// char openName = 0; if (len > 0) { for (int i = 0; i < len; i++) {// ??? curChar = str.charAt(i);// ?? if (isKey) {// ??key if (curChar == '=') {// ?= key = temp.toString(); temp.setLength(0); isKey = false; } else { temp.append(curChar); } } else {// ??value if (isOpen) { if (curChar == openName) { isOpen = false; } } else {//? if (curChar == '{') {//? isOpen = true; openName = '}'; } if (curChar == '[') { isOpen = true; openName = ']'; } } if (curChar == '&' && !isOpen) {// ?&,??map putKeyValueToMap(temp, isKey, key, map); temp.setLength(0); isKey = true; } else { temp.append(curChar); } } } putKeyValueToMap(temp, isKey, key, map); } return map; } private static void putKeyValueToMap(StringBuilder temp, boolean isKey, String key, Map<String, String> map) throws UnsupportedEncodingException { if (isKey) { key = temp.toString(); if (key.length() == 0) { throw new RuntimeException("QString format illegal"); } map.put(key, ""); } else { if (key.length() == 0) { throw new RuntimeException("QString format illegal"); } map.put(key, temp.toString()); } } /** * ?NULL * * @param s * ? * @return true- false-? */ public static boolean isEmpty(String s) { return null == s || "".equals(s.trim()); } /** * * @param contentData * @return */ public static Map<String, String> filterBlank(Map<String, String> contentData) { LogUtil.writeLog("? :"); Map<String, String> submitFromData = new HashMap<String, String>(); Set<String> keyset = contentData.keySet(); for (String key : keyset) { String value = contentData.get(key); if (StringUtils.isNotBlank(value)) { // value??? submitFromData.put(key, value.trim()); LogUtil.writeLog(key + "-->" + String.valueOf(value)); } } return submitFromData; } /** * ???(SHA-1?)<br> * @param rspData ?<br> * @param encoding ?encoding<br> * @return true false <br> */ public static boolean validate(Map<String, String> rspData, String encoding) { LogUtil.writeLog("?"); if (SDKUtil.isEmpty(encoding)) { encoding = "UTF-8"; } String stringSign = rspData.get(SDKConstants.param_signature); // ?certId ????Map? String certId = rspData.get(SDKConstants.param_certId); LogUtil.writeLog("??[" + certId + "]"); // Map???key1=value1&key2=value2? String stringData = SDKUtil.coverMap2String(rspData); LogUtil.writeLog("[" + stringData + "]"); try { // ???????. return SecureUtil.validateSignBySoft(CertUtil.getValidateKey(certId), SecureUtil.base64Decode(stringSign.getBytes(encoding)), SecureUtil.sha1X16(stringData, encoding)); } catch (UnsupportedEncodingException e) { LogUtil.writeErrorLog(e.getMessage(), e); } catch (Exception e) { LogUtil.writeErrorLog(e.getMessage(), e); } return false; } }