From c354852482d6c4e676e46ad134cc508b9305b59b Mon Sep 17 00:00:00 2001 From: Bruce Wayne Date: Fri, 26 Feb 2021 15:00:20 +0800 Subject: [PATCH] Better code Shadowsocks.Protocol --- .../Shadowsocks/AeadBlockMessage.cs | 8 ++++---- .../Socks5/Socks5UserPasswordRequestMessage.cs | 2 +- .../Socks5/Socks5VersionIdentifierMessage.cs | 2 +- Shadowsocks.Protocol/Util.cs | 13 ++----------- 4 files changed, 8 insertions(+), 17 deletions(-) diff --git a/Shadowsocks.Protocol/Shadowsocks/AeadBlockMessage.cs b/Shadowsocks.Protocol/Shadowsocks/AeadBlockMessage.cs index ffe27bca..e2aa4d5b 100644 --- a/Shadowsocks.Protocol/Shadowsocks/AeadBlockMessage.cs +++ b/Shadowsocks.Protocol/Shadowsocks/AeadBlockMessage.cs @@ -31,10 +31,10 @@ namespace Shadowsocks.Protocol.Shadowsocks m.Span[0] = (byte)(Data.Length / 256); m.Span[1] = (byte)(Data.Length % 256); var len1 = aead.Encrypt(nonce.Span, m.Span, buffer.Span); - Util.SodiumIncrement(nonce.Span); + nonce.Span.SodiumIncrement(); buffer = buffer.Slice(len1); aead.Encrypt(nonce.Span, Data.Span, buffer.Span); - Util.SodiumIncrement(nonce.Span); + nonce.Span.SodiumIncrement(); return len; } @@ -48,7 +48,7 @@ namespace Shadowsocks.Protocol.Shadowsocks // decrypt length Memory m = new byte[2]; len = aead.Decrypt(nonce.Span, m.Span, buffer.Span); - Util.SodiumIncrement(nonce.Span); + nonce.Span.SodiumIncrement(); if (len != 2) return (false, 0); expectedDataLength = m.Span[0] * 256 + m.Span[1]; @@ -61,7 +61,7 @@ namespace Shadowsocks.Protocol.Shadowsocks var dataBuffer = buffer.Slice(tagLength + 2); Data = new byte[expectedDataLength]; len = aead.Decrypt(nonce.Span, Data.Span, dataBuffer.Span); - Util.SodiumIncrement(nonce.Span); + nonce.Span.SodiumIncrement(); if (len != expectedDataLength) return (false, 0); return (true, totalLength); } diff --git a/Shadowsocks.Protocol/Socks5/Socks5UserPasswordRequestMessage.cs b/Shadowsocks.Protocol/Socks5/Socks5UserPasswordRequestMessage.cs index 22050bb3..79629335 100644 --- a/Shadowsocks.Protocol/Socks5/Socks5UserPasswordRequestMessage.cs +++ b/Shadowsocks.Protocol/Socks5/Socks5UserPasswordRequestMessage.cs @@ -41,7 +41,7 @@ namespace Shadowsocks.Protocol.Socks5 if (ReferenceEquals(this, other)) return true; if (other.GetType() != GetType()) return false; var msg = (Socks5UserPasswordRequestMessage) other; - return Util.MemEqual(User, msg.User) && Util.MemEqual(Password, msg.Password); + return User.SequenceEqual(msg.User) && Password.SequenceEqual(msg.Password); } } } \ No newline at end of file diff --git a/Shadowsocks.Protocol/Socks5/Socks5VersionIdentifierMessage.cs b/Shadowsocks.Protocol/Socks5/Socks5VersionIdentifierMessage.cs index 8d1805fe..b3ff91a7 100644 --- a/Shadowsocks.Protocol/Socks5/Socks5VersionIdentifierMessage.cs +++ b/Shadowsocks.Protocol/Socks5/Socks5VersionIdentifierMessage.cs @@ -36,7 +36,7 @@ namespace Shadowsocks.Protocol.Socks5 if (other is null) return false; if (ReferenceEquals(this, other)) return true; if (other.GetType() != GetType()) return false; - return Util.MemEqual(Auth, ((Socks5VersionIdentifierMessage) other).Auth); + return Auth.SequenceEqual(((Socks5VersionIdentifierMessage) other).Auth); } } } \ No newline at end of file diff --git a/Shadowsocks.Protocol/Util.cs b/Shadowsocks.Protocol/Util.cs index 89956696..612c61ca 100644 --- a/Shadowsocks.Protocol/Util.cs +++ b/Shadowsocks.Protocol/Util.cs @@ -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 bool MemEqual(Memory m1, Memory 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 m1, ReadOnlyMemory m2) => m1.Span.SequenceEqual(m2.Span); - public static void SodiumIncrement(Span salt) + public static void SodiumIncrement(this Span salt) { for (var i = 0; i < salt.Length; ++i) {