Package cn.gmkit.sm9
Class SM9
java.lang.Object
cn.gmkit.sm9.SM9
SM9 门面工具类,提供一次性签名、验签与 IBE 加解密入口。
该类封装了 SM9Signature 上下文的创建与释放,适合“一次性”短数据场景;
对大数据流式处理或需要复用上下文的场景,请直接使用 SM9Signature。
能力范围
与 GmSSL 一致,SM9 仅支持 签名 / 验签 与 加密 / 解密(IBE), 不支持密钥交换。可用性
所有方法都依赖 native 库;可先通过isAvailable() 判断当前平台是否支持。-
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]使用用户解密私钥解密密文。static byte[]encrypt(SM9EncMasterKey masterPublicKey, String id, byte[] plaintext) 使用公开主密钥向指定用户标识加密明文。static SM9EncKeyextractEncKey(SM9EncMasterKey master, String id) 派生用户解密私钥。static SM9SignKeyextractSignKey(SM9SignMasterKey master, String id) 派生用户签名私钥。static SM9EncMasterKey生成 SM9 加密主密钥。static SM9SignMasterKey生成 SM9 签名主密钥。static boolean判断当前平台的 SM9 native 库是否已成功加载。static String返回 native 库加载失败的诊断消息。static String返回当前运行环境对应的 native 平台标识,例如linux-x86_64。static String返回 native 桥接版本标识。static byte[]sign(SM9SignKey signKey, byte[] data) 使用用户签名私钥对数据进行签名。static booleanverify(SM9SignMasterKey masterPublicKey, String id, byte[] data, byte[] signature) 使用公开主密钥与用户标识验证签名。
-
Method Details
-
isAvailable
public static boolean isAvailable()判断当前平台的 SM9 native 库是否已成功加载。- Returns:
- 已加载返回
true
-
nativeVersion
返回 native 桥接版本标识。- Returns:
- 版本标识
-
nativePlatform
返回当前运行环境对应的 native 平台标识,例如linux-x86_64。如果操作系统或 CPU 架构不在支持范围内,返回
unsupported,可结合nativeLoadErrorMessage()查看具体原因。- Returns:
- native 平台标识
-
nativeLoadErrorMessage
返回 native 库加载失败的诊断消息。- Returns:
- 加载失败消息;native 已可用时返回
null
-
generateSignMasterKey
生成 SM9 签名主密钥。- Returns:
- 签名主密钥
- Throws:
SM9Exception- native 不可用或生成操作失败时抛出
-
generateEncMasterKey
生成 SM9 加密主密钥。- Returns:
- 加密主密钥
-
extractSignKey
派生用户签名私钥。- Parameters:
master- 签名主密钥id- 用户标识- Returns:
- 用户签名私钥
- Throws:
SM9Exception- master 为空、ID 为空或 native 派生失败时抛出
-
extractEncKey
派生用户解密私钥。- Parameters:
master- 加密主密钥id- 用户标识- Returns:
- 用户解密私钥
- Throws:
SM9Exception- master 为空、ID 为空或 native 派生失败时抛出
-
sign
使用用户签名私钥对数据进行签名。- 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
使用公开主密钥向指定用户标识加密明文。- Parameters:
masterPublicKey- 公开主密钥id- 接收方用户标识plaintext- 明文,长度不得超过SM9EncMasterKey.MAX_PLAINTEXT_SIZE- Returns:
- 密文
- Throws:
SM9Exception- 主密钥为空、ID 无效、明文为空/超长或 native 加密失败时抛出
-
decrypt
使用用户解密私钥解密密文。- Parameters:
encKey- 用户解密私钥ciphertext- 密文- Returns:
- 明文
- Throws:
SM9Exception- 私钥为空、密文为空/超长或 native 解密失败时抛出
-