Package cn.gmkit.sm9

Class SM9Signature

java.lang.Object
cn.gmkit.sm9.SM9Signature
All Implemented Interfaces:
AutoCloseable

public final class SM9Signature extends Object implements AutoCloseable
SM9 签名 / 验签上下文,支持流式 update 处理大数据。

创建时通过 doSign 指定模式:true 为签名,false 为验签。 使用完毕后应调用 close() 释放 native 资源(推荐使用 try-with-resources)。

典型用法


 try (SM9Signature signer = new SM9Signature(true)) {
     signer.update(data);
     byte[] sig = signer.sign(signKey);
 }
 try (SM9Signature verifier = new SM9Signature(false)) {
     verifier.update(data);
     boolean ok = verifier.verify(sig, masterPublicKey, id);
 }
 
  • Constructor Summary

    Constructors
    Constructor
    Description
    SM9Signature(boolean doSign)
    创建签名或验签上下文。
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    释放底层 native 资源。重复调用安全。
    void
    reset(boolean doSign)
    重置上下文以复用,重新进入签名或验签模式。
    byte[]
    sign(SM9SignKey signKey)
    使用用户签名私钥完成签名,输出 DER 编码的签名值。
    update(byte[] data)
    追加待处理数据。
    update(byte[] data, int offset, int length)
    追加待处理数据的指定区间。
    boolean
    verify(byte[] signature, SM9SignMasterKey masterPublicKey, String id)
    使用公开主密钥与用户标识完成验签。

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • SM9Signature

      public SM9Signature(boolean doSign)
      创建签名或验签上下文。
      Parameters:
      doSign - true 创建签名上下文,false 创建验签上下文
      Throws:
      SM9Exception - native 不可用或上下文初始化失败时抛出
  • Method Details

    • reset

      public void reset(boolean doSign)
      重置上下文以复用,重新进入签名或验签模式。
      Parameters:
      doSign - true 重置为签名模式,false 重置为验签模式
      Throws:
      SM9Exception - 上下文已关闭或 native 初始化失败时抛出
    • update

      public SM9Signature update(byte[] data)
      追加待处理数据。
      Parameters:
      data - 数据
      Returns:
      当前上下文,便于链式调用
      Throws:
      SM9Exception - 数据为空、范围无效、上下文已关闭或 native 更新失败时抛出
    • update

      public SM9Signature update(byte[] data, int offset, int length)
      追加待处理数据的指定区间。
      Parameters:
      data - 数据缓冲
      offset - 起始偏移
      length - 长度
      Returns:
      当前上下文,便于链式调用
      Throws:
      SM9Exception - 缓冲区范围无效、上下文已关闭或 native 更新失败时抛出
    • sign

      public byte[] sign(SM9SignKey signKey)
      使用用户签名私钥完成签名,输出 DER 编码的签名值。
      Parameters:
      signKey - 用户签名私钥
      Returns:
      签名值
      Throws:
      SM9Exception - 私钥为空、上下文已关闭或 native 签名失败时抛出
    • verify

      public boolean verify(byte[] signature, SM9SignMasterKey masterPublicKey, String id)
      使用公开主密钥与用户标识完成验签。
      Parameters:
      signature - 待验证签名值
      masterPublicKey - 公开主密钥
      id - 签名者用户标识
      Returns:
      验证通过返回 true
      Throws:
      SM9Exception - 签名、主公钥或 ID 无效,或上下文已关闭时抛出
    • close

      public void close()
      释放底层 native 资源。重复调用安全。
      Specified by:
      close in interface AutoCloseable