Skip to content

Commit

Permalink
Add a few spans on net6.0 or greater
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob-Hague committed Jun 4, 2023
1 parent 27976bb commit 7391735
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/Renci.SshNet/Common/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@ public static bool IsEqualTo(this byte[] left, byte[] right)
return true;
}

#if NET6_0_OR_GREATER
return left.AsSpan().SequenceEqual(right);
#else
if (left.Length != right.Length)
{
return false;
Expand All @@ -259,6 +262,7 @@ public static bool IsEqualTo(this byte[] left, byte[] right)
}

return true;
#endif
}

/// <summary>
Expand Down
44 changes: 44 additions & 0 deletions src/Renci.SshNet/Common/SshDataStream.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
#if NET6_0_OR_GREATER
using System.Buffers.Binary;
#endif
using System.Globalization;
using System.IO;
using System.Text;
Expand Down Expand Up @@ -62,8 +65,14 @@ public bool IsEndOfData
/// <param name="value"><see cref="uint"/> data to write.</param>
public void Write(uint value)
{
#if NET6_0_OR_GREATER
Span<byte> bytes = stackalloc byte[4];
BinaryPrimitives.WriteUInt32BigEndian(bytes, value);
Write(bytes);
#else
var bytes = Pack.UInt32ToBigEndian(value);
Write(bytes, 0, bytes.Length);
#endif
}

/// <summary>
Expand All @@ -72,8 +81,14 @@ public void Write(uint value)
/// <param name="value"><see cref="ulong"/> data to write.</param>
public void Write(ulong value)
{
#if NET6_0_OR_GREATER
Span<byte> bytes = stackalloc byte[8];
BinaryPrimitives.WriteUInt64BigEndian(bytes, value);
Write(bytes);
#else
var bytes = Pack.UInt64ToBigEndian(value);
Write(bytes, 0, bytes.Length);
#endif
}

/// <summary>
Expand Down Expand Up @@ -188,8 +203,14 @@ public BigInteger ReadBigInt()
/// </returns>
public uint ReadUInt32()
{
#if NET6_0_OR_GREATER
Span<byte> bytes = stackalloc byte[4];
ReadBytes(bytes);
return BinaryPrimitives.ReadUInt32BigEndian(bytes);
#else
var data = ReadBytes(4);
return Pack.BigEndianToUInt32(data);
#endif
}

/// <summary>
Expand All @@ -200,8 +221,14 @@ public uint ReadUInt32()
/// </returns>
public ulong ReadUInt64()
{
#if NET6_0_OR_GREATER
Span<byte> bytes = stackalloc byte[8];
ReadBytes(bytes);
return BinaryPrimitives.ReadUInt64BigEndian(bytes);
#else
var data = ReadBytes(8);
return Pack.BigEndianToUInt64(data);
#endif
}

/// <summary>
Expand Down Expand Up @@ -264,5 +291,22 @@ private byte[] ReadBytes(int length)

return data;
}

#if NET6_0_OR_GREATER
/// <summary>
/// Fills the specified span with bytes from the internal buffer.
/// </summary>
/// <param name="destination">The span to fill.</param>
/// <exception cref="ArgumentException">The Length of <paramref name="destination"/> is greater than the actual number of bytes read.</exception>
private void ReadBytes(Span<byte> destination)
{
var bytesRead = Read(destination);

if (bytesRead < destination.Length)
{
throw new ArgumentException(nameof(destination), string.Format(CultureInfo.InvariantCulture, "The requested length ({0}) is greater than the actual number of bytes read ({1}).", destination.Length, bytesRead));
}
}
#endif
}
}
13 changes: 10 additions & 3 deletions src/Renci.SshNet/Session.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1254,11 +1254,18 @@ private Message ReceiveMessage(Socket socket)
if (_serverMac != null)
{
var clientHash = _serverMac.ComputeHash(data, 0, data.Length - serverMacLength);

bool serverHashEqualsClientHash;

#if NET6_0_OR_GREATER
var serverHash = data.AsSpan(data.Length - serverMacLength);
serverHashEqualsClientHash = serverHash.SequenceEqual(clientHash);
#else
var serverHash = data.Take(data.Length - serverMacLength, serverMacLength);
serverHashEqualsClientHash = serverHash.IsEqualTo(clientHash);
#endif

// TODO add IsEqualTo overload that takes left+right index and number of bytes to compare;
// TODO that way we can eliminate the extra allocation of the Take above
if (!serverHash.IsEqualTo(clientHash))
if (!serverHashEqualsClientHash)
{
throw new SshConnectionException("MAC error", DisconnectReason.MacError);
}
Expand Down

0 comments on commit 7391735

Please sign in to comment.