# 签名规则

## 签名说明

API 请求在通过 internet 传输的过程中极有可能被篡改，为了确保请求未被更改，除公共接口（基础信息，行情数据）外的私有接口均必须使用您的 API Key 做签名认证，以校验参数或参数值在传输途中是否发生了更改。每一个API Key需要有适当的权限才能访问相应的接口。每个新创建的API Key都需要分配权限。权限类型分为：读取，交易。在使用接口前，请查看每个接口的权限类型，并确认你的API Key有相应的权限。

一个合法的请求由以下几部分组成：

* 方法请求地址：即访问服务器地址 api.huobi.pro，比如 api.huobi.pro/v1/order/orders。
* API 访问密钥（AccessKeyId）：您申请的 API Key 中的 Access Key。
* 时间戳（Timestamp）：您发出请求的时间 (UTC 时区) (UTC 时区) (UTC 时区) 。如：2017-05-11T16:22:06。在查询请求中包含此值有助于防止第三方截取您的请求。
* 参数：所有在QueryString中的参数参与签名验证。
  * 对于Get请求：所有的参数都在请求的QueryString中。
  * 对于Post请求：仅key,sign,timestamp在QueryString中，其他以json格式存放于请求的Body中。（存在Body中的参数不参与签名校验）
* 签名：签名计算得出的值，用于确保签名有效和未被篡改。

{% hint style="info" %}
对于需要签名的请求，请求的QueryString中必须带以下三个参数：

key， 用户申请的apikey。

timestamp，出于安全考虑，用户需要带linux timestamp。

sign，按照签名步骤生成的签名。
{% endhint %}

## 签名步骤

为了方便说明，以下例子假设申请的ApiKey如下：

```
Api Key: 050a553410ea46079a317e04451fdae4
Api Secret: dc76d6292de3481fa43ece65e875c027
```

### 1，获取计算签名的参数

请求的QueryString中获取用于签名计算的参数，假设请求地址为：<https://openapi.moorbit.com/api/v1/orders?orderid=234234234324\\&key=050a553410ea46079a317e04451fdae4\\&timestamp=1568955510>

那么，用于签名的参数为：

| 参数名       | 参数值                              |
| --------- | -------------------------------- |
| orderid   | 234234234324                     |
| key       | 050a553410ea46079a317e04451fdae4 |
| timestamp | 1568955510                       |

### 2，对用于签名的参数进行排序

对所有参数，按&#x7167;***参数名***&#x8FDB;行升序排序，以步骤1为例，参数排序结果为：

| 参数名       | 参数值                              |
| --------- | -------------------------------- |
| key       | 050a553410ea46079a317e04451fdae4 |
| orderid   | 234234234324                     |
| timestamp | 1568955510                       |

### 3，将排序后的参数转为字符串

对于排序后的字符按照 key1=value1\&key2=value2\&key3=value3的方式转换为字符串，对于步骤2的表格转换后的结果如下：

```
key=050a553410ea46079a317e04451fdae4&orderid=234234234324&timestamp=1568955510
```

### 4，使用HmacSHA256计算签名

对步骤3的结果计算HmacSHA256，计算HmacSHA256时Secret使用于ApiKey配对的Secret，结果如下：

```
dea39da7a2574af488f2c80c54f3ab8e1f0bfff821ea394992dc559ca6ede438
```

{% hint style="info" %}
HmacSHA256调试时可以使用在线生成器验证：

&#x20;<https://www.freeformatter.com/hmac-generator.html>
{% endhint %}

### 5，将签名添加到QueryString中

将步骤4生成的签名，添加到请求的Url中，结果如下：

```
https://openapi.moorbit.com/api/v1/orders?orderid=234234234324&key=050a553410ea46079a317e04451fdae4&timestamp=1568955510&sign=dea39da7a2574af488f2c80c54f3ab8e1f0bfff821ea394992dc559ca6ede438
```


---

# Agent Instructions: 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://moorbit-1.gitbook.io/zh-cn/introduction/sign-rules.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.
