encrypt method

  1. @override
Uint8List encrypt(
  1. Uint8List plaintext
)
override

Encrypts plaintext and returns the ciphertext.

The returned bytes may be longer than plaintext to accommodate an IV, nonce, or authentication tag.

Implementation

@override
Uint8List encrypt(Uint8List plaintext) {
  try {
    final iv = _generateIv();
    final cipher = _buildCipher(iv, forEncryption: true);

    // Output buffer: payload + 16-byte auth tag
    final outputLen = plaintext.length + 16;
    final output = Uint8List(outputLen);

    var offset = cipher.processBytes(plaintext, 0, plaintext.length, output, 0);
    offset += cipher.doFinal(output, offset);

    // Prepend IV → [ IV | ciphertext+tag ]
    final result = Uint8List(12 + outputLen);
    result.setRange(0, 12, iv);
    result.setRange(12, 12 + outputLen, output);
    return result;
  } catch (e) {
    if (e is SuperCacheException) rethrow;
    throw CacheEncryptionException(
      'AES-256-GCM encryption failed: $e',
      cause: e,
    );
  }
}