Renci.SshNet.ServiceFactory.CreateKeyExchange C# (CSharp) Method

CreateKeyExchange() public method

Negotiates a key exchange algorithm, and creates a IKeyExchange for the negotiated algorithm.
is null. is null. No key exchange algorithms are supported by both client and server.
public CreateKeyExchange ( Type>.IDictionary clientAlgorithms, string serverAlgorithms ) : IKeyExchange
clientAlgorithms Type>.IDictionary A of the key exchange algorithms supported by the client where key is the name of the algorithm, and value is the type implementing this algorithm.
serverAlgorithms string The names of the key exchange algorithms supported by the SSH server.
return IKeyExchange
        public IKeyExchange CreateKeyExchange(IDictionary<string, Type> clientAlgorithms, string[] serverAlgorithms)
        {
            if (clientAlgorithms == null)
                throw new ArgumentNullException("clientAlgorithms");
            if (serverAlgorithms == null)
                throw new ArgumentNullException("serverAlgorithms");

            // find an algorithm that is supported by both client and server
            var keyExchangeAlgorithmType = (from c in clientAlgorithms
                                            from s in serverAlgorithms
                                            where s == c.Key
                                            select c.Value).FirstOrDefault();

            if (keyExchangeAlgorithmType == null)
            {
                throw new SshConnectionException("Failed to negotiate key exchange algorithm.", DisconnectReason.KeyExchangeFailed);
            }

            return keyExchangeAlgorithmType.CreateInstance<IKeyExchange>();
        }
    }