decryptWithKeyDerivedFromString function

Future<List<int>> decryptWithKeyDerivedFromString(
  1. {required String serialized,
  2. required String passphrase}
)

Provided an encrypted+serialized string (in Cryppo's encryption serialization format which includes serialized key derivation artefacts), derive the key (using the entered passphrase) and return the decrypted binary data.

Implementation

Future<List<int>> decryptWithKeyDerivedFromString(
    {required String serialized, required String passphrase}) async {
  final parts = serialized.split('.');

  if (parts.length != 5) {
    throw Exception('Invalid encrypted serialized data - expected 5 parts');
  }

  /// Serialization in the following segments:
  /// parts[0] = encryptionStrategy
  /// parts[1] = encryptedData
  /// parts[2] = encryptionArtefacts
  /// (When using a derived key:)
  /// parts[3] = keyDerivationStrategy
  /// parts[4] = keyDerivationArtefacts

  final derivedKey = await deriveKeyWithSerializedOptions(
      passphrase, parts.sublist(parts.length - 2).join('.'));
  return _decryptSerialized(parts.sublist(0, 3).join('.'), derivedKey.key);
}