decryptPhrase function

Future<String> decryptPhrase(
  1. Map<String, dynamic> keyStore,
  2. String passphrase
)

Implementation

Future<String> decryptPhrase(
  Map<String, dynamic> keyStore,
  String passphrase,
) async {
  final Uint8List ciphertext =
      (keyStore['crypto']['ciphertext'] as String).toU8a();
  final String kdf = keyStore['crypto']['kdf'];

  final Map<String, dynamic> kdfParams =
      keyStore['crypto']['kdfparams'] is String
          ? json.decode(keyStore['crypto']['kdfparams'])
          : keyStore['crypto']['kdfparams'];
  final cipherParams = keyStore['crypto']['cipherparams'];
  final Uint8List iv = (cipherParams['iv'] as String).toU8a();

  final deriveKeyResult = await nativeDeriveKey(
    kdf: kdf,
    iv: iv,
    message: null,
    useCipherText: ciphertext,
    kdfParams: kdfParams,
    passphrase: passphrase,
    salt: (kdfParams['salt'] as String).replaceAll('0x', ''),
  );

  final String macString = keyStore['crypto']['mac'];

  final Function eq = const ListEquality().equals;
  if (!eq(
    deriveKeyResult.mac.toUpperCase().codeUnits,
    macString.toUpperCase().codeUnits,
  )) {
    throw StateError('Decryption failed.');
  }

  final encryptedPhrase = (keyStore['crypto']['ciphertext'] as String).toU8a();

  return (await _decryptPhraseAsync(
    cipherText: encryptedPhrase,
    key: deriveKeyResult.leftBits,
    iv: iv,
  ))
      .u8aToString();
}