Class KeyUtil

java.lang.Object
cloud.opencode.base.crypto.key.KeyUtil

public final class KeyUtil extends Object
Key utility class for key operations and queries - Utility methods for working with cryptographic keys 密钥工具类 - 密钥操作和查询的实用方法

Features | 主要功能:

  • Key conversion and encoding utilities - 密钥转换和编码工具

Usage Examples | 使用示例:

byte[] encoded = KeyUtil.encode(key);

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
  • Null-safe: Yes - 空值安全: 是

Performance | 性能特性:

  • Time complexity: O(1) - 时间复杂度: O(1)
  • Space complexity: O(k) - 空间复杂度: O(k),k为密钥大小
Since:
JDK 25, opencode-base-crypto V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • getKeySize

      public static int getKeySize(Key key)
      Get key size in bits 获取密钥大小(位)
      Parameters:
      key - the key to check
      Returns:
      key size in bits
      Throws:
      OpenKeyException - if key size cannot be determined
    • getAlgorithm

      public static String getAlgorithm(Key key)
      Get key algorithm 获取密钥算法
      Parameters:
      key - the key to check
      Returns:
      algorithm name
      Throws:
      OpenKeyException - if key is null
    • isPrivateKey

      public static boolean isPrivateKey(Key key)
      Check if key is a private key 检查密钥是否为私钥
      Parameters:
      key - the key to check
      Returns:
      true if key is a private key
      Throws:
      OpenKeyException - if key is null
    • isPublicKey

      public static boolean isPublicKey(Key key)
      Check if key is a public key 检查密钥是否为公钥
      Parameters:
      key - the key to check
      Returns:
      true if key is a public key
      Throws:
      OpenKeyException - if key is null
    • isSecretKey

      public static boolean isSecretKey(Key key)
      Check if key is a secret key 检查密钥是否为对称密钥
      Parameters:
      key - the key to check
      Returns:
      true if key is a secret key
      Throws:
      OpenKeyException - if key is null
    • getEncoded

      public static byte[] getEncoded(Key key)
      Get encoded key bytes 获取编码的密钥字节
      Parameters:
      key - the key to encode
      Returns:
      encoded key bytes
      Throws:
      OpenKeyException - if key is null or encoding fails
    • getFormat

      public static String getFormat(Key key)
      Get key format 获取密钥格式
      Parameters:
      key - the key to check
      Returns:
      key format (e.g., "X.509", "PKCS#8", "RAW")
      Throws:
      OpenKeyException - if key is null