sharedSecretSync method

  1. @override
SecretKey sharedSecretSync({
  1. required KeyPairData keyPairData,
  2. required PublicKey remotePublicKey,
})
override

Computes shared secret synchronously (unlike sharedSecretKey).

Example

In this example, we use DartX25519 class:

import 'package:cryptography_plus/cryptography_plus.dart';

void main() async {
  final algorithm = DartX25519();

  // We need the private key pair of Alice.
  final aliceKeyPair = algorithm.newKeyPairSync();

  // We need only public key of Bob.
  final bobKeyPair = algorithm.newKeyPairSync();
  final bobPublicKey = bobKeyPair.publicKey;

  // We can now calculate a 32-byte shared secret key.
  final sharedSecretKey = algorithm.sharedSecretKeySync(
    keyPair: aliceKeyPair,
    remotePublicKey: bobPublicKey,
  );
}

Implementation

@override
SecretKey sharedSecretSync({
  required KeyPairData keyPairData,
  required PublicKey remotePublicKey,
}) {
  if (keyPairData is! SimpleKeyPairData ||
      !KeyPairType.x25519.isValidKeyPairData(keyPairData)) {
    throw ArgumentError.value(
      keyPairData,
      'keyPairData',
    );
  }
  if (remotePublicKey is! SimplePublicKey ||
      !KeyPairType.x25519.isValidPublicKey(remotePublicKey)) {
    throw ArgumentError.value(
      remotePublicKey,
      'remotePublicKey',
    );
  }
  final privateKeyBytes = modifiedPrivateKeyBytes(keyPairData.bytes);
  final result = Uint8List(32);
  _calculate(
    result,
    privateKeyBytes,
    Uint8List.fromList(remotePublicKey.bytes),
  );
  return SecretKey(result);
}