> For the complete documentation index, see [llms.txt](https://docs.supefina.net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.supefina.net/huan-ying-shi-yong-supefina-de-api-wen-dang/qian-ming-suan-fa.md).

# 签名算法

## 签名生成步骤

1. 设所有发送或者接收到的数据为集合M，将集合M内非空参数值的参数按照参数名ASCII码从小到大排序（字典序），使用URL键值对的格式（即key1=value1\&key2=value2…）拼接成字符串stringA。
2. 在stringA最后拼接上key（商户密钥）得到stringSignTemp字符串，并对stringSignTemp进行MD5运算，再将得到的字符串所有字符转换为大写，得到sign值signValue。

{% hint style="info" %}
**注意**

1. 参数名ASCII码从小到大排序（字典序）；
2. 如果参数的值为空不参与签名；
3. 参数名区分大小写；
4. 验证调用返回或支付系统主动通知签名时，数据中sign参数不参与签名，将生成的签名与该sign值作校验。
5. 支付接口可能增加字段，验证签名时必须支持增加的扩展字段。
6. 签名时商户密钥使用设置时的明文（未绑定谷歌验证码可能导致商户后台密钥显示密文）
   {% endhint %}

## 示例

1. 例如请求参数如下：

```json
 {
  "countryId": "COL",
  "currency": "COP",
  "customerAccount": "3720000264",
  "merId": "8301000002750275",
  "merOrderNo": "merOrderNo",
  "nonceStr": "string",
  "orderAmount": "30000",
  "payProduct": "08",
  "nonceStr": "4cKcL83FIsDgjAi"
}
```

2. 根据规则拼接字符串得到待签名的字符串：

```
countryId=COL&currency=COP&customerAccount=3720000264&merId=8301000002750275&merOrderNo=merOrderNo&nonceStr=4cKcL83FIsDgjAi&orderAmount=30000&payProduct=08&key=11111111111111111111111111111111
```

3. 最终签名结果

```
1DD2448C750D92B3AE512F2E493F5665
```

4. 最终请求参数

```
 {
  "countryId": "COL",
  "currency": "COP",
  "customerAccount": "3720000264",
  "merId": "8301000002750275",
  "merOrderNo": "merOrderNo",
  "nonceStr": "string",
  "orderAmount": "30000",
  "payProduct": "08",
  "nonceStr": "4cKcL83FIsDgjAi",
  "sign": "1DD2448C750D92B3AE512F2E493F5665"
}
```

## 签名SDK使用

1. SDK下载见[“字典资源＞下载”-“签名SDK”](/huan-ying-shi-yong-supefina-de-api-wen-dang/zi-dian-he-zi-yuan/xia-zai.md#qian-ming-sdk)
2. 将“supefina-sign”jar包引入自己的项目
3. 通过com.supefina.sign.SupefinaSignUtils#sign(java.lang.Object,java.lang.String)方法完成签名操作。（参数1：请求参数json对象；参数2：商户密钥）

## 签名Demo

```java
public static String sign(Object data, String key) {
        return sign(JSON.parseObject(JSONObject.toJSONString(data)), key);
    }

public static String sign(Map<String, Object> data, String key) {
        data.remove("sign");
        String signedValue = getSignedValue(data);
        signedValue += "key=" + key;
        log.info("signedValue:{}", signedValue);
        return md5(signedValue, "UTF-8").toUpperCase();
    }

private static String getSignedValue(Map<String, Object> reqMap) {
        Map<String, String> copy = new TreeMap<>();
        reqMap.forEach((k, v) -> {
            if (v != null && !"".equals(v)) {
                copy.put(k, v.toString());
            }
        });
        StringBuilder sb = new StringBuilder();
        copy.forEach((k, v) -> {
            if (v != null) {
                sb.append(k).append("=").append(v).append("&");
            }
        });
        return sb.toString();
    }
```

## 验签demo

```java
String callbackData = "{\n" + "  \"countryId\": \"COL\",\n" + "  \"currency\": \"COP\",\n"
+ "  \"customerAccount\": \"3720000264\",\n" + "  \"merId\": \"8301000002750275\",\n"
+ "  \"merOrderNo\": \"merOrderNo\",\n" + "  \"nonceStr\": \"string\",\n"
+ "  \"orderAmount\": \"30000\",\n" + "  \"payProduct\": \"08\",\n"
+ "  \"nonceStr\": \"4cKcL83FIsDgjAi\",\n" + "  \"sign\": \"1DD2448C750D92B3AE512F2E493F5665\"\n" + "}";
JSONObject data = JSON.parseObject(callbackData);

String sign = data.get("sign").toString();
data.remove("sign");
String signValue = SupefinaSignUtils.sign(data, "商户key");
if (Objects.equals(sign, signValue)) {
    // 验证签名通过
} else {
    // 失败
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.supefina.net/huan-ying-shi-yong-supefina-de-api-wen-dang/qian-ming-suan-fa.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
