<PackageReference Include="SSH.NET" Version="2023.0.0" />

Extensions

static class Extensions
Collection of different extension methods.
using Renci.SshNet.Abstractions; using Renci.SshNet.Messages; using System; using System.Collections.Generic; using System.Globalization; using System.Net.Sockets; using System.Text; namespace Renci.SshNet.Common { internal static class Extensions { internal static byte[] ToArray(this ServiceName serviceName) { switch (serviceName) { case ServiceName.UserAuthentication: return SshData.Ascii.GetBytes("ssh-userauth"); case ServiceName.Connection: return SshData.Ascii.GetBytes("ssh-connection"); default: throw new NotSupportedException($"""{serviceName}"""); } } internal static ServiceName ToServiceName(this byte[] data) { string string = SshData.Ascii.GetString(data, 0, data.Length); if (string == "ssh-userauth") return ServiceName.UserAuthentication; if (string == "ssh-connection") return ServiceName.Connection; throw new NotSupportedException($"""{string}"""); } internal static BigInteger ToBigInteger(this byte[] data) { byte[] array = new byte[data.Length]; Buffer.BlockCopy(data, 0, array, 0, data.Length); return new BigInteger(array.Reverse()); } public static BigInteger ToBigInteger2(this byte[] data) { if ((data[0] & 128) != 0) { byte[] array = new byte[data.Length + 1]; Buffer.BlockCopy(data, 0, array, 1, data.Length); data = array; } return data.ToBigInteger(); } internal static T[] Reverse<T>(this T[] array) { Array.Reverse(array); return array; } internal static void DebugPrint(this IEnumerable<byte> bytes) { StringBuilder stringBuilder = new StringBuilder(); foreach (byte byte in bytes) { stringBuilder.AppendFormat(CultureInfo.CurrentCulture, "0x{0:x2}, ", byte); } } internal static T CreateInstance<T>(this Type type) where T : class { if ((object)type == null) return null; return Activator.CreateInstance(type) as T; } internal static void ValidatePort(this uint value, string argument) { if (value > 65535) throw new ArgumentOutOfRangeException(argument, string.Format(CultureInfo.InvariantCulture, "Specified value cannot be greater than {0}.", 65535)); } internal static void ValidatePort(this int value, string argument) { if (value < 0) throw new ArgumentOutOfRangeException(argument, string.Format(CultureInfo.InvariantCulture, "Specified value cannot be less than {0}.", 0)); if (value > 65535) throw new ArgumentOutOfRangeException(argument, string.Format(CultureInfo.InvariantCulture, "Specified value cannot be greater than {0}.", 65535)); } public static byte[] Take(this byte[] value, int offset, int count) { if (value == null) throw new ArgumentNullException("value"); if (count == 0) return Array.Empty<byte>(); if (offset == 0 && value.Length == count) return value; byte[] array = new byte[count]; Buffer.BlockCopy(value, offset, array, 0, count); return array; } public static byte[] Take(this byte[] value, int count) { if (value == null) throw new ArgumentNullException("value"); if (count == 0) return Array.Empty<byte>(); if (value.Length == count) return value; byte[] array = new byte[count]; Buffer.BlockCopy(value, 0, array, 0, count); return array; } public static bool IsEqualTo(this byte[] left, byte[] right) { if (left == null) throw new ArgumentNullException("left"); if (right == null) throw new ArgumentNullException("right"); if (left == right) return true; if (left.Length != right.Length) return false; for (int i = 0; i < left.Length; i++) { if (left[i] != right[i]) return false; } return true; } public static byte[] TrimLeadingZeros(this byte[] value) { if (value == null) throw new ArgumentNullException("value"); for (int i = 0; i < value.Length; i++) { if (value[i] != 0) { if (i == 0) return value; int num = value.Length - i; byte[] array = new byte[num]; Buffer.BlockCopy(value, i, array, 0, num); return array; } } return value; } public static byte[] Pad(this byte[] data, int length) { if (length <= data.Length) return data; byte[] array = new byte[length]; Buffer.BlockCopy(data, 0, array, array.Length - data.Length, data.Length); return array; } public static byte[] Concat(this byte[] first, byte[] second) { if (first == null || first.Length == 0) return second; if (second == null || second.Length == 0) return first; byte[] array = new byte[first.Length + second.Length]; Buffer.BlockCopy(first, 0, array, 0, first.Length); Buffer.BlockCopy(second, 0, array, first.Length, second.Length); return array; } internal static bool CanRead(this Socket socket) { return SocketAbstraction.CanRead(socket); } internal static bool CanWrite(this Socket socket) { return SocketAbstraction.CanWrite(socket); } internal static bool IsConnected(this Socket socket) { return socket?.Connected ?? false; } } }