preWrite method

  1. @override
void preWrite(
  1. PHiveCtx ctx
)

Serializes and encrypts the current value, storing the nonce in metadata.

Implementation

@override
/// Serializes and encrypts the current value, storing the nonce in metadata.
void preWrite(PHiveCtx ctx) {
  if (ctx.value == null) return;

  final plaintext = Uint8List.fromList(utf8.encode(jsonEncode(ctx.value)));
  final nonce = _createNonce();
  ctx.pendingMetadata[_nonceMetadataKey] = base64Url.encode(nonce);

  final key = PhiveMetaRegistry.requireSeedSync(seedId);
  final cipher = _buildCipher(true, key, nonce);
  final ciphertext = cipher.process(plaintext);
  ctx.value = base64Url.encode(ciphertext);
}