postRead method

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

Decrypts and attempts to JSON-decode a previously encrypted value.

Implementation

@override
/// Decrypts and attempts to JSON-decode a previously encrypted value.
void postRead(PHiveCtx ctx) {
  if (ctx.value is! String || !ctx.metadata.containsKey(_nonceMetadataKey)) {
    return;
  }

  final nonce = Uint8List.fromList(
    base64Url.decode(ctx.metadata[_nonceMetadataKey] as String),
  );
  final ciphertext = Uint8List.fromList(base64Url.decode(ctx.value as String));
  final key = PhiveMetaRegistry.requireSeedSync(seedId);
  final cipher = _buildCipher(false, key, nonce);

  final plaintext = cipher.process(ciphertext);
  final decodedString = utf8.decode(plaintext);

  try {
    ctx.value = jsonDecode(decodedString);
  } catch (_) {
    ctx.value = decodedString;
  }
}