copyWith method

SecureMessage copyWith({
  1. int? version,
  2. int? window,
  3. Uint8List? nonce,
  4. Uint8List? ciphertext,
  5. Uint8List? tag,
})

Convenience: clones this message with new ciphertext/tag (e.g., for re-encrypt).

HINT: Rarely needed; supplied for completeness in tests/tools.

Implementation

SecureMessage copyWith({
  int? version,
  int? window,
  Uint8List? nonce,
  Uint8List? ciphertext,
  Uint8List? tag,
}) {
  final nextNonce = nonce ?? this.nonce;
  final nextCipher = ciphertext ?? this.ciphertext;
  final nextTag = tag ?? this.tag;

  // Re-validate variable-length fields on mutation.
  NonceGenerator.validate(nextNonce);
  if (nextCipher.isEmpty) {
    throw InvalidMessageException(cause: ArgumentError('ciphertext must not be empty.'));
  }
  if (nextTag.length != 32) {
    throw InvalidMessageException(cause: ArgumentError.value(nextTag.length, 'tag.length', 'HMAC-SHA256 tag must be 32 bytes.'));
  }

  return SecureMessage._internal(
    version: version ?? this.version,
    window: window ?? this.window,
    nonce: Uint8List.fromList(nextNonce),
    ciphertext: Uint8List.fromList(nextCipher),
    tag: Uint8List.fromList(nextTag),
  );
}