新网开放接口 Help

签名算法

认识 API

新网每个API接口请求参数类型统一通过 Body 传递Json格式数据, 请求包含认证信息(Xn-PublicKey、Xn-Signature), 且Body参数中除了各接口独有的参数外,必须传递 authTimestamp(当前时间戳) 用于确认接口送达网关的时间是否超时,如果请求发起至新网接口网关超过10秒, 接口网关会拒绝执行并响应超时错误,如果使用PHP作为开发语言,您可能需要对时间戳增加3个0处理。

公共参数

公共参数是在操作所有 API 的时候都必需给出的参数

参数名

位置

类型

必选

说明

Xn-PublicKey

header

string

是

用户公钥

Xn-Signature

header

string

是

通过私钥以及参数进行加密得出的签名串

authTimestamp

body

string

是

请求发起的时间戳

签名计算

接口调用时需要根据私钥(Xn-PrivateKey )通过HMAC-sha256算法对请求体body中的json参数整体进行签名得到(* Xn-Signature*) 本例中假设

Xn-PublicKey = "aa2470d8-998b-11ee-9299-33ae3c036dfe" Xn-PrivateKey = "ad8a5e0e-998b-11ee-a724-0f663880d3f4"

请求参数为

{ "authTimestamp": "1702452601870", "instanceCode": "08dfaf63fa1049e1ae1c6569bd3cd453" }

那么我们需要将整个json串以私钥进行HMAC-sha256签名计算 (请求的json串需要去除美化的空格、回车等) ,通过计算可得出签名( Xn-Signature) (可以此作为签名算法编写测试,如果得出结果一致,表示签名算法逻辑正确)

94c8e38acddbbe0618888c2a1856f9765f00fc2a979090cf27aed963c86025a1

Java代码签名请求示例(基于RestTemplate和hutool实现):

import cn.hutool.crypto.digest.HmacAlgorithm; import cn.hutool.crypto.digest.HMac; import cn.hutool.json.JSONUtil; import org.springframework.http.*; import org.springframework.web.client.RestTemplate; import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; public class RestTemplateHmacExample { public static void main(String[] args) { // 请求地址 String url = "https://x-api.xinnet.com/xcloud/trade/v1/host/list"; // 公钥和私钥 String publicKey = "aa2470d8-998b-11ee-9299-33ae3c036dfe"; String privateKey = "ad8a5e0e-998b-11ee-a724-0f663880d3f4"; // 请求参数(带时间戳) long timestamp = System.currentTimeMillis(); Map<String, Object> params = new HashMap<>(); params.put("authTimestamp", String.valueOf(timestamp)); // 将参数转成 JSON 字符串(Hutool) String jsonBody = JSONUtil.toJsonStr(params); // 使用 HMAC-SHA256 生成签名(Hutool) HMac hmac = new HMac(HmacAlgorithm.HmacSHA256, privateKey.getBytes(StandardCharsets.UTF_8)); String signature = hmac.digestHex(jsonBody); // 设置请求头 HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); headers.set("Xn-PublicKey", publicKey); headers.set("Xn-Signature", signature); // 构建请求体 HttpEntity<String> request = new HttpEntity<>(jsonBody, headers); // 发送请求 RestTemplate restTemplate = new RestTemplate(); ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class); // 输出结果 System.out.println("Status Code: " + response.getStatusCode()); System.out.println("Body: " + response.getBody()); } }

Java代码签名请求示例:

public static void main(String[] args) { String url = "https://x-api.xinnet.com/xcloud/trade/v1/host/list"; String publicKey = "aa2470d8-998b-11ee-9299-33ae3c036dfe"; String privateKey = "ad8a5e0e-998b-11ee-a724-0f663880d3f4"; Map<String, Object> header = new HashMap<>(); Map<String, Object> params = new HashMap<>(); params.put("authTimestamp", "1702452601870"); header.put("Xn-PublicKey", publicKey); header.put("Xn-Signature", HMAC.sha256(privateKey, JsonUtil.toJson(params))); String result; try { result = HttpClient.request(url).header(header).post(JsonUtil.toJson(params)).asString(); } catch (IOException e) { throw new RuntimeException(e); } System.out.println(result); }

Python3代码签名请求示例:

import requests import hmac import hashlib import json import time def sha256_signature(private_key, message): message_bytes = message.encode('utf-8') key_bytes = private_key.encode('utf-8') return hmac.new(key_bytes, message_bytes, digestmod=hashlib.sha256).hexdigest() if __name__ == '__main__': public_key = "aa2470d8-998b-11ee-9299-33ae3c036dfe" private_key = "ad8a5e0e-998b-11ee-a724-0f663880d3f4" params = { "authTimestamp": str(int(time.time() * 1000)) } headers = { "Xn-PublicKey": public_key, "Xn-Signature": sha256_signature(private_key, json.dumps(params)) } try: response = requests.post("https://x-api.xinnet.com/xcloud/trade/v1/host/list", headers=headers, json=params) result = response.text print(result) except requests.RequestException as e: raise RuntimeError(e)
Last modified: 25 April 2026