wzy
2021-01-26 a918381d1910d16f991d09188341ff63ed13d4f1
zq-erp/src/main/java/com/matrix/component/wechat/externalInterface/common/Signature.java
@@ -22,7 +22,7 @@
    * @return 签名
    * @throws IllegalAccessException
    */
   public static String getSign(Object o) throws IllegalAccessException {
   public static String getSign(Object o,String paySecret) throws IllegalAccessException {
      ArrayList<String> list = new ArrayList<String>();
      Class<? extends Object> cls = o.getClass();
      Field[] fields = cls.getDeclaredFields();
@@ -40,14 +40,14 @@
         sb.append(arrayToSort[i]);
      }
      String result = sb.toString();
      result += "key=" + WechatConfigure.key;
      result += "key=" +paySecret;
      Util.log("Sign Before MD5:" + result);
      result = MD5.MD5Encode(result).toUpperCase();
      Util.log("Sign Result:" + result);
      return result;
   }
   public static String getSign(Map<String, Object> map) {
   public static String getSign(Map<String, Object> map,String paySecret) {
      ArrayList<String> list = new ArrayList<String>();
      for (Map.Entry<String, Object> entry : map.entrySet()) {
         if (entry.getValue() != "") {
@@ -62,7 +62,7 @@
         sb.append(arrayToSort[i]);
      }
      String result = sb.toString();
      result += "key=" + WechatConfigure.key;
      result += "key=" + paySecret;
      Util.log("Sign Before MD5:" + result);
      result = MD5.MD5Encode(result).toUpperCase();
      Util.log("Sign Result:" + result);
@@ -79,13 +79,13 @@
    * @throws IOException
    * @throws SAXException
    */
   public static String getSignFromResponseString(String responseString)
   public static String getSignFromResponseString(String responseString,String paySecret)
         throws IOException, SAXException, ParserConfigurationException {
      Map<String, Object> map = XMLParser.getMapFromXML(responseString);
      // 清掉返回数据对象里面的Sign数据(不能把这个数据也加进去进行签名),然后用签名算法进行签名
      map.put("sign", "");
      // 将API返回的数据根据用签名算法进行计算新的签名,用来跟API返回的签名进行比较
      return Signature.getSign(map);
      return Signature.getSign(map,paySecret);
   }
   /**
@@ -98,7 +98,7 @@
    * @throws IOException
    * @throws SAXException
    */
   public static boolean checkIsSignValidFromResponseString(String responseString)
   public static boolean checkIsSignValidFromResponseString(String responseString,String paySecret)
         throws ParserConfigurationException, IOException, SAXException {
      Map<String, Object> map = XMLParser.getMapFromXML(responseString);
@@ -113,7 +113,7 @@
      // 清掉返回数据对象里面的Sign数据(不能把这个数据也加进去进行签名),然后用签名算法进行签名
      map.put("sign", "");
      // 将API返回的数据根据用签名算法进行计算新的签名,用来跟API返回的签名进行比较
      String signForAPIResponse = Signature.getSign(map);
      String signForAPIResponse = Signature.getSign(map,paySecret);
      if (!signForAPIResponse.equals(signFromAPIResponse)) {
         // 签名验不过,表示这个API返回的数据有可能已经被篡改了
@@ -124,9 +124,7 @@
      return true;
   }
   
   public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException {
      System.out.println(checkIsSignValidFromResponseString("<xml><appid><![CDATA[wx13515a9204ea669d]]></appid><attach><![CDATA[35f7170585ad4f09b5109cc985b1eec0]]></attach><bank_type><![CDATA[CFT]]></bank_type><cash_fee><![CDATA[1]]></cash_fee><fee_type><![CDATA[CNY]]></fee_type><is_subscribe><![CDATA[N]]></is_subscribe><mch_id><![CDATA[1486409192]]></mch_id><nonce_str><![CDATA[elqhnr10vzaq0wgalf9njjyqwyd7gyr7]]></nonce_str><openid><![CDATA[o8x8E5lqmD9c5rdsjTQ9KWN4-H40]]></openid><out_trade_no><![CDATA[2018090610284198419]]></out_trade_no><result_code><![CDATA[SUCCESS]]></result_code><return_code><![CDATA[SUCCESS]]></return_code><sign><![CDATA[DD04172CDAA60F834163AF3F5B63BE3F]]></sign><time_end><![CDATA[20180906102853]]></time_end><total_fee>1</total_fee><trade_type><![CDATA[JSAPI]]></trade_type><transaction_id><![CDATA[4200000169201809067369627940]]></transaction_id></xml>"));
   }
   /**
    *