preWrite method

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

Encrypts the current string value and stores the nonce in PHive metadata.

Implementation

@override
/// Encrypts the current string value and stores the nonce in PHive metadata.
void preWrite(PHiveCtx ctx) {
  if (ctx.value is! String) return;

  final plaintext = Uint8List.fromList(utf8.encode(ctx.value as String));
  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);
}