decryptCborPhrase function

Future<String> decryptCborPhrase(
  1. List<int> bytes, {
  2. String? password,
})

Implementation

Future<String> decryptCborPhrase(
  List<int> bytes, {
  String? password,
}) async {
  final recover = Map<String, dynamic>.from(cborDecode(bytes));
  final Uint8List ciphertext = Uint8List.fromList(recover['ciphertext']);

  final Uint8List iv = Uint8List.fromList(recover['cipherparams']['iv']);
  final kdfParams = Map<String, dynamic>.from(cborDecode(recover['kdfparams']));
  final String kdf = Uint8List.fromList(recover['kdf']).u8aToString();

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

  final Uint8List macFromRecover = Uint8List.fromList(
    u8aToU8a(
      (recover['mac'] as List).map((ele) {
        return int.parse(ele.toString());
      }).toList(),
    ),
  );

  if (!u8aEq(deriveKeyResult.mac.toU8a(), macFromRecover)) {
    throw StateError('decryption failed.');
  }

  final Uint8List encryptedPhrase = Uint8List.fromList(recover['ciphertext']);

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