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

Extensions

static class Extensions
Collection of different extension methods.
using Renci.SshNet.Messages; using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Net.Sockets; using System.Numerics; using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; 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 ReadOnlySpan<byte> data) { byte[] array = data.ToArray(); Array.Reverse((Array)array); return new BigInteger(array); } internal static BigInteger ToBigInteger(this byte[] data) { byte[] array = new byte[data.Length]; Buffer.BlockCopy(data, 0, array, 0, data.Length); Array.Reverse((Array)array); return new BigInteger(array); } 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); Array.Reverse((Array)array); return new BigInteger(array); } 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((Array)array); 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 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 left.AsSpan().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 IsConnected(this Socket socket) { return socket?.Connected ?? false; } internal static string Join(this IEnumerable<string> values, string separator) { return string.Join(separator, values); } internal static bool TryAdd<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, TValue value) { if (!dictionary.ContainsKey(key)) { dictionary.Add(key, value); return true; } return false; } internal static bool Remove<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, out TValue value) { if (dictionary.TryGetValue(key, out value)) { dictionary.Remove(key); return true; } value = default(TValue); return false; } internal static ArraySegment<T> Slice<T>(this ArraySegment<T> arraySegment, int index) { return new ArraySegment<T>(arraySegment.Array, arraySegment.Offset + index, arraySegment.Count - index); } internal static ArraySegment<T> Slice<T>(this ArraySegment<T> arraySegment, int index, int count) { return new ArraySegment<T>(arraySegment.Array, arraySegment.Offset + index, count); } internal static T[] ToArray<T>(this ArraySegment<T> arraySegment) { if (arraySegment.Count == 0) return Array.Empty<T>(); T[] array = new T[arraySegment.Count]; Array.Copy(arraySegment.Array, arraySegment.Offset, array, 0, arraySegment.Count); return array; } internal static void ReadExactly(this Stream stream, byte[] buffer, int offset, int count) { int num = 0; while (true) { if (num >= count) return; int num2 = stream.Read(buffer, offset + num, count - num); if (num2 == 0) break; num += num2; } throw new EndOfStreamException(); } internal static Task<T> WaitAsync<T>(this Task<T> task, CancellationToken cancellationToken) { if (task.IsCompleted || !cancellationToken.CanBeCanceled) return task; <>c__DisplayClass25_0<T> <>c__DisplayClass25_; return <>c__DisplayClass25_.<WaitAsync>g__WaitCore|0(); } } }