decrypt function

Uint8List decrypt(
  1. String privateKey,
  2. String content
)

Implementation

Uint8List decrypt(String privateKey, String content) {
  final ourPrivateKey = base64Decode(privateKey);
  final envelopeDecode = base64Decode(content);

  final map = jsonDecode(String.fromCharCodes(envelopeDecode));
  final provisionEnvelope =
      ProvisionEnvelope.fromJson(map as Map<String, dynamic>);
  final publicKeyable = Curve.decodePoint(provisionEnvelope.publicKey, 0);
  final message = provisionEnvelope.body;
  if (message[0] != 1) {
    throw LegacyMessageException('Invalid version');
  }
  final iv = Uint8List.fromList(message.getRange(1, 16 + 1).toList());
  final mac = message.getRange(message.length - 32, message.length).toList();
  final ivAndCiphertext =
      Uint8List.fromList(message.getRange(0, message.length - 32).toList());
  final cipherText = Uint8List.fromList(
      message.getRange(16 + 1, message.length - 32).toList());
  final sharedSecret = Curve.calculateAgreement(
      publicKeyable, Curve.decodePrivatePoint(ourPrivateKey));

  final derivedSecretBytes = HKDFv3().deriveSecrets(sharedSecret,
      Uint8List.fromList(utf8.encode(provision)), DerivedRootSecrets.size);

  final aesKey =
      Uint8List.fromList(derivedSecretBytes.getRange(0, 32).toList());
  final macKey = Uint8List.fromList(
      derivedSecretBytes.getRange(32, derivedSecretBytes.length).toList());

  if (!verifyMAC(macKey, ivAndCiphertext, mac)) {
    throw InvalidMacException("MAC doesn't match!");
  }
  final plaintext = aesCbcDecrypt(aesKey, iv, cipherText);
  return plaintext;
}