From 05d2efd3379314d473f93bde38b48b47a82ec27d Mon Sep 17 00:00:00 2001 From: lidgren Date: Thu, 23 Aug 2012 13:37:28 +0000 Subject: [PATCH] Linq namespace usage removed --- .../Encryption/NetAESEncryption.cs | 26 +++++-------------- .../Encryption/NetDESEncryption.cs | 23 +++++----------- .../Encryption/NetRC2Encryption.cs | 23 +++++----------- .../Encryption/NetTripleDESEncryption.cs | 23 +++++----------- Lidgren.Network/NetBuffer.Read.cs | 1 - Lidgren.Network/NetUtility.cs | 14 ++++++++++ 6 files changed, 39 insertions(+), 71 deletions(-) diff --git a/Lidgren.Network/Encryption/NetAESEncryption.cs b/Lidgren.Network/Encryption/NetAESEncryption.cs index ffb9bdf..d3523b0 100644 --- a/Lidgren.Network/Encryption/NetAESEncryption.cs +++ b/Lidgren.Network/Encryption/NetAESEncryption.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.IO; -using System.Linq; using System.Security.Cryptography; using System.Text; @@ -20,7 +19,6 @@ namespace Lidgren.Network static NetAESEncryption() { - AesCryptoServiceProvider aes = new AesCryptoServiceProvider(); List temp = new List(); foreach (KeySizes keysize in aes.LegalKeySizes) @@ -55,18 +53,11 @@ namespace Lidgren.Network public NetAESEncryption(byte[] key, byte[] iv) { if (!m_keysizes.Contains(key.Length * 8)) - { - string lengths = m_keysizes.Aggregate("", (current, i) => current + string.Format("{0}, ", i)); - lengths = lengths.Remove(lengths.Length - 3); - throw new NetException(string.Format("Not a valid key size. (Valid values are: {0})", lengths)); - } + throw new NetException(string.Format("Not a valid key size. (Valid values are: {0})", NetUtility.MakeCommaDelimitedList(m_keysizes))); if (!m_blocksizes.Contains(iv.Length * 8)) - { - string lengths = m_blocksizes.Aggregate("", (current, i) => current + string.Format("{0}, ", i)); - lengths = lengths.Remove(lengths.Length - 3); - throw new NetException(string.Format("Not a valid iv size. (Valid values are: {0})", lengths)); - } + throw new NetException(string.Format("Not a valid iv size. (Valid values are: {0})", NetUtility.MakeCommaDelimitedList(m_blocksizes))); + m_key = key; m_iv = iv; m_bitSize = m_key.Length * 8; @@ -78,11 +69,8 @@ namespace Lidgren.Network public NetAESEncryption(string key, int bitsize) { if (!m_keysizes.Contains(bitsize)) - { - string lengths = m_keysizes.Aggregate("", (current, i) => current + string.Format("{0}, ", i)); - lengths = lengths.Remove(lengths.Length - 3); - throw new NetException(string.Format("Not a valid key size. (Valid values are: {0})", lengths)); - } + throw new NetException(string.Format("Not a valid key size. (Valid values are: {0})", NetUtility.MakeCommaDelimitedList(m_keysizes))); + byte[] entropy = Encoding.UTF32.GetBytes(key); // I know hardcoding salts is bad, but in this case I think it is acceptable. HMACSHA512 hmacsha512 = new HMACSHA512(Convert.FromBase64String("i88NEiez3c50bHqr3YGasDc4p8jRrxJAaiRiqixpvp4XNAStP5YNoC2fXnWkURtkha6M8yY901Gj07IRVIRyGL==")); @@ -104,7 +92,7 @@ namespace Lidgren.Network /// NetAESEncryption constructor /// public NetAESEncryption(string key) - : this(key, m_keysizes.Max()) + : this(key, m_keysizes[0]) { } @@ -172,4 +160,4 @@ namespace Lidgren.Network return true; } } -} \ No newline at end of file +} diff --git a/Lidgren.Network/Encryption/NetDESEncryption.cs b/Lidgren.Network/Encryption/NetDESEncryption.cs index d212468..4e9ef09 100644 --- a/Lidgren.Network/Encryption/NetDESEncryption.cs +++ b/Lidgren.Network/Encryption/NetDESEncryption.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.IO; -using System.Linq; using System.Security.Cryptography; using System.Text; @@ -55,18 +54,11 @@ namespace Lidgren.Network public NetDESEncryption(byte[] key, byte[] iv) { if (!m_keysizes.Contains(key.Length * 8)) - { - string lengths = m_keysizes.Aggregate("", (current, i) => current + string.Format("{0}, ", i)); - lengths = lengths.Remove(lengths.Length - 3); - throw new NetException(string.Format("Not a valid key size. (Valid values are: {0})", lengths)); - } + throw new NetException(string.Format("Not a valid key size. (Valid values are: {0})", NetUtility.MakeCommaDelimitedList(m_keysizes))); if (!m_blocksizes.Contains(iv.Length * 8)) - { - string lengths = m_blocksizes.Aggregate("", (current, i) => current + string.Format("{0}, ", i)); - lengths = lengths.Remove(lengths.Length - 3); - throw new NetException(string.Format("Not a valid iv size. (Valid values are: {0})", lengths)); - } + throw new NetException(string.Format("Not a valid iv size. (Valid values are: {0})", NetUtility.MakeCommaDelimitedList(m_blocksizes))); + m_key = key; m_iv = iv; m_bitSize = m_key.Length * 8; @@ -78,11 +70,8 @@ namespace Lidgren.Network public NetDESEncryption(string key, int bitsize) { if (!m_keysizes.Contains(bitsize)) - { - string lengths = m_keysizes.Aggregate("", (current, i) => current + string.Format("{0}, ", i)); - lengths = lengths.Remove(lengths.Length - 3); - throw new NetException(string.Format("Not a valid key size. (Valid values are: {0})", lengths)); - } + throw new NetException(string.Format("Not a valid key size. (Valid values are: {0})", NetUtility.MakeCommaDelimitedList(m_keysizes))); + byte[] entropy = Encoding.UTF32.GetBytes(key); // I know hardcoding salts is bad, but in this case I think it is acceptable. HMACSHA512 hmacsha512 = new HMACSHA512(Convert.FromBase64String("i88NEiez3c50bHqr3YGasDc4p8jRrxJAaiRiqixpvp4XNAStP5YNoC2fXnWkURtkha6M8yY901Gj07IRVIRyGL==")); @@ -104,7 +93,7 @@ namespace Lidgren.Network /// NetDESEncryption constructor /// public NetDESEncryption(string key) - : this(key, m_keysizes.Max()) + : this(key, m_keysizes[0]) { } diff --git a/Lidgren.Network/Encryption/NetRC2Encryption.cs b/Lidgren.Network/Encryption/NetRC2Encryption.cs index a65d234..c5f36f8 100644 --- a/Lidgren.Network/Encryption/NetRC2Encryption.cs +++ b/Lidgren.Network/Encryption/NetRC2Encryption.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.IO; -using System.Linq; using System.Security.Cryptography; using System.Text; @@ -55,18 +54,11 @@ namespace Lidgren.Network public NetRC2Encryption(byte[] key, byte[] iv) { if (!m_keysizes.Contains(key.Length * 8)) - { - string lengths = m_keysizes.Aggregate("", (current, i) => current + string.Format("{0}, ", i)); - lengths = lengths.Remove(lengths.Length - 3); - throw new NetException(string.Format("Not a valid key size. (Valid values are: {0})", lengths)); - } + throw new NetException(string.Format("Not a valid key size. (Valid values are: {0})", NetUtility.MakeCommaDelimitedList(m_keysizes))); if (!m_blocksizes.Contains(iv.Length * 8)) - { - string lengths = m_blocksizes.Aggregate("", (current, i) => current + string.Format("{0}, ", i)); - lengths = lengths.Remove(lengths.Length - 3); - throw new NetException(string.Format("Not a valid iv size. (Valid values are: {0})", lengths)); - } + throw new NetException(string.Format("Not a valid iv size. (Valid values are: {0})", NetUtility.MakeCommaDelimitedList(m_blocksizes))); + m_key = key; m_iv = iv; m_bitSize = m_key.Length * 8; @@ -78,11 +70,8 @@ namespace Lidgren.Network public NetRC2Encryption(string key, int bitsize) { if (!m_keysizes.Contains(bitsize)) - { - string lengths = m_keysizes.Aggregate("", (current, i) => current + string.Format("{0}, ", i)); - lengths = lengths.Remove(lengths.Length - 3); - throw new NetException(string.Format("Not a valid key size. (Valid values are: {0})", lengths)); - } + throw new NetException(string.Format("Not a valid key size. (Valid values are: {0})", NetUtility.MakeCommaDelimitedList(m_keysizes))); + byte[] entropy = Encoding.UTF32.GetBytes(key); // I know hardcoding salts is bad, but in this case I think it is acceptable. HMACSHA512 hmacsha512 = new HMACSHA512(Convert.FromBase64String("i88NEiez3c50bHqr3YGasDc4p8jRrxJAaiRiqixpvp4XNAStP5YNoC2fXnWkURtkha6M8yY901Gj07IRVIRyGL==")); @@ -105,7 +94,7 @@ namespace Lidgren.Network /// /// public NetRC2Encryption(string key) - : this(key, m_keysizes.Max()) + : this(key, m_keysizes[0]) { } diff --git a/Lidgren.Network/Encryption/NetTripleDESEncryption.cs b/Lidgren.Network/Encryption/NetTripleDESEncryption.cs index a1bfd10..0995399 100644 --- a/Lidgren.Network/Encryption/NetTripleDESEncryption.cs +++ b/Lidgren.Network/Encryption/NetTripleDESEncryption.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.IO; -using System.Linq; using System.Security.Cryptography; using System.Text; @@ -55,18 +54,11 @@ namespace Lidgren.Network public NetTripleDESEncryption(byte[] key, byte[] iv) { if (!m_keysizes.Contains(key.Length * 8)) - { - string lengths = m_keysizes.Aggregate("", (current, i) => current + string.Format("{0}, ", i)); - lengths = lengths.Remove(lengths.Length - 3); - throw new NetException(string.Format("Not a valid key size. (Valid values are: {0})", lengths)); - } + throw new NetException(string.Format("Not a valid key size. (Valid values are: {0})", NetUtility.MakeCommaDelimitedList(m_keysizes))); if (!m_blocksizes.Contains(iv.Length * 8)) - { - string lengths = m_blocksizes.Aggregate("", (current, i) => current + string.Format("{0}, ", i)); - lengths = lengths.Remove(lengths.Length - 3); - throw new NetException(string.Format("Not a valid iv size. (Valid values are: {0})", lengths)); - } + throw new NetException(string.Format("Not a valid iv size. (Valid values are: {0})", NetUtility.MakeCommaDelimitedList(m_blocksizes))); + m_key = key; m_iv = iv; m_bitSize = m_key.Length * 8; @@ -78,11 +70,8 @@ namespace Lidgren.Network public NetTripleDESEncryption(string key, int bitsize) { if (!m_keysizes.Contains(bitsize)) - { - string lengths = m_keysizes.Aggregate("", (current, i) => current + string.Format("{0}, ", i)); - lengths = lengths.Remove(lengths.Length - 3); - throw new NetException(string.Format("Not a valid key size. (Valid values are: {0})", lengths)); - } + throw new NetException(string.Format("Not a valid key size. (Valid values are: {0})", NetUtility.MakeCommaDelimitedList(m_keysizes))); + byte[] entropy = Encoding.UTF32.GetBytes(key); // I know hardcoding salts is bad, but in this case I think it is acceptable. HMACSHA512 hmacsha512 = new HMACSHA512(Convert.FromBase64String("i88NEiez3c50bHqr3YGasDc4p8jRrxJAaiRiqixpvp4XNAStP5YNoC2fXnWkURtkha6M8yY901Gj07IRVIRyGL==")); @@ -104,7 +93,7 @@ namespace Lidgren.Network /// NetTriplsDESEncryption constructor /// public NetTripleDESEncryption(string key) - : this(key, m_keysizes.Max()) + : this(key, m_keysizes[0]) { } diff --git a/Lidgren.Network/NetBuffer.Read.cs b/Lidgren.Network/NetBuffer.Read.cs index 57b0d31..0d98d86 100644 --- a/Lidgren.Network/NetBuffer.Read.cs +++ b/Lidgren.Network/NetBuffer.Read.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Linq; using System.Text; using System.Reflection; using System.Net; diff --git a/Lidgren.Network/NetUtility.cs b/Lidgren.Network/NetUtility.cs index 8b4d292..5c4e0a3 100644 --- a/Lidgren.Network/NetUtility.cs +++ b/Lidgren.Network/NetUtility.cs @@ -26,6 +26,7 @@ using System.Net.NetworkInformation; using System.Net.Sockets; using System.Text; using System.Text.RegularExpressions; +using System.Collections.Generic; namespace Lidgren.Network { @@ -555,5 +556,18 @@ namespace Lidgren.Network return NetDeliveryMethod.UnreliableSequenced; return NetDeliveryMethod.Unreliable; } + + public static string MakeCommaDelimitedList(IList list) + { + var cnt = list.Count; + StringBuilder bdr = new StringBuilder(cnt * 5); // educated guess + for(int i=0;i