cryptography
cryptography copied to clipboard
Unnecesary `async`/`Future`?
/// 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.
);
}
I think because of the parallel web-support which enforces usage as Future<>.
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
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.