cryptography icon indicating copy to clipboard operation
cryptography copied to clipboard

Cannot encrypt synchronously / unnecessary async

Open creativecreatorormaybenot opened this issue 3 years ago • 0 comments

Taking an example encryption algorithm and mode:

AesCtr.with128bits(
  macAlgorithm: Hmac.sha256(),
);

There is no way to encrypt a cleartext synchronously.
This is because both generating the newSecretKey() as well as running the actual encrypt() function returns a Future.

I was confused by this since I did not see a reason for any async operation to be part of this and therefore I went through the whole source code (the DartAesCtr in this case) only to find out that there is not a single async operation. The only reason this is async is because of the abstract definition of the base class. The implementation only returns Future.values a number of times. This means that this should not be marked async in the first place.

There are two requests I have here:

  1. The abstract definitions should use FutureOr from dart:async instead.
  2. Every algorithm that works synchronously should also offer a synchronous encryption method.

One way to implement this would be to add an encryptSync that throws an error whenever this is not possible.


This is a deal-breaker for me since I rely on encrypting synchronously in a Flutter app in order to not skip a frame.