Class Sm2Signature

java.lang.Object
cloud.opencode.base.crypto.signature.Sm2Signature
All Implemented Interfaces:
SignatureEngine

public final class Sm2Signature extends Object implements SignatureEngine
SM2 signature implementation (Chinese national cryptographic standard). SM2 签名实现(中国国家密码标准)。

Requires Bouncy Castle provider for SM2 support. 需要 Bouncy Castle 提供商支持 SM2。

Features | 主要功能:

  • SM2 signatures (Chinese national standard) - SM2 签名(中国国密标准)
  • Requires Bouncy Castle provider - 需要 Bouncy Castle 提供者

Usage Examples | 使用示例:

Sm2Signature sm2 = Sm2Signature.create();
sm2.setPrivateKey(privateKey);
byte[] sig = sm2.sign(data);

Security | 安全性:

  • Thread-safe: No - 线程安全: 否
  • Null-safe: Yes - 空值安全: 是
Since:
JDK 25, opencode-base-crypto V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    Create SM2 signature instance.
    byte[]
    Complete the multi-part signing operation 完成多部分签名操作
    Complete the multi-part signing and return Base64 signature 完成多部分签名并返回 Base64 签名
    boolean
    doVerify(byte[] signature)
    Complete the multi-part verification operation 完成多部分验证操作
    Get the signature algorithm name 获取签名算法名称
    static boolean
    Check if Bouncy Castle provider is available.
    Set both keys from a key pair 从密钥对设置公私钥
    setPrivateKey(byte[] encodedKey)
    Set the private key from encoded bytes 从编码字节设置私钥
    Set the private key for signing operations 设置用于签名操作的私钥
    Set the private key from PEM format 从 PEM 格式设置私钥
    setPublicKey(byte[] encodedKey)
    Set the public key from encoded bytes 从编码字节设置公钥
    Set the public key for verification operations 设置用于验证操作的公钥
    Set the public key from PEM format 从 PEM 格式设置公钥
    byte[]
    sign(byte[] data)
    Sign data and return signature bytes 签名数据并返回签名字节
    byte[]
    Sign data from input stream 从输入流签名数据
    byte[]
    sign(String data)
    Sign UTF-8 encoded string data 签名 UTF-8 编码的字符串数据
    signBase64(byte[] data)
    Sign data and return Base64 encoded signature 签名数据并返回 Base64 编码的签名
    Sign UTF-8 encoded string and return Base64 signature 签名 UTF-8 编码字符串并返回 Base64 签名
    byte[]
    signFile(Path file)
    Sign file content 签名文件内容
    signHex(byte[] data)
    Sign data and return hexadecimal encoded signature 签名数据并返回十六进制编码的签名
    update(byte[] data)
    Update the signature with additional data (for multi-part signing) 使用额外数据更新签名(用于多部分签名)
    update(String data)
    Update the signature with UTF-8 encoded string 使用 UTF-8 编码字符串更新签名
    boolean
    verify(byte[] data, byte[] signature)
    Verify signature for given data 验证给定数据的签名
    boolean
    verify(String data, byte[] signature)
    Verify signature for UTF-8 encoded string 验证 UTF-8 编码字符串的签名
    boolean
    verifyBase64(byte[] data, String base64Signature)
    Verify Base64 encoded signature 验证 Base64 编码的签名
    boolean
    verifyBase64(String data, String base64Signature)
    Verify Base64 encoded signature for string data 验证字符串数据的 Base64 编码签名
    boolean
    verifyFile(Path file, byte[] signature)
    Verify signature for file content 验证文件内容的签名
    boolean
    verifyHex(byte[] data, String hexSignature)
    Verify hexadecimal encoded signature 验证十六进制编码的签名

    Methods inherited from class Object

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

    • create

      public static Sm2Signature create()
      Create SM2 signature instance. 创建 SM2 签名实例。
      Returns:
      SM2 signature instance / SM2 签名实例
    • isBouncyCastleAvailable

      public static boolean isBouncyCastleAvailable()
      Check if Bouncy Castle provider is available. 检查 Bouncy Castle 提供商是否可用。
      Returns:
      true if available / 如果可用则返回 true
    • setPrivateKey

      public SignatureEngine setPrivateKey(PrivateKey privateKey)
      Description copied from interface: SignatureEngine
      Set the private key for signing operations 设置用于签名操作的私钥
      Specified by:
      setPrivateKey in interface SignatureEngine
      Parameters:
      privateKey - the private key
      Returns:
      this engine instance for method chaining
    • setPrivateKey

      public SignatureEngine setPrivateKey(byte[] encodedKey)
      Description copied from interface: SignatureEngine
      Set the private key from encoded bytes 从编码字节设置私钥
      Specified by:
      setPrivateKey in interface SignatureEngine
      Parameters:
      encodedKey - encoded private key bytes
      Returns:
      this engine instance for method chaining
    • setPrivateKeyPem

      public SignatureEngine setPrivateKeyPem(String pem)
      Description copied from interface: SignatureEngine
      Set the private key from PEM format 从 PEM 格式设置私钥
      Specified by:
      setPrivateKeyPem in interface SignatureEngine
      Parameters:
      pem - PEM formatted private key
      Returns:
      this engine instance for method chaining
    • setPublicKey

      public SignatureEngine setPublicKey(PublicKey publicKey)
      Description copied from interface: SignatureEngine
      Set the public key for verification operations 设置用于验证操作的公钥
      Specified by:
      setPublicKey in interface SignatureEngine
      Parameters:
      publicKey - the public key
      Returns:
      this engine instance for method chaining
    • setPublicKey

      public SignatureEngine setPublicKey(byte[] encodedKey)
      Description copied from interface: SignatureEngine
      Set the public key from encoded bytes 从编码字节设置公钥
      Specified by:
      setPublicKey in interface SignatureEngine
      Parameters:
      encodedKey - encoded public key bytes
      Returns:
      this engine instance for method chaining
    • setPublicKeyPem

      public SignatureEngine setPublicKeyPem(String pem)
      Description copied from interface: SignatureEngine
      Set the public key from PEM format 从 PEM 格式设置公钥
      Specified by:
      setPublicKeyPem in interface SignatureEngine
      Parameters:
      pem - PEM formatted public key
      Returns:
      this engine instance for method chaining
    • setKeyPair

      public SignatureEngine setKeyPair(KeyPair keyPair)
      Description copied from interface: SignatureEngine
      Set both keys from a key pair 从密钥对设置公私钥
      Specified by:
      setKeyPair in interface SignatureEngine
      Parameters:
      keyPair - the key pair
      Returns:
      this engine instance for method chaining
    • sign

      public byte[] sign(byte[] data)
      Description copied from interface: SignatureEngine
      Sign data and return signature bytes 签名数据并返回签名字节
      Specified by:
      sign in interface SignatureEngine
      Parameters:
      data - data to sign
      Returns:
      signature bytes
    • sign

      public byte[] sign(String data)
      Description copied from interface: SignatureEngine
      Sign UTF-8 encoded string data 签名 UTF-8 编码的字符串数据
      Specified by:
      sign in interface SignatureEngine
      Parameters:
      data - string data to sign
      Returns:
      signature bytes
    • signBase64

      public String signBase64(byte[] data)
      Description copied from interface: SignatureEngine
      Sign data and return Base64 encoded signature 签名数据并返回 Base64 编码的签名
      Specified by:
      signBase64 in interface SignatureEngine
      Parameters:
      data - data to sign
      Returns:
      Base64 encoded signature
    • signBase64

      public String signBase64(String data)
      Description copied from interface: SignatureEngine
      Sign UTF-8 encoded string and return Base64 signature 签名 UTF-8 编码字符串并返回 Base64 签名
      Specified by:
      signBase64 in interface SignatureEngine
      Parameters:
      data - string data to sign
      Returns:
      Base64 encoded signature
    • signHex

      public String signHex(byte[] data)
      Description copied from interface: SignatureEngine
      Sign data and return hexadecimal encoded signature 签名数据并返回十六进制编码的签名
      Specified by:
      signHex in interface SignatureEngine
      Parameters:
      data - data to sign
      Returns:
      hexadecimal encoded signature
    • signFile

      public byte[] signFile(Path file)
      Description copied from interface: SignatureEngine
      Sign file content 签名文件内容
      Specified by:
      signFile in interface SignatureEngine
      Parameters:
      file - file to sign
      Returns:
      signature bytes
    • sign

      public byte[] sign(InputStream input)
      Description copied from interface: SignatureEngine
      Sign data from input stream 从输入流签名数据
      Specified by:
      sign in interface SignatureEngine
      Parameters:
      input - input stream to read data from
      Returns:
      signature bytes
    • verify

      public boolean verify(byte[] data, byte[] signature)
      Description copied from interface: SignatureEngine
      Verify signature for given data 验证给定数据的签名
      Specified by:
      verify in interface SignatureEngine
      Parameters:
      data - data that was signed
      signature - signature bytes
      Returns:
      true if signature is valid
    • verify

      public boolean verify(String data, byte[] signature)
      Description copied from interface: SignatureEngine
      Verify signature for UTF-8 encoded string 验证 UTF-8 编码字符串的签名
      Specified by:
      verify in interface SignatureEngine
      Parameters:
      data - string data that was signed
      signature - signature bytes
      Returns:
      true if signature is valid
    • verifyBase64

      public boolean verifyBase64(byte[] data, String base64Signature)
      Description copied from interface: SignatureEngine
      Verify Base64 encoded signature 验证 Base64 编码的签名
      Specified by:
      verifyBase64 in interface SignatureEngine
      Parameters:
      data - data that was signed
      base64Signature - Base64 encoded signature
      Returns:
      true if signature is valid
    • verifyBase64

      public boolean verifyBase64(String data, String base64Signature)
      Description copied from interface: SignatureEngine
      Verify Base64 encoded signature for string data 验证字符串数据的 Base64 编码签名
      Specified by:
      verifyBase64 in interface SignatureEngine
      Parameters:
      data - string data that was signed
      base64Signature - Base64 encoded signature
      Returns:
      true if signature is valid
    • verifyHex

      public boolean verifyHex(byte[] data, String hexSignature)
      Description copied from interface: SignatureEngine
      Verify hexadecimal encoded signature 验证十六进制编码的签名
      Specified by:
      verifyHex in interface SignatureEngine
      Parameters:
      data - data that was signed
      hexSignature - hexadecimal encoded signature
      Returns:
      true if signature is valid
    • verifyFile

      public boolean verifyFile(Path file, byte[] signature)
      Description copied from interface: SignatureEngine
      Verify signature for file content 验证文件内容的签名
      Specified by:
      verifyFile in interface SignatureEngine
      Parameters:
      file - file that was signed
      signature - signature bytes
      Returns:
      true if signature is valid
    • update

      public SignatureEngine update(byte[] data)
      Description copied from interface: SignatureEngine
      Update the signature with additional data (for multi-part signing) 使用额外数据更新签名(用于多部分签名)
      Specified by:
      update in interface SignatureEngine
      Parameters:
      data - data to add
      Returns:
      this engine instance for method chaining
    • update

      public SignatureEngine update(String data)
      Description copied from interface: SignatureEngine
      Update the signature with UTF-8 encoded string 使用 UTF-8 编码字符串更新签名
      Specified by:
      update in interface SignatureEngine
      Parameters:
      data - string data to add
      Returns:
      this engine instance for method chaining
    • doSign

      public byte[] doSign()
      Description copied from interface: SignatureEngine
      Complete the multi-part signing operation 完成多部分签名操作
      Specified by:
      doSign in interface SignatureEngine
      Returns:
      signature bytes
    • doSignBase64

      public String doSignBase64()
      Description copied from interface: SignatureEngine
      Complete the multi-part signing and return Base64 signature 完成多部分签名并返回 Base64 签名
      Specified by:
      doSignBase64 in interface SignatureEngine
      Returns:
      Base64 encoded signature
    • doVerify

      public boolean doVerify(byte[] signature)
      Description copied from interface: SignatureEngine
      Complete the multi-part verification operation 完成多部分验证操作
      Specified by:
      doVerify in interface SignatureEngine
      Parameters:
      signature - signature to verify
      Returns:
      true if signature is valid
    • getAlgorithm

      public String getAlgorithm()
      Description copied from interface: SignatureEngine
      Get the signature algorithm name 获取签名算法名称
      Specified by:
      getAlgorithm in interface SignatureEngine
      Returns:
      algorithm name