dint-dev / cryptography

Cryptography for Flutter developers: encryption, digital signatures, key agreement, etc.
https://pub.dev/packages/cryptography
Apache License 2.0
155 stars 75 forks source link

Unnecesary `async`/`Future`? #166

Open busslina opened 8 months ago

busslina commented 8 months ago
/// Constructs a new [SecretKey] from the bytes.
///
/// Throws [ArgumentError] if the argument length is not [secretKeyLength].
Future<SecretKey> newSecretKeyFromBytes(List<int> bytes) async {
  if (bytes.length != secretKeyLength) {
    throw ArgumentError('Invalid secret key length');
  }
  return SecretKeyData(
    Uint8List.fromList(bytes),
    overwriteWhenDestroyed: true, // We copied the bytes so overwriting is ok.
  );
}
justkawal commented 8 months ago

I think because of the parallel web-support which enforces usage as Future<>.

busslina commented 8 months ago

You can use this design:

abstract interface class Interface {
  FutureOr<SecretKey> newSecretKeyFromBytes(List<int> bytes);
}

class ImplementationWeb extends Interface {
  @override
  Future<SecretKey> newSecretKeyFromBytes(List<int> bytes) =>
      throw ('Not implemented');
}

class ImplementationNative extends Interface {
  @override
  SecretKey newSecretKeyFromBytes(List<int> bytes) => throw ('Not implemented');
}

This async is make me changing too much method signatures

daegalus commented 4 months ago

I would have to agree with this. I want to use cryptography in the uuid library, but it being async, would require me to change my entire library to async functions, which would not only break everyone using the library, but is unnecessary.

The above design would go a long way to solving that for those that don't need it.