Avoid using less optimal Linq, and use optimized extension methods instead.

This commit is contained in:
drieseng
2016-05-16 17:03:38 +02:00
parent b359dfa1e5
commit 05d8ccbd7b
29 changed files with 66 additions and 72 deletions
+1 -1
View File
@@ -25,7 +25,7 @@ namespace Renci.SshNet
/// <summary>
/// Gets list of allowed authentications.
/// </summary>
public IEnumerable<string> AllowedAuthentications { get; protected set; }
public IList<string> AllowedAuthentications { get; protected set; }
/// <summary>
/// Initializes a new instance of the <see cref="AuthenticationMethod"/> class.
+2 -2
View File
@@ -1,5 +1,5 @@
using System;
using System.Linq;
using Renci.SshNet.Common;
using Renci.SshNet.Security.Cryptography;
namespace Renci.SshNet
@@ -30,7 +30,7 @@ namespace Renci.SshNet
public CipherInfo(int keySize, Func<byte[], byte[], Cipher> cipher)
{
KeySize = keySize;
Cipher = (key, iv) => (cipher(key.Take(KeySize / 8).ToArray(), iv));
Cipher = (key, iv) => (cipher(key.Take(KeySize / 8), iv));
}
}
}
+4 -4
View File
@@ -30,7 +30,7 @@ namespace Renci.SshNet
var authenticated = noneAuthenticationMethod.Authenticate(session);
if (authenticated != AuthenticationResult.Success)
{
if (!TryAuthenticate(session, new AuthenticationState(connectionInfo.AuthenticationMethods.ToList()), noneAuthenticationMethod.AllowedAuthentications.ToList(), ref authenticationException))
if (!TryAuthenticate(session, new AuthenticationState(connectionInfo.AuthenticationMethods), noneAuthenticationMethod.AllowedAuthentications.ToList(), ref authenticationException))
{
throw authenticationException;
}
@@ -51,7 +51,7 @@ namespace Renci.SshNet
ICollection<string> allowedAuthenticationMethods,
ref SshAuthenticationException authenticationException)
{
if (!allowedAuthenticationMethods.Any())
if (allowedAuthenticationMethods.Count == 0)
{
authenticationException = new SshAuthenticationException("No authentication methods defined on SSH server.");
return false;
@@ -61,7 +61,7 @@ namespace Renci.SshNet
// passed in the ctor, not the order in which the SSH server returns
// the allowed authentication methods
var matchingAuthenticationMethods = authenticationState.SupportedAuthenticationMethods.Where(a => allowedAuthenticationMethods.Contains(a.Name)).ToList();
if (!matchingAuthenticationMethods.Any())
if (matchingAuthenticationMethods.Count == 0)
{
authenticationException = new SshAuthenticationException(string.Format("No suitable authentication method found to complete authentication ({0}).", string.Join(",", allowedAuthenticationMethods.ToArray())));
return false;
@@ -87,7 +87,7 @@ namespace Renci.SshNet
switch (authenticationResult)
{
case AuthenticationResult.PartialSuccess:
if (TryAuthenticate(session, authenticationState, authenticationMethod.AllowedAuthentications.ToList(), ref authenticationException))
if (TryAuthenticate(session, authenticationState, authenticationMethod.AllowedAuthentications, ref authenticationException))
{
authenticationResult = AuthenticationResult.Success;
}
-1
View File
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace Renci.SshNet.Common
{
+2 -2
View File
@@ -228,7 +228,7 @@ namespace Renci.SshNet.Common
/// A <see cref="byte"/> array that contains the specified number of bytes at the specified offset
/// of the input array.
/// </returns>
internal static byte[] Take(this byte[] value, int offset, int count)
public static byte[] Take(this byte[] value, int offset, int count)
{
if (value == null)
throw new ArgumentNullException("value");
@@ -244,7 +244,7 @@ namespace Renci.SshNet.Common
return taken;
}
internal static byte[] Take(this byte[] value, int count)
public static byte[] Take(this byte[] value, int count)
{
if (value == null)
throw new ArgumentNullException("value");
+4 -4
View File
@@ -46,7 +46,7 @@ namespace Renci.SshNet
/// <summary>
/// Gets supported authentication methods for this connection.
/// </summary>
public IEnumerable<AuthenticationMethod> AuthenticationMethods { get; private set; }
public IList<AuthenticationMethod> AuthenticationMethods { get; private set; }
/// <summary>
/// Gets supported compression algorithms for this connection.
@@ -284,7 +284,7 @@ namespace Renci.SshNet
if (authenticationMethods == null)
throw new ArgumentNullException("authenticationMethods");
if (!authenticationMethods.Any())
if (authenticationMethods.Length == 0)
throw new ArgumentException("At least one authentication method should be specified.", "authenticationMethods");
// Set default connection values
@@ -437,9 +437,9 @@ namespace Renci.SshNet
return new NoneAuthenticationMethod(Username);
}
IEnumerable<IAuthenticationMethod> IConnectionInfoInternal.AuthenticationMethods
IList<IAuthenticationMethod> IConnectionInfoInternal.AuthenticationMethods
{
get { return AuthenticationMethods.Cast<IAuthenticationMethod>(); }
get { return AuthenticationMethods.Cast<IAuthenticationMethod>().ToList(); }
}
}
}
+2 -2
View File
@@ -1,6 +1,6 @@
using System;
using System.Linq;
using System.Security.Cryptography;
using Renci.SshNet.Common;
namespace Renci.SshNet
{
@@ -30,7 +30,7 @@ namespace Renci.SshNet
public HashInfo(int keySize, Func<byte[], HashAlgorithm> hash)
{
KeySize = keySize;
HashAlgorithm = key => (hash(key.Take(KeySize / 8).ToArray()));
HashAlgorithm = key => (hash(key.Take(KeySize / 8)));
}
}
}
+1 -1
View File
@@ -22,7 +22,7 @@ namespace Renci.SshNet
/// <value>
/// The list of allowed authentications.
/// </value>
IEnumerable<string> AllowedAuthentications { get; }
IList<string> AllowedAuthentications { get; }
/// <summary>
/// Gets the name of the authentication method.
+1 -1
View File
@@ -22,7 +22,7 @@ namespace Renci.SshNet
/// <value>
/// The supported authentication methods for this connection.
/// </value>
IEnumerable<IAuthenticationMethod> AuthenticationMethods { get; }
IList<IAuthenticationMethod> AuthenticationMethods { get; }
/// <summary>
/// Creates a <see cref="NoneAuthenticationMethod"/> for the credentials represented
@@ -91,8 +91,8 @@ namespace Renci.SshNet
else
_authenticationResult = AuthenticationResult.Failure;
// Copy allowed authentication methods
AllowedAuthentications = e.Message.AllowedAuthentications.ToList();
// Copy allowed authentication methods
AllowedAuthentications = e.Message.AllowedAuthentications;
_authenticationCompleted.Set();
}
@@ -1,4 +1,6 @@
namespace Renci.SshNet.Messages.Authentication
using Renci.SshNet.Common;
namespace Renci.SshNet.Messages.Authentication
{
/// <summary>
/// Represents "keyboard-interactive" SSH_MSG_USERAUTH_REQUEST message.
@@ -42,8 +44,8 @@
public RequestMessageKeyboardInteractive(ServiceName serviceName, string username)
: base(serviceName, username, "keyboard-interactive")
{
Language = new byte[0];
SubMethods = new byte[0];
Language = Array<byte>.Empty;
SubMethods = Array<byte>.Empty;
}
/// <summary>
@@ -1,4 +1,5 @@
using System;
using Renci.SshNet.Common;
namespace Renci.SshNet.Messages.Transport
{
@@ -20,7 +21,7 @@ namespace Renci.SshNet.Messages.Transport
/// </summary>
public IgnoreMessage()
{
Data = new byte[0];
Data = Array<byte>.Empty;
}
/// <summary>
+2 -3
View File
@@ -1,5 +1,4 @@
using System;
using System.Linq;
using System.Threading;
using Renci.SshNet.Messages.Authentication;
using Renci.SshNet.Messages;
@@ -76,8 +75,8 @@ namespace Renci.SshNet
else
_authenticationResult = AuthenticationResult.Failure;
// Copy allowed authentication methods
AllowedAuthentications = e.Message.AllowedAuthentications.ToList();
// Copy allowed authentication methods
AllowedAuthentications = e.Message.AllowedAuthentications;
_authenticationCompleted.Set();
}
@@ -1,5 +1,4 @@
using System;
using System.Linq;
using System.Text;
using System.Threading;
using Renci.SshNet.Abstractions;
@@ -116,7 +115,7 @@ namespace Renci.SshNet
_authenticationResult = AuthenticationResult.Failure;
// Copy allowed authentication methods
AllowedAuthentications = e.Message.AllowedAuthentications.ToList();
AllowedAuthentications = e.Message.AllowedAuthentications;
_authenticationCompleted.Set();
}
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections.ObjectModel;
using Renci.SshNet.Messages.Authentication;
using Renci.SshNet.Messages;
@@ -127,7 +126,7 @@ namespace Renci.SshNet
_authenticationResult = AuthenticationResult.Failure;
// Copy allowed authentication methods
AllowedAuthentications = e.Message.AllowedAuthentications.ToList();
AllowedAuthentications = e.Message.AllowedAuthentications;
_authenticationCompleted.Set();
}
+7 -8
View File
@@ -1,5 +1,4 @@
using System;
using System.Linq;
using System.Collections.Generic;
using System.IO;
using System.Text;
@@ -191,11 +190,11 @@ namespace Renci.SshNet
switch (keyName)
{
case "RSA":
_key = new RsaKey(decryptedData.ToArray());
_key = new RsaKey(decryptedData);
HostKey = new KeyHostAlgorithm("ssh-rsa", _key);
break;
case "DSA":
_key = new DsaKey(decryptedData.ToArray());
_key = new DsaKey(decryptedData);
HostKey = new KeyHostAlgorithm("ssh-dss", _key);
break;
case "SSH2 ENCRYPTED":
@@ -288,13 +287,13 @@ namespace Renci.SshNet
while (cipherKey.Count < length)
{
hash = passwordBytes.Concat(hash).ToArray();
hash = passwordBytes.Concat(hash);
hash = md5.ComputeHash(hash);
cipherKey.AddRange(hash);
}
}
return cipherKey.Take(length).ToArray();
return cipherKey.ToArray().Take(length);
}
/// <summary>
@@ -325,14 +324,14 @@ namespace Renci.SshNet
var passwordBytes = Encoding.UTF8.GetBytes(passPhrase);
// Use 8 bytes binary salt
var initVector = passwordBytes.Concat(binarySalt.Take(8)).ToArray();
var initVector = passwordBytes.Concat(binarySalt.Take(8));
var hash = md5.ComputeHash(initVector);
cipherKey.AddRange(hash);
while (cipherKey.Count < cipherInfo.KeySize / 8)
{
hash = hash.Concat(initVector).ToArray();
hash = hash.Concat(initVector);
hash = md5.ComputeHash(hash);
cipherKey.AddRange(hash);
}
@@ -425,7 +424,7 @@ namespace Renci.SshNet
var bytesArray = new byte[data.Length + 1];
Buffer.BlockCopy(data, 0, bytesArray, 1, data.Length);
return new BigInteger(bytesArray.Reverse().ToArray());
return new BigInteger(bytesArray.Reverse());
}
protected override void LoadData()
@@ -1,5 +1,4 @@
using System;
using System.Linq;
using Renci.SshNet.Common;
namespace Renci.SshNet.Security.Cryptography
@@ -39,7 +38,7 @@ namespace Renci.SshNet.Security.Cryptography
var encryptedSignature = _cipher.Decrypt(signature);
var hashData = Hash(input);
var expected = DerEncode(hashData);
return expected.SequenceEqual(encryptedSignature);
return expected.IsEqualTo(encryptedSignature);
}
/// <summary>
@@ -57,7 +56,7 @@ namespace Renci.SshNet.Security.Cryptography
// Calculate DER string
var derEncodedHash = DerEncode(hashData);
return _cipher.Encrypt(derEncodedHash).TrimLeadingZero().ToArray();
return _cipher.Encrypt(derEncodedHash).TrimLeadingZeros();
}
/// <summary>
@@ -1,4 +1,4 @@
using System.Linq;
using Renci.SshNet.Common;
namespace Renci.SshNet.Security.Cryptography.Ciphers
{
@@ -39,7 +39,7 @@ namespace Renci.SshNet.Security.Cryptography.Ciphers
{
Cipher = cipher;
_blockSize = cipher.BlockSize;
IV = IV.Take(_blockSize).ToArray();
IV = IV.Take(_blockSize);
}
/// <summary>
@@ -1,5 +1,4 @@
using System;
using System.Linq;
using System.Security.Cryptography;
using Renci.SshNet.Common;
@@ -42,7 +41,7 @@ namespace Renci.SshNet.Security.Cryptography
{
var hashInput = _hash.ComputeHash(input);
var hm = new BigInteger(hashInput.Reverse().Concat(new byte[] { 0 }).ToArray());
var hm = new BigInteger(hashInput.Reverse().Concat(new byte[] { 0 }));
if (signature.Length != 40)
throw new InvalidOperationException("Invalid signature.");
@@ -98,7 +97,7 @@ namespace Renci.SshNet.Security.Cryptography
{
var hashInput = _hash.ComputeHash(input);
var m = new BigInteger(hashInput.Reverse().Concat(new byte[] { 0 }).ToArray());
var m = new BigInteger(hashInput.Reverse().Concat(new byte[] { 0 }));
BigInteger s;
BigInteger r;
@@ -139,11 +138,11 @@ namespace Renci.SshNet.Security.Cryptography
var signature = new byte[40];
// issue #1918: pad part with zero's on the left if length is less than 20
var rBytes = r.ToByteArray().Reverse().TrimLeadingZero().ToArray();
var rBytes = r.ToByteArray().Reverse().TrimLeadingZeros();
Array.Copy(rBytes, 0, signature, 20 - rBytes.Length, rBytes.Length);
// issue #1918: pad part with zero's on the left if length is less than 20
var sBytes = s.ToByteArray().Reverse().TrimLeadingZero().ToArray();
var sBytes = s.ToByteArray().Reverse().TrimLeadingZeros();
Array.Copy(sBytes, 0, signature, 40 - sBytes.Length, sBytes.Length);
return signature;
@@ -1,6 +1,6 @@
#if FEATURE_HMAC_MD5
using System.Linq;
using Renci.SshNet.Common;
namespace Renci.SshNet.Security.Cryptography
{
@@ -31,7 +31,7 @@ namespace Renci.SshNet.Security.Cryptography
protected override byte[] HashFinal()
{
var hash = base.HashFinal();
return hash.Take(HashSize / 8).ToArray();
return hash.Take(HashSize / 8);
}
}
}
@@ -1,6 +1,6 @@
#if FEATURE_HMAC_SHA1
using System.Linq;
using Renci.SshNet.Common;
namespace Renci.SshNet.Security.Cryptography
{
@@ -28,7 +28,7 @@ namespace Renci.SshNet.Security.Cryptography
protected override byte[] HashFinal()
{
var hash = base.HashFinal();
return hash.Take(HashSize / 8).ToArray();
return hash.Take(HashSize / 8);
}
}
}
@@ -1,6 +1,6 @@
#if FEATURE_HMAC_SHA256
using System.Linq;
using Renci.SshNet.Common;
namespace Renci.SshNet.Security.Cryptography
{
@@ -28,7 +28,7 @@ namespace Renci.SshNet.Security.Cryptography
protected override byte[] HashFinal()
{
var hash = base.HashFinal();
return hash.Take(HashSize / 8).ToArray();
return hash.Take(HashSize / 8);
}
}
}
@@ -1,6 +1,6 @@
#if FEATURE_HMAC_SHA384
using System.Linq;
using Renci.SshNet.Common;
namespace Renci.SshNet.Security.Cryptography
{
@@ -28,7 +28,7 @@ namespace Renci.SshNet.Security.Cryptography
protected override byte[] HashFinal()
{
var hash = base.HashFinal();
return hash.Take(HashSize / 8).ToArray();
return hash.Take(HashSize / 8);
}
}
}
@@ -1,6 +1,6 @@
#if FEATURE_HMAC_SHA512
using System.Linq;
using Renci.SshNet.Common;
namespace Renci.SshNet.Security.Cryptography
{
@@ -28,7 +28,7 @@ namespace Renci.SshNet.Security.Cryptography
protected override byte[] HashFinal()
{
var hash = base.HashFinal();
return hash.Take(HashSize / 8).ToArray();
return hash.Take(HashSize / 8);
}
}
}
@@ -29,7 +29,7 @@ namespace Renci.SshNet.Security
get
{
BigInteger prime;
BigInteger.TryParse(SecondOkleyGroup, NumberStyles.AllowHexSpecifier, CultureInfo.CurrentCulture, out prime);
BigInteger.TryParse(SecondOkleyGroup, NumberStyles.AllowHexSpecifier, NumberFormatInfo.CurrentInfo, out prime);
return prime;
}
}
@@ -29,7 +29,7 @@ namespace Renci.SshNet.Security
get
{
BigInteger prime;
BigInteger.TryParse(SecondOkleyGroup, NumberStyles.AllowHexSpecifier, CultureInfo.CurrentCulture, out prime);
BigInteger.TryParse(SecondOkleyGroup, NumberStyles.AllowHexSpecifier, NumberFormatInfo.CurrentInfo, out prime);
return prime;
}
}
@@ -1,5 +1,4 @@
using System;
using System.Linq;
using Renci.SshNet.Messages.Transport;
using Renci.SshNet.Messages;
using Renci.SshNet.Common;
@@ -85,8 +84,8 @@ namespace Renci.SshNet.Security
{
base.Start(session, message);
_serverPayload = message.GetBytes().ToArray();
_clientPayload = Session.ClientInitMessage.GetBytes().ToArray();
_serverPayload = message.GetBytes();
_clientPayload = Session.ClientInitMessage.GetBytes();
Session.RegisterMessage("SSH_MSG_KEXECDH_REPLY");
@@ -102,14 +101,14 @@ namespace Renci.SshNet.Security
//3. Output (d,Q).
BigInteger p;
BigInteger.TryParse("00FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF", NumberStyles.AllowHexSpecifier, CultureInfo.CurrentCulture, out p);
BigInteger.TryParse("00FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF", NumberStyles.AllowHexSpecifier, NumberFormatInfo.CurrentInfo, out p);
BigInteger n;
BigInteger.TryParse("00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973", NumberStyles.AllowHexSpecifier, CultureInfo.CurrentCulture, out n);
BigInteger.TryParse("00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973", NumberStyles.AllowHexSpecifier, NumberFormatInfo.CurrentInfo, out n);
BigInteger G;
BigInteger.TryParse("00036B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296", NumberStyles.AllowHexSpecifier, CultureInfo.CurrentCulture, out G);
BigInteger.TryParse("00036B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296", NumberStyles.AllowHexSpecifier, NumberFormatInfo.CurrentInfo, out G);
BigInteger d;
+3 -3
View File
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Security.Cryptography;
@@ -16,6 +15,7 @@ using Renci.SshNet.Messages.Connection;
using Renci.SshNet.Messages.Transport;
using Renci.SshNet.Security;
using System.Globalization;
using System.Linq;
using Renci.SshNet.Abstractions;
using Renci.SshNet.Security.Cryptography;
@@ -954,7 +954,7 @@ namespace Renci.SshNet
if (serverHash != null)
{
Buffer.BlockCopy(nextBlocks, nextBlocks.Length - serverHash.Length, serverHash, 0, serverHash.Length);
nextBlocks = nextBlocks.Take(nextBlocks.Length - serverHash.Length).ToArray();
nextBlocks = nextBlocks.Take(nextBlocks.Length - serverHash.Length);
}
if (nextBlocks.Length > 0)
@@ -978,7 +978,7 @@ namespace Renci.SshNet
{
var clientHash = _serverMac.ComputeHash(data);
if (!serverHash.SequenceEqual(clientHash))
if (!serverHash.IsEqualTo(clientHash))
{
throw new SshConnectionException("MAC error", DisconnectReason.MacError);
}
+1 -2
View File
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Renci.SshNet.Channels;
@@ -225,7 +224,7 @@ namespace Renci.SshNet
/// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed.</exception>
public override void Write(byte[] buffer, int offset, int count)
{
foreach (var b in buffer.Skip(offset).Take(count).ToArray())
foreach (var b in buffer.Take(offset, count))
{
if (_outgoing.Count < BufferSize)
{