postRead method

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

Decrypts a previously encrypted string value using the stored IV.

Implementation

@override
/// Decrypts a previously encrypted string value using the stored IV.
void postRead(PHiveCtx ctx) {
  if (ctx.value is! String || !ctx.metadata.containsKey(_ivMetadataKey)) {
    return;
  }

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

  final paddedPlaintext = Uint8List(ciphertext.length);
  for (var offset = 0; offset < ciphertext.length; offset += _blockSize) {
    cipher.processBlock(ciphertext, offset, paddedPlaintext, offset);
  }

  ctx.value = utf8.decode(_unpadPlaintext(paddedPlaintext));
}