Class RsaPssSignature

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

public final class RsaPssSignature extends Object implements SignatureEngine
RSA-PSS signature implementation - RSA probabilistic signature scheme (recommended) RSA-PSS 签名实现 - RSA 概率签名方案(推荐)

RSA-PSS is a more secure RSA signature scheme than PKCS#1 v1.5. It provides provable security and is recommended for new applications. RSA-PSS 是比 PKCS#1 v1.5 更安全的 RSA 签名方案。它提供可证明的安全性,推荐用于新应用。

Features | 主要功能:

  • RSA-PSS signatures with SHA-256/384/512 - RSA-PSS 签名(SHA-256/384/512)
  • Probabilistic signature scheme - 概率签名方案

Usage Examples | 使用示例:

RsaPssSignature pss = RsaPssSignature.sha256();
pss.setPrivateKey(privateKey);
byte[] sig = pss.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
    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 获取签名算法名称
    Get the private key 获取私钥
    Get the public key 获取公钥
    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 格式设置公钥
    Create RSA-PSS signature with SHA-256 (2048-bit key recommended) 创建使用 SHA-256 的 RSA-PSS 签名(推荐 2048 位密钥)
    Create RSA-PSS signature with SHA-256 and generated 2048-bit key pair 创建使用 SHA-256 和生成的 2048 位密钥对的 RSA-PSS 签名
    Create RSA-PSS signature with SHA-384 (3072-bit key recommended) 创建使用 SHA-384 的 RSA-PSS 签名(推荐 3072 位密钥)
    Create RSA-PSS signature with SHA-512 (4096-bit key recommended) 创建使用 SHA-512 的 RSA-PSS 签名(推荐 4096 位密钥)
    Create RSA-PSS signature with SHA-512 and generated 4096-bit key pair 创建使用 SHA-512 和生成的 4096 位密钥对的 RSA-PSS 签名
    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 验证十六进制编码的签名
    withGeneratedKeyPair(int keySize)
    Generate a new RSA key pair of specified size 生成指定大小的新 RSA 密钥对

    Methods inherited from class Object

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

    • sha256

      public static RsaPssSignature sha256()
      Create RSA-PSS signature with SHA-256 (2048-bit key recommended) 创建使用 SHA-256 的 RSA-PSS 签名(推荐 2048 位密钥)
      Returns:
      RSA-PSS with SHA-256 instance
    • sha384

      public static RsaPssSignature sha384()
      Create RSA-PSS signature with SHA-384 (3072-bit key recommended) 创建使用 SHA-384 的 RSA-PSS 签名(推荐 3072 位密钥)
      Returns:
      RSA-PSS with SHA-384 instance
    • sha512

      public static RsaPssSignature sha512()
      Create RSA-PSS signature with SHA-512 (4096-bit key recommended) 创建使用 SHA-512 的 RSA-PSS 签名(推荐 4096 位密钥)
      Returns:
      RSA-PSS with SHA-512 instance
    • sha256WithKeyPair

      public static RsaPssSignature sha256WithKeyPair()
      Create RSA-PSS signature with SHA-256 and generated 2048-bit key pair 创建使用 SHA-256 和生成的 2048 位密钥对的 RSA-PSS 签名
      Returns:
      RSA-PSS with SHA-256 and generated keys
    • sha512WithKeyPair

      public static RsaPssSignature sha512WithKeyPair()
      Create RSA-PSS signature with SHA-512 and generated 4096-bit key pair 创建使用 SHA-512 和生成的 4096 位密钥对的 RSA-PSS 签名
      Returns:
      RSA-PSS with SHA-512 and generated keys
    • withGeneratedKeyPair

      public RsaPssSignature withGeneratedKeyPair(int keySize)
      Generate a new RSA key pair of specified size 生成指定大小的新 RSA 密钥对
      Parameters:
      keySize - the key size in bits (minimum 2048 recommended)
      Returns:
      this signature instance with generated keys
      Throws:
      IllegalArgumentException - if keySize is invalid
    • 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
    • getPublicKey

      public PublicKey getPublicKey()
      Get the public key 获取公钥
      Returns:
      the public key, or null if not set
    • getPrivateKey

      public PrivateKey getPrivateKey()
      Get the private key 获取私钥
      Returns:
      the private key, or null if not set