negotiateAlgorithms method

  1. @override
NegotiationResult? negotiateAlgorithms(
  1. CryptoPeerCapabilities local,
  2. CryptoPeerCapabilities remote, {
  3. bool isInitiator = true,
})
override

Negotiates algorithms based on local and remote capabilities. Returns null if no compatible algorithms are found.

Implementation

@override
NegotiationResult? negotiateAlgorithms(
  CryptoPeerCapabilities local,
  CryptoPeerCapabilities remote, {
  bool isInitiator = true,
}) {
  // Find common algorithms
  final commonKeyExchange = local.keyExchange
      .where((alg) => remote.keyExchange.contains(alg))
      .toList();

  final commonAsymmetric = local.asymmetric
      .where((alg) => remote.asymmetric.contains(alg))
      .toList();

  final commonSymmetric = local.symmetric
      .where((alg) => remote.symmetric.contains(alg))
      .toList();

  if (commonKeyExchange.isEmpty || commonAsymmetric.isEmpty || commonSymmetric.isEmpty) {
    return null; // No compatible algorithms
  }

  // Select algorithms using priority order
  final selectedKeyExchange = _keyExchangePriority
      .firstWhere((alg) => commonKeyExchange.contains(alg));

  final selectedAsymmetric = _asymmetricPriority
      .firstWhere((alg) => commonAsymmetric.contains(alg));

  final selectedSymmetric = _symmetricPriority
      .firstWhere((alg) => commonSymmetric.contains(alg));

  return NegotiationResult(
    keyExchange: selectedKeyExchange,
    asymmetric: selectedAsymmetric,
    symmetric: selectedSymmetric,
    localPeerId: local.peerId,
    remotePeerId: remote.peerId,
    isInitiator: isInitiator,
  );
}