encrypt method

Future<String> encrypt(
  1. String plainText
)

Encrypts text using AES-GCM (Compatible with Android/Swift format: IV + TAG + CIPHERTEXT)

Implementation

Future<String> encrypt(String plainText) async {
  try {
    final secretKey = SecretKey(utf8.encode(CoreConstants.encryptionKey));
    final clearText = utf8.encode(plainText);

    // Encrypt
    final secretBox = await _algorithm.encrypt(
      clearText,
      secretKey: secretKey,
    );

    // Format: IV (nonce) + TAG (mac) + CIPHERTEXT
    final combined = <int>[];
    combined.addAll(secretBox.nonce);
    combined.addAll(secretBox.mac.bytes);
    combined.addAll(secretBox.cipherText);

    return base64.encode(combined);
  } catch (e) {
    rethrow;
  }
}