签名计算规则
<h5>简要描述:</h5>
<ul>
<li>参数按照字段名的ascii码从小到大排序后,使用QueryString的格式(即key1=value1&key2=value2…)拼接成字符串后,空值不传递,不参与签名组串。再到后面追加上(&商户key值),用md5进行加密成32位小写字符串。<font color='red'>注意:(sign、appid、分页请求参数默认不参与加密)需要参与加密的参数在接口请求参数中有写明</font></li>
</ul>
<h5>例子:</h5>
<p>商户id为:<font color='red'>10000</font>
商户key值为:<font color='red'>Az6c01f185-440e-44a8-9d41-ceOpen</font></p>
<h5>假设请求参数为:</h5>
<pre><code class="language-json">{
&quot;appId&quot;: &quot;10088581615&quot;,
&quot;sign&quot;: &quot;82a4da7502872e7a128b510d10cad6ab&quot;,
&quot;status&quot;: &quot;1&quot;,
&quot;timeBegin&quot;: &quot;2025-03-17 11:36:05&quot;,
&quot;timeEnd&quot;: &quot;2025-03-29 11:36:05&quot;
}</code></pre>
<h5>验签步骤:</h5>
<ol>
<li><strong>拼接字符串如下</strong></li>
</ol>
<pre><code class="language-javascript">status=1&amp;timeBegin=2025-03-17 11:36:05&amp;timeEnd=2025-03-29 11:36:05</code></pre>
<ol>
<li><strong>尾部追加上 商户key 的MD5源串如下(注意这是签名的拼接明文不是提交数据):</strong></li>
</ol>
<pre><code class="language-javascript">status=1&amp;timeBegin=2025-03-17 11:36:05&amp;timeEnd=2025-03-29 11:36:05&amp;Az6c01f185-440e-44a8-9d41-ceOpen</code></pre>
<ol>
<li>MD5加密后转成32位小写的签名值如下:</li>
</ol>
<pre><code class="language-javascript">921bfe2dcd4ecca574d8b8c76412ede5</code></pre>
<h5>JAVA代码示例:</h5>
<pre><code class="language-java">public static String generateSignature(Map&lt;String, String&gt; params, String merchantKey) {
// 1. 参数排序
TreeMap&lt;String, String&gt; sortedParams = new TreeMap&lt;&gt;(params);
// 2. 拼接QueryString,忽略空值
StringBuilder queryString = new StringBuilder();
for (Map.Entry&lt;String, String&gt; entry : sortedParams.entrySet()) {
if (entry.getValue() != null &amp;&amp; !entry.getValue().isEmpty()) {
if (queryString.length() &gt; 0) {
queryString.append(&quot;&amp;&quot;);
}
queryString.append(entry.getKey()).append(&quot;=&quot;).append(entry.getValue());
}
}
// 3. 追加商户key
queryString.append(&quot;&amp;&quot;).append(merchantKey);
// 4. MD5加密
return md5Hash(queryString.toString());
}
private static String md5Hash(String input) {
try {
MessageDigest md = MessageDigest.getInstance(&quot;MD5&quot;);
byte[] messageDigest = md.digest(input.getBytes());
StringBuilder hexString = new StringBuilder();
for (byte b : messageDigest) {
String hex = Integer.toHexString(0xff &amp; b);
if (hex.length() == 1) {
hexString.append(&#039;0&#039;);
}
hexString.append(hex);
}
return hexString.toString();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}</code></pre>