Package cn.gmkit.sm9

Class SM9

java.lang.Object
cn.gmkit.sm9.SM9

public final class SM9 extends Object
SM9 门面工具类,提供一次性签名、验签与 IBE 加解密入口。

该类封装了 SM9Signature 上下文的创建与释放,适合“一次性”短数据场景; 对大数据流式处理或需要复用上下文的场景,请直接使用 SM9Signature

能力范围

与 GmSSL 一致,SM9 仅支持 签名 / 验签加密 / 解密(IBE)不支持密钥交换

可用性

所有方法都依赖 native 库;可先通过 isAvailable() 判断当前平台是否支持。
  • Method Details

    • isAvailable

      public static boolean isAvailable()
      判断当前平台的 SM9 native 库是否已成功加载。
      Returns:
      已加载返回 true
    • nativeVersion

      public static String nativeVersion()
      返回 native 桥接版本标识。
      Returns:
      版本标识
    • nativePlatform

      public static String nativePlatform()
      返回当前运行环境对应的 native 平台标识,例如 linux-x86_64

      如果操作系统或 CPU 架构不在支持范围内,返回 unsupported,可结合 nativeLoadErrorMessage() 查看具体原因。

      Returns:
      native 平台标识
    • nativeLoadErrorMessage

      public static String nativeLoadErrorMessage()
      返回 native 库加载失败的诊断消息。
      Returns:
      加载失败消息;native 已可用时返回 null
    • generateSignMasterKey

      public static SM9SignMasterKey generateSignMasterKey()
      生成 SM9 签名主密钥。
      Returns:
      签名主密钥
      Throws:
      SM9Exception - native 不可用或生成操作失败时抛出
    • generateEncMasterKey

      public static SM9EncMasterKey generateEncMasterKey()
      生成 SM9 加密主密钥。
      Returns:
      加密主密钥
    • extractSignKey

      public static SM9SignKey extractSignKey(SM9SignMasterKey master, String id)
      派生用户签名私钥。
      Parameters:
      master - 签名主密钥
      id - 用户标识
      Returns:
      用户签名私钥
      Throws:
      SM9Exception - master 为空、ID 为空或 native 派生失败时抛出
    • extractEncKey

      public static SM9EncKey extractEncKey(SM9EncMasterKey master, String id)
      派生用户解密私钥。
      Parameters:
      master - 加密主密钥
      id - 用户标识
      Returns:
      用户解密私钥
      Throws:
      SM9Exception - master 为空、ID 为空或 native 派生失败时抛出
    • sign

      public static byte[] sign(SM9SignKey signKey, byte[] data)
      使用用户签名私钥对数据进行签名。
      Parameters:
      signKey - 用户签名私钥
      data - 待签名数据
      Returns:
      签名值
      Throws:
      SM9Exception - 参数为空、native 不可用或签名失败时抛出
    • verify

      public static boolean verify(SM9SignMasterKey masterPublicKey, String id, byte[] data, byte[] signature)
      使用公开主密钥与用户标识验证签名。
      Parameters:
      masterPublicKey - 公开主密钥
      id - 签名者用户标识
      data - 原始数据
      signature - 待验证签名值
      Returns:
      验证通过返回 true
      Throws:
      SM9Exception - 主公钥、数据或签名为空,或用户标识无效时抛出
    • encrypt

      public static byte[] encrypt(SM9EncMasterKey masterPublicKey, String id, byte[] plaintext)
      使用公开主密钥向指定用户标识加密明文。
      Parameters:
      masterPublicKey - 公开主密钥
      id - 接收方用户标识
      plaintext - 明文,长度不得超过 SM9EncMasterKey.MAX_PLAINTEXT_SIZE
      Returns:
      密文
      Throws:
      SM9Exception - 主密钥为空、ID 无效、明文为空/超长或 native 加密失败时抛出
    • decrypt

      public static byte[] decrypt(SM9EncKey encKey, byte[] ciphertext)
      使用用户解密私钥解密密文。
      Parameters:
      encKey - 用户解密私钥
      ciphertext - 密文
      Returns:
      明文
      Throws:
      SM9Exception - 私钥为空、密文为空/超长或 native 解密失败时抛出