decrypt method

Future<String> decrypt(
  1. String encryptedBase64
)

Decrypts text using AES-GCM

Implementation

Future<String> decrypt(String encryptedBase64) async {
  try {
    final combined = base64.decode(encryptedBase64);
    final secretKey = SecretKey(utf8.encode(CoreConstants.encryptionKey));

    // Extract parts
    // IV is usually 12 bytes
    final iv = combined.sublist(0, 12);
    // Tag (MAC) is usually 16 bytes
    final tag = combined.sublist(12, 28);
    // The rest is ciphertext
    final cipherText = combined.sublist(28);

    final secretBox = SecretBox(
      cipherText,
      nonce: iv,
      mac: Mac(tag),
    );

    // Decrypt
    final clearText = await _algorithm.decrypt(
      secretBox,
      secretKey: secretKey,
    );

    return utf8.decode(clearText);
  } catch (e) {
    rethrow;
  }
}