encryptMessage static method
Implementation
static Future<String> encryptMessage(
String plaintext,
String senderPrivateKey,
String recipientPublicKey, {
Uint8List? customNonce,
Uint8List? customConversationKey,
}) async {
// Step 1: Compute Shared Secret
final sharedSecret = customConversationKey ??
computeSharedSecret(senderPrivateKey, recipientPublicKey);
// Step 2: Derive Conversation Key
final conversationKey =
customConversationKey ?? deriveConversationKey(sharedSecret);
// Step 3: Generate or Use Custom Nonce
final nonce = customNonce ?? secureRandomBytes(32);
// Step 4: Derive Message Keys
final keys = deriveMessageKeys(conversationKey, nonce);
final chachaKey = keys['chachaKey']!;
final chachaNonce = keys['chachaNonce']!;
final hmacKey = keys['hmacKey']!;
// Step 5: Pad Plaintext
final paddedPlaintext = pad(utf8.encode(plaintext));
// Step 6: Encrypt
final ciphertext =
await encryptChaCha20(chachaKey, chachaNonce, paddedPlaintext);
// Step 7: Calculate MAC
final mac = calculateMac(hmacKey, nonce, ciphertext);
// Step 8: Construct Payload
return constructPayload(nonce, ciphertext, mac);
}