Browse Source

Better code

Shadowsocks.Protocol
pull/3084/head
Bruce Wayne 3 years ago
parent
commit
c354852482
No known key found for this signature in database GPG Key ID: 6D396097F05051BF
4 changed files with 8 additions and 17 deletions
  1. +4
    -4
      Shadowsocks.Protocol/Shadowsocks/AeadBlockMessage.cs
  2. +1
    -1
      Shadowsocks.Protocol/Socks5/Socks5UserPasswordRequestMessage.cs
  3. +1
    -1
      Shadowsocks.Protocol/Socks5/Socks5VersionIdentifierMessage.cs
  4. +2
    -11
      Shadowsocks.Protocol/Util.cs

+ 4
- 4
Shadowsocks.Protocol/Shadowsocks/AeadBlockMessage.cs View File

@@ -31,10 +31,10 @@ namespace Shadowsocks.Protocol.Shadowsocks
m.Span[0] = (byte)(Data.Length / 256); m.Span[0] = (byte)(Data.Length / 256);
m.Span[1] = (byte)(Data.Length % 256); m.Span[1] = (byte)(Data.Length % 256);
var len1 = aead.Encrypt(nonce.Span, m.Span, buffer.Span); var len1 = aead.Encrypt(nonce.Span, m.Span, buffer.Span);
Util.SodiumIncrement(nonce.Span);
nonce.Span.SodiumIncrement();
buffer = buffer.Slice(len1); buffer = buffer.Slice(len1);
aead.Encrypt(nonce.Span, Data.Span, buffer.Span); aead.Encrypt(nonce.Span, Data.Span, buffer.Span);
Util.SodiumIncrement(nonce.Span);
nonce.Span.SodiumIncrement();
return len; return len;
} }


@@ -48,7 +48,7 @@ namespace Shadowsocks.Protocol.Shadowsocks
// decrypt length // decrypt length
Memory<byte> m = new byte[2]; Memory<byte> m = new byte[2];
len = aead.Decrypt(nonce.Span, m.Span, buffer.Span); len = aead.Decrypt(nonce.Span, m.Span, buffer.Span);
Util.SodiumIncrement(nonce.Span);
nonce.Span.SodiumIncrement();
if (len != 2) return (false, 0); if (len != 2) return (false, 0);


expectedDataLength = m.Span[0] * 256 + m.Span[1]; expectedDataLength = m.Span[0] * 256 + m.Span[1];
@@ -61,7 +61,7 @@ namespace Shadowsocks.Protocol.Shadowsocks
var dataBuffer = buffer.Slice(tagLength + 2); var dataBuffer = buffer.Slice(tagLength + 2);
Data = new byte[expectedDataLength]; Data = new byte[expectedDataLength];
len = aead.Decrypt(nonce.Span, Data.Span, dataBuffer.Span); len = aead.Decrypt(nonce.Span, Data.Span, dataBuffer.Span);
Util.SodiumIncrement(nonce.Span);
nonce.Span.SodiumIncrement();
if (len != expectedDataLength) return (false, 0); if (len != expectedDataLength) return (false, 0);
return (true, totalLength); return (true, totalLength);
} }


+ 1
- 1
Shadowsocks.Protocol/Socks5/Socks5UserPasswordRequestMessage.cs View File

@@ -41,7 +41,7 @@ namespace Shadowsocks.Protocol.Socks5
if (ReferenceEquals(this, other)) return true; if (ReferenceEquals(this, other)) return true;
if (other.GetType() != GetType()) return false; if (other.GetType() != GetType()) return false;
var msg = (Socks5UserPasswordRequestMessage) other; var msg = (Socks5UserPasswordRequestMessage) other;
return Util.MemEqual(User, msg.User) && Util.MemEqual(Password, msg.Password);
return User.SequenceEqual(msg.User) && Password.SequenceEqual(msg.Password);
} }
} }
} }

+ 1
- 1
Shadowsocks.Protocol/Socks5/Socks5VersionIdentifierMessage.cs View File

@@ -36,7 +36,7 @@ namespace Shadowsocks.Protocol.Socks5
if (other is null) return false; if (other is null) return false;
if (ReferenceEquals(this, other)) return true; if (ReferenceEquals(this, other)) return true;
if (other.GetType() != GetType()) return false; if (other.GetType() != GetType()) return false;
return Util.MemEqual(Auth, ((Socks5VersionIdentifierMessage) other).Auth);
return Auth.SequenceEqual(((Socks5VersionIdentifierMessage) other).Auth);
} }
} }
} }

+ 2
- 11
Shadowsocks.Protocol/Util.cs View File

@@ -28,18 +28,9 @@ namespace Shadowsocks.Protocol


public static ArgumentException BufferTooSmall(int expected, int actual, string name) => new ArgumentException($"Require {expected} byte buffer, received {actual} byte", name); public static ArgumentException BufferTooSmall(int expected, int actual, string name) => new ArgumentException($"Require {expected} byte buffer, received {actual} byte", name);


public static bool MemEqual(Memory<byte> m1, Memory<byte> m2)
{
if (m1.Length != m2.Length) return false;
for (var i = 0; i < m1.Length; i++)
{
if (m1.Span[i] != m2.Span[i]) return false;
}

return true;
}
public static bool SequenceEqual(this Memory<byte> m1, ReadOnlyMemory<byte> m2) => m1.Span.SequenceEqual(m2.Span);


public static void SodiumIncrement(Span<byte> salt)
public static void SodiumIncrement(this Span<byte> salt)
{ {
for (var i = 0; i < salt.Length; ++i) for (var i = 0; i < salt.Length; ++i)
{ {


Loading…
Cancel
Save