<PackageReference Include="SSH.NET" Version="2024.2.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.Numerics; using System.Runtime.CompilerServices; using System.Text; using System.Threading; 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); return new BigInteger(array.Reverse()); } return data.ToBigInteger(); } public static byte[] ToByteArray(this BigInteger bigInt, bool isUnsigned = false, bool isBigEndian = false) { byte[] array = bigInt.ToByteArray(); if (isUnsigned && array[array.Length - 1] == 0) array = array.Take(array.Length - 1); if (isBigEndian) array.Reverse(); return array; } public static long GetBitLength(this BigInteger bigint) { return (long)Math.Ceiling(BigInteger.Log((bigint.Sign < 0) ? (-bigint) : (bigint + (BigInteger)1), 2)); } public static byte[] ExportKeyParameter(this BigInteger value, int length) { byte[] array = ToByteArray(value, true, true); if (array.Length < length) { byte[] array2 = new byte[length]; Buffer.BlockCopy(array, 0, array2, length - array.Length, array.Length); return array2; } return array; } public static void SetIgnoringObjectDisposed(this EventWaitHandle waitHandle) { try { waitHandle.Set(); } catch (ObjectDisposedException) { } } internal static T[] Reverse<T>(this T[] array) { Array.Reverse((Array)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 void ValidatePort(this uint value, [System.Runtime.CompilerServices.CallerArgumentExpression("value")] string argument = null) { 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, [System.Runtime.CompilerServices.CallerArgumentExpression("value")] string argument = null) { 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) { ThrowHelper.ThrowIfNull(value, "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) { ThrowHelper.ThrowIfNull(value, "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) { ThrowHelper.ThrowIfNull(left, "left"); ThrowHelper.ThrowIfNull(right, "right"); return MemoryExtensions.AsSpan(left).SequenceEqual(right); } public static byte[] TrimLeadingZeros(this byte[] value) { ThrowHelper.ThrowIfNull(value, "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; } } }