decryptMessage static method

Future<String> decryptMessage(
  1. String payload,
  2. String recipientPrivateKey,
  3. String senderPublicKey, {
  4. Uint8List? customConversationKey,
})

Implementation

static Future<String> decryptMessage(
  String payload,
  String recipientPrivateKey,
  String senderPublicKey, {
  Uint8List? customConversationKey,
}) async {
  // Step 1: Compute Shared Secret
  final sharedSecret = customConversationKey ??
      computeSharedSecret(recipientPrivateKey, senderPublicKey);

  // Step 2: Derive Conversation Key
  final conversationKey =
      customConversationKey ?? deriveConversationKey(sharedSecret);

  // Step 3: Parse Payload
  final parsed = parsePayload(payload);
  final nonce = parsed['nonce'];
  final ciphertext = parsed['ciphertext'];
  final mac = parsed['mac'];

  // Step 4: Derive Message Keys
  final keys = deriveMessageKeys(conversationKey, nonce);
  final chachaKey = keys['chachaKey']!;
  final chachaNonce = keys['chachaNonce']!;
  final hmacKey = keys['hmacKey']!;

  // Step 5: Verify MAC
  verifyMac(hmacKey, nonce, ciphertext, mac);

  // Step 6: Decrypt
  final paddedPlaintext =
      await decryptChaCha20(chachaKey, chachaNonce, ciphertext);

  // Step 7: Unpad Plaintext
  final plaintextBytes = unpad(paddedPlaintext);

  return utf8.decode(plaintextBytes);
}