{"id":1591,"date":"2023-03-25T19:55:37","date_gmt":"2023-03-25T11:55:37","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1591"},"modified":"2023-04-27T21:28:11","modified_gmt":"2023-04-27T13:28:11","slug":"wechat-pay-hmac-sha256-add-verify-signature","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/25\/wechat-pay-hmac-sha256-add-verify-signature\/","title":{"rendered":"\u5fae\u4fe1\u652f\u4ed8HMAC-SHA256\u52a0\u89e3\u7b7e\u91c7\u5751\u8bb0\u5f55"},"content":{"rendered":"<h3>sign_type\u4e5f\u8981\u53c2\u4e0e\u52a0\u7b7e<\/h3>\n<pre><code>appid=wx28d9f58adc1a2f8e&amp;attach=pay-test&amp;body=native-pay-test&amp;fee_type=THB&amp;mch_id=118875980&amp;nonce_str=20201201114940636&amp;notify_url=https:\/\/www.yezhou.cc\/callback\/notify.php&amp;out_trade_no=wx20201201114940636&amp;sign_type=HMAC-SHA256&amp;sub_mch_id=516669865&amp;time_expire=20201202114940&amp;time_start=20201201114940&amp;total_fee=280&amp;trade_type=NATIVE<\/code><\/pre>\n<p><!-- more --><\/p>\n<h3>\u52a0\u7b7e\u4e32\u9700\u8981\u6dfb\u52a0 &amp;key=xxx<\/h3>\n<pre><code class=\"language-java\">public static String hmacSHA256(String data, String key, String charset) throws Exception {\n    String content = String.format(&quot;%s&amp;key=%s&quot;, data, key);\n    Mac sha256Hmac = Mac.getInstance(&quot;HmacSHA256&quot;);\n    SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(charset), &quot;HmacSHA256&quot;);\n    sha256Hmac.init(secretKey);\n    byte[] array = sha256Hmac.doFinal(content.getBytes(charset));\n    StringBuilder sb = new StringBuilder();\n    for (byte item : array) {\n        sb.append(Integer.toHexString((item &amp; 0xFF) | 0x100).substring(1, 3));\n    }\n    return sb.toString();\n}\n\npublic static boolean verifyHmacSHA256(String text, String sign, String key, String charset) throws Exception {\n    String mySign = hmacSHA256(text, key, charset);\n    if (mySign.equalsIgnoreCase(sign)) {\n        return true;\n    } else {\n        return false;\n    }\n}<\/code><\/pre>\n<h3>\u9a8c\u7b7e\u5b57\u6bb5\u662f\u4e0b\u5212\u7ebf\u5f0f<\/h3>\n<p>\u9a8c\u7b7e\u5b57\u6bb5\u662f\u4e0b\u5212\u7ebf\u5f0f\uff0c\u800c\u975e\u9a7c\u5cf0\u5f0f\uff0c\u5e8f\u5217\u5316\u53ca\u53cd\u5e8f\u5217\u5316\u65f6\u5e94\u6ce8\u610f<\/p>\n<p>\u9519\u8bef\u9a8c\u7b7e\u4e32\uff1a<\/p>\n<pre><code>appid=wx28d9f58adc1a2f8e&amp;attach=product_code=WECHAT_PAY|payment_type=QRCODE&amp;bankType=OTHERS&amp;cashFee=3&amp;cashFeeType=CNY&amp;deviceInfo=appblog.cn&amp;feeType=THB&amp;isSubscribe=Y&amp;mchId=118875980&amp;nonceStr=01422887685418391183670693845884&amp;openid=oaJiE1PwlwP_Tee0Z7uzrQFJeRqE&amp;outTradeNo=132020120102277089&amp;rateValue=21830000&amp;resultCode=SUCCESS&amp;returnCode=SUCCESS&amp;subMchId=516669865&amp;timeEnd=20201201135626&amp;totalFee=15&amp;tradeType=NATIVE&amp;transactionId=4200000817202012015050164846<\/code><\/pre>\n<p>\u6b63\u786e\u9a8c\u7b7e\u4e32\uff1a<\/p>\n<pre><code>appid=wx28d9f58adc1a2f8e&amp;attach=product_code=WECHAT_PAY|payment_type=QRCODE&amp;bank_type=OTHERS&amp;cash_fee=3&amp;cash_fee_type=CNY&amp;device_info=appblog.cn&amp;fee_type=THB&amp;is_subscribe=Y&amp;mch_id=118875980&amp;nonce_str=01422887685418391183670693845884&amp;openid=oaJiE1PwlwP_Tee0Z7uzrQFJeRqE&amp;out_trade_no=132020120102277089&amp;rate_value=21830000&amp;result_code=SUCCESS&amp;return_code=SUCCESS&amp;sub_mch_id=516669865&amp;time_end=20201201135626&amp;total_fee=15&amp;trade_type=NATIVE&amp;transaction_id=4200000817202012015050164846<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>sign_type\u4e5f\u8981\u53c2\u4e0e\u52a0\u7b7e appid=wx28d9f58adc1a2f8e&amp;attach=pay [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[371],"tags":[190],"class_list":["post-1591","post","type-post","status-publish","format-standard","hentry","category-wechat-pay","tag-hmac-sha"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1591","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1591"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1591\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1591"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1591"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1591"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}