Class AbstractResultEncryptor

java.lang.Object
cloud.opencode.base.web.crypto.AbstractResultEncryptor
All Implemented Interfaces:
ResultEncryptor
Direct Known Subclasses:
AesResultEncryptor

public abstract class AbstractResultEncryptor extends Object implements ResultEncryptor
Abstract Result Encryptor 抽象响应加密器

Base class for result encryptors providing common functionality.

响应加密器基类,提供通用功能。

Features | 主要功能:

  • Base class for result encryptors - 响应加密器基类
  • JSON serialization/deserialization via OpenJson - 通过OpenJson进行JSON序列化/反序列化
  • Base64 encoding of encrypted data - 加密数据的Base64编码
  • Generic type support via TypeReference - 通过TypeReference支持泛型类型
  • HMAC signature for tamper detection - HMAC签名防篡改

Usage Examples | 使用示例:

public class MyEncryptor extends AbstractResultEncryptor {
    protected byte[] doEncrypt(byte[] data) { ... }
    protected byte[] doDecrypt(byte[] data) { ... }
    protected byte[] doSign(byte[] data) { ... }
    public String getAlgorithm() { return "MY-ALG"; }
}

// Decrypt with generic type
Result<List<User>> result = encryptor.decrypt(encrypted, new TypeReference<List<User>>() {});

Security | 安全性:

  • Thread-safe: Implementation-dependent - 取决于实现
  • Null-safe: No (result must not be null) - 否(结果不能为null)
Since:
JDK 25, opencode-base-web V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • AbstractResultEncryptor

      public AbstractResultEncryptor()
  • Method Details

    • encrypt

      public <T> EncryptedResult encrypt(Result<T> result)
      Description copied from interface: ResultEncryptor
      Encrypt result 加密响应
      Specified by:
      encrypt in interface ResultEncryptor
      Type Parameters:
      T - the data type | 数据类型
      Parameters:
      result - the result to encrypt | 要加密的响应
      Returns:
      the encrypted result | 加密后的响应
    • decrypt

      public <T> Result<T> decrypt(EncryptedResult encrypted, Class<T> dataType)
      Description copied from interface: ResultEncryptor
      Decrypt to result 解密为响应
      Specified by:
      decrypt in interface ResultEncryptor
      Type Parameters:
      T - the data type | 数据类型
      Parameters:
      encrypted - the encrypted result | 加密的响应
      dataType - the data type class | 数据类型类
      Returns:
      the decrypted result | 解密后的响应
    • decrypt

      public <T> Result<T> decrypt(EncryptedResult encrypted, TypeReference<T> typeReference)
      Description copied from interface: ResultEncryptor
      Decrypt to result with generic type support 解密为带泛型类型的响应
      Specified by:
      decrypt in interface ResultEncryptor
      Type Parameters:
      T - the data type | 数据类型
      Parameters:
      encrypted - the encrypted result | 加密的响应
      typeReference - the type reference for generic types | 泛型类型引用
      Returns:
      the decrypted result | 解密后的响应
    • doEncrypt

      protected abstract byte[] doEncrypt(byte[] data) throws Exception
      Perform encryption 执行加密
      Parameters:
      data - the data to encrypt | 要加密的数据
      Returns:
      the encrypted data | 加密后的数据
      Throws:
      Exception - if encryption fails | 如果加密失败
    • doDecrypt

      protected abstract byte[] doDecrypt(byte[] data) throws Exception
      Perform decryption 执行解密
      Parameters:
      data - the data to decrypt | 要解密的数据
      Returns:
      the decrypted data | 解密后的数据
      Throws:
      Exception - if decryption fails | 如果解密失败
    • doSign

      protected abstract byte[] doSign(byte[] data) throws Exception
      Compute HMAC signature 计算HMAC签名
      Parameters:
      data - the data to sign | 要签名的数据
      Returns:
      the HMAC signature bytes | HMAC签名字节
      Throws:
      Exception - if signing fails | 如果签名失败