Class SmUtil

java.lang.Object
com.sure.tool.crypto.SmUtil

public final class SmUtil extends Object
国密算法统一入口(SM2 / SM3 / SM4)。

全部为纯 JDK 实现,零第三方依赖,遵循 GB/T 32905 / 32907 / 32918。

Since:
1.0.1
Author:
suretool
  • Method Details

    • sm2KeyPair

      public static Sm2Util.Sm2KeyPair sm2KeyPair()
      生成 SM2 密钥对。
      Returns:
      密钥对
    • sm2Sign

      public static byte[] sm2Sign(Sm2Util.Sm2KeyPair keyPair, byte[] message)
      SM2 签名(r||s,64 字节)。
      Parameters:
      keyPair - 密钥对
      message - 消息
      Returns:
      签名
    • sm2Verify

      public static boolean sm2Verify(BigInteger pubX, BigInteger pubY, byte[] message, byte[] signature)
      SM2 验签。
      Parameters:
      pubX - 公钥 x
      pubY - 公钥 y
      message - 消息
      signature - 签名
      Returns:
      是否通过
    • sm2Encrypt

      public static byte[] sm2Encrypt(BigInteger pubX, BigInteger pubY, byte[] data)
      SM2 加密(C1||C3||C2)。
      Parameters:
      pubX - 公钥 x
      pubY - 公钥 y
      data - 明文
      Returns:
      密文
    • sm2Decrypt

      public static byte[] sm2Decrypt(Sm2Util.Sm2KeyPair keyPair, byte[] cipher)
      SM2 解密。
      Parameters:
      keyPair - 密钥对
      cipher - 密文
      Returns:
      明文
    • sm3

      public static byte[] sm3(byte[] data)
      SM3 摘要。
      Parameters:
      data - 输入
      Returns:
      32 字节摘要
    • sm3Hex

      public static String sm3Hex(String text)
      SM3 摘要(十六进制)。
      Parameters:
      text - 字符串
      Returns:
      64 位十六进制摘要
    • sm4Key

      public static byte[] sm4Key()
      SM4 生成密钥。
      Returns:
      16 字节密钥
    • sm4EncryptHex

      public static String sm4EncryptHex(byte[] key, String text)
      SM4-ECB 加密(十六进制)。
      Parameters:
      key - 16 字节密钥
      text - 明文字符串
      Returns:
      十六进制密文
    • sm4DecryptStr

      public static String sm4DecryptStr(byte[] key, String hex)
      SM4-ECB 解密(十六进制)。
      Parameters:
      key - 16 字节密钥
      hex - 十六进制密文
      Returns:
      明文字符串