# 数据签名与验签

相关源码开源地址：研发人员加班打包创建中...

公私钥文章请查看：

{% content-ref url="/pages/bYvBNOefqFCMpnB2Ruc6" %}
[钱包地址、公私钥生成算法](/index/kai-fa-zhi-nan/qian-bao-di-zhi-gong-si-yao-sheng-cheng-suan-fa.md)
{% endcontent-ref %}

### 数据签名

```java
/**
 * 通过私钥字节进行签名
 * @param data 待签名数据
 * @param privateKey 私钥字节
 * @return byte[] 数字签名
 * @throws Exception 异常
 */
public static byte[] sign(byte[] data, byte[] privateKey) throws Exception {
    PrivateKey priKey = ECDSAUtils.getPrivateKey(privateKey);
    // 实例化Signature
    Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM, BouncyCastleProvider.PROVIDER_NAME);
    // 初始化Signature
    signature.initSign(priKey);
    // 更新
    signature.update(data);
    // 签名
    return signature.sign();
}

```

### 签名数据验签

```java
/**
 * 通过公钥字节进行验签
 * @param data 待校验数据
 * @param publicKey 公钥字节
 * @param sign 数字签名-通过sign接口签名后的字节数据
 * @return boolean 校验成功返回true 失败返回false
 * @throws Exception 异常
 *
 */
public static boolean verify(byte[] data, byte[] publicKey, byte[] sign) throws Exception {
    //获取公钥
    PublicKey pubKey = ECDSAUtils.getPublicKey(publicKey);
    // 实例化Signature
    Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM, BouncyCastleProvider.PROVIDER_NAME);
    // 初始化Signature
    signature.initVerify(pubKey);
    // 更新
    signature.update(data);
    // 验证
    return signature.verify(sign);
}
```


---

# 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://datacbc.gitbook.io/index/kai-fa-zhi-nan/shu-ju-qian-ming-yu-yan-qian.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.
