sharedSecretKey method

  1. @override
Future<SecretKey> sharedSecretKey({
  1. required KeyPair keyPair,
  2. required PublicKey remotePublicKey,
})
override

Calculates a shared SecretKey.

Implementation

@override
Future<SecretKey> sharedSecretKey({
  required KeyPair keyPair,
  required PublicKey remotePublicKey,
}) async {
  if (usePlugin) {
    try {
      final keyPairData = await keyPair.extract();
      if (keyPairData is! EcKeyPairData) {
        throw ArgumentError.value(
          keyPair,
          'keyPair',
          'Expected EcKeyPair',
        );
      }
      if (remotePublicKey is! EcPublicKey) {
        throw ArgumentError.value(
          remotePublicKey,
          'remotePublicKey',
          'Expected EcPublicKey',
        );
      }
      final result = await channel.invokeMethod(
        'ecdh.sharedSecretKey',
        {
          'algo': algorithmName,
          'privateKey': Uint8List.fromList(keyPairData.d),
          'remoteX': Uint8List.fromList(remotePublicKey.x),
        },
      );
      if (result is! Map) {
        usePlugin = false;
        throw StateError(
          '"package:better_cryptography_flutter": invalid output: $result',
        );
      }
      final error = result['error'];
      if (error != null) {
        throw StateError(
          '"package:better_cryptography_flutter": invalid output from plugin: $error',
        );
      }
      final bytes = result['bytes'] as Uint8List;
      return SecretKey(bytes);
    } catch (error, stackTrace) {
      usePlugin = false;
      reportError(error, stackTrace);
    }
  }
  return super.sharedSecretKey(
    keyPair: keyPair,
    remotePublicKey: remotePublicKey,
  );
}