Record Class VersionedPayload

java.lang.Object
java.lang.Record
cloud.opencode.base.crypto.versioned.VersionedPayload
Record Components:
version - the cipher version (0-255) | 加密版本号(0-255)
algorithm - the algorithm name | 算法名称
ciphertext - the encrypted data | 加密数据

public record VersionedPayload(int version, String algorithm, byte[] ciphertext) extends Record
Immutable record representing a versioned cipher payload with serialization support. 表示带版本的加密负载的不可变记录,支持序列化。

Serialization format: [1 byte version] [1 byte algorithm name length] [algorithm name UTF-8] [ciphertext]

序列化格式: [1字节版本] [1字节算法名长度] [算法名UTF8] [密文]

Features | 主要功能:

  • Compact binary serialization - 紧凑的二进制序列化
  • Version range 0-255 - 版本号范围 0-255
  • Defensive copies of ciphertext array - 密文数组的防御性拷贝

Usage Examples | 使用示例:

VersionedPayload payload = new VersionedPayload(1, "AES-256-GCM", ciphertext);
byte[] serialized = payload.serialize();
VersionedPayload restored = VersionedPayload.deserialize(serialized);

Security | 安全性:

  • Thread-safe: Yes (immutable) - 线程安全: 是(不可变)
Since:
JDK 25, opencode-base-crypto V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • VersionedPayload

      public VersionedPayload(int version, String algorithm, byte[] ciphertext)
      Compact constructor with parameter validation. 紧凑构造器,含参数校验。
  • Method Details

    • ciphertext

      public byte[] ciphertext()
      Returns a defensive copy of the ciphertext. 返回密文的防御性拷贝。
      Returns:
      copy of ciphertext bytes | 密文字节的拷贝
    • serialize

      public byte[] serialize()
      Serializes this payload to a byte array. 将此负载序列化为字节数组。

      Format: [1 byte version] [1 byte alg-name length] [alg-name UTF-8] [ciphertext]

      Returns:
      serialized byte array | 序列化的字节数组
    • deserialize

      public static VersionedPayload deserialize(byte[] data)
      Deserializes a byte array into a VersionedPayload. 将字节数组反序列化为 VersionedPayload。
      Parameters:
      data - the serialized data | 序列化数据
      Returns:
      deserialized VersionedPayload | 反序列化的 VersionedPayload
      Throws:
      OpenCryptoException - if the data is malformed | 当数据格式错误时抛出
    • equals

      public boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared with Objects::equals(Object,Object); primitive components are compared with the compare method from their corresponding wrapper classes.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • hashCode

      public int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • toString

      public String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • version

      public int version()
      Returns the value of the version record component.
      Returns:
      the value of the version record component
    • algorithm

      public String algorithm()
      Returns the value of the algorithm record component.
      Returns:
      the value of the algorithm record component