Sfoglia il codice sorgente

Minor code performance improvment, replace Array.Copy with Buffer.BlockCopy

olegkap_cp 14 anni fa
parent
commit
b52679c0ef

+ 5 - 3
Renci.SshClient/Renci.SshNet/Common/BigInteger.cs

@@ -44,6 +44,7 @@
 * ***************************************************************************/
 
 using System;
+using System.Linq;
 using System.Collections.Generic;
 using System.Diagnostics;
 using System.Diagnostics.CodeAnalysis;
@@ -93,10 +94,10 @@ namespace Renci.SshNet.Common
                     return 0;
 
                 var msbIndex = this._data.Length - 1;
-                
+
                 while (this._data[msbIndex] == 0)
                     msbIndex--;
-                
+
                 var msbBitCount = BitScanBackward(this._data[msbIndex]) + 1;
 
                 return msbIndex * 4 * 8 + msbBitCount + ((this._sign > 0) ? 0 : 1);
@@ -3123,6 +3124,7 @@ namespace Renci.SshNet.Common
         private static byte[] Resize(byte[] v, int len)
         {
             byte[] res = new byte[len];
+            Buffer.BlockCopy(v, 0, res, 0, Math.Min(v.Length, len));
             Array.Copy(v, res, Math.Min(v.Length, len));
             return res;
         }
@@ -3130,7 +3132,7 @@ namespace Renci.SshNet.Common
         private static uint[] Resize(uint[] v, int len)
         {
             uint[] res = new uint[len];
-            Array.Copy(v, res, Math.Min(v.Length, len));
+            Buffer.BlockCopy(v, 0, res, 0, Math.Min(v.Length, len) * sizeof(uint));
             return res;
         }
 

+ 2 - 1
Renci.SshClient/Renci.SshNet/Common/DerData.cs

@@ -199,7 +199,8 @@ namespace Renci.SshNet.Common
         {
             var temp = new ulong[identifier.Identifiers.Length - 1];
             temp[0] = identifier.Identifiers[0] * 40 + identifier.Identifiers[1];
-            Array.Copy(identifier.Identifiers, 2, temp, 1, identifier.Identifiers.Length - 2);
+            Buffer.BlockCopy(identifier.Identifiers, 2 * sizeof(ulong), temp, 1 * sizeof(ulong), (identifier.Identifiers.Length - 2) * sizeof(ulong));
+            //Array.Copy(identifier.Identifiers, 2, temp, 1, identifier.Identifiers.Length - 2);
             var bytes = new List<byte>();
             foreach (var subidentifier in temp)
             {

+ 3 - 9
Renci.SshClient/Renci.SshNet/Security/Cryptography/CipherDigitalSignature.cs

@@ -44,16 +44,10 @@ namespace Renci.SshNet.Security.Cryptography
 
             var expected = DerEncode(hashData);
 
-            if (expected.Length != encryptedSignature.Length)
+            if (expected.SequenceEqual(encryptedSignature))
+                return true;
+            else
                 return false;
-
-            for (int i = 0; i < expected.Length; i++)
-            {
-                if (expected[i] != encryptedSignature[i])
-                    return false;
-            }
-
-            return true;
         }
 
         /// <summary>

+ 5 - 5
Renci.SshClient/Renci.SshNet/Security/Cryptography/Ciphers/BlowfishCipher.cs

@@ -441,12 +441,12 @@ namespace Renci.SshNet.Security.Cryptography.Ciphers
 			* Initialise the S-boxes and the P-array, with a fixed string
 			* This string contains the hexadecimal digits of pi (3.141...)
 			*/
-			Array.Copy(KS0, 0, S0, 0, SBOX_SK);
-			Array.Copy(KS1, 0, S1, 0, SBOX_SK);
-			Array.Copy(KS2, 0, S2, 0, SBOX_SK);
-			Array.Copy(KS3, 0, S3, 0, SBOX_SK);
+			Buffer.BlockCopy(KS0, 0, S0, 0, SBOX_SK * sizeof(uint));
+			Buffer.BlockCopy(KS1, 0, S1, 0, SBOX_SK * sizeof(uint));
+			Buffer.BlockCopy(KS2, 0, S2, 0, SBOX_SK * sizeof(uint));
+			Buffer.BlockCopy(KS3, 0, S3, 0, SBOX_SK * sizeof(uint));
 
-			Array.Copy(KP, 0, P, 0, P_SZ);
+			Buffer.BlockCopy(KP, 0, P, 0, P_SZ * sizeof(uint));
 
 			/*
 			* (2)

+ 3 - 2
Renci.SshClient/Renci.SshNet/Security/Cryptography/Ciphers/Modes/CbcCipherMode.cs

@@ -49,7 +49,8 @@ namespace Renci.SshNet.Security.Cryptography.Ciphers.Modes
 
             this.Cipher.EncryptBlock(this.IV, 0, inputCount, outputBuffer, outputOffset);
 
-            Array.Copy(outputBuffer, outputOffset, this.IV, 0, this.IV.Length);
+            Buffer.BlockCopy(outputBuffer, outputOffset, this.IV, 0, this.IV.Length);
+
 
             return this._blockSize;
         }
@@ -83,7 +84,7 @@ namespace Renci.SshNet.Security.Cryptography.Ciphers.Modes
                 outputBuffer[outputOffset + i] ^= this.IV[i];
             }
 
-            Array.Copy(inputBuffer, inputOffset, this.IV, 0, this.IV.Length);
+            Buffer.BlockCopy(inputBuffer, inputOffset, this.IV, 0, this.IV.Length);
 
             return this._blockSize;
         }

+ 4 - 4
Renci.SshClient/Renci.SshNet/Security/Cryptography/Ciphers/Modes/CfbCipherMode.cs

@@ -52,8 +52,8 @@ namespace Renci.SshNet.Security.Cryptography.Ciphers.Modes
                 outputBuffer[outputOffset + i] = (byte)(this._ivOutput[i] ^ inputBuffer[inputOffset + i]);
             }
 
-            Array.Copy(this.IV, this._blockSize, this.IV, 0, this.IV.Length - this._blockSize);
-            Array.Copy(outputBuffer, outputOffset, this.IV, this.IV.Length - this._blockSize, this._blockSize);
+            Buffer.BlockCopy(this.IV, this._blockSize, this.IV, 0, this.IV.Length - this._blockSize);
+            Buffer.BlockCopy(outputBuffer, outputOffset, this.IV, this.IV.Length - this._blockSize, this._blockSize);
 
             return this._blockSize;
         }
@@ -82,8 +82,8 @@ namespace Renci.SshNet.Security.Cryptography.Ciphers.Modes
 
             this.Cipher.EncryptBlock(this.IV, 0, this.IV.Length, this._ivOutput, 0);
 
-            Array.Copy(this.IV, this._blockSize, this.IV, 0, this.IV.Length - this._blockSize);
-            Array.Copy(inputBuffer, inputOffset, this.IV, this.IV.Length - this._blockSize, this._blockSize);
+            Buffer.BlockCopy(this.IV, this._blockSize, this.IV, 0, this.IV.Length - this._blockSize);
+            Buffer.BlockCopy(inputBuffer, inputOffset, this.IV, this.IV.Length - this._blockSize, this._blockSize);
 
             for (int i = 0; i < this._blockSize; i++)
             {

+ 4 - 4
Renci.SshClient/Renci.SshNet/Security/Cryptography/Ciphers/Modes/OfbCipherMode.cs

@@ -53,8 +53,8 @@ namespace Renci.SshNet.Security.Cryptography.Ciphers.Modes
                 outputBuffer[outputOffset + i] = (byte)(this._ivOutput[i] ^ inputBuffer[inputOffset + i]);
             }
 
-            Array.Copy(this.IV, this._blockSize, this.IV, 0, this.IV.Length - this._blockSize);
-            Array.Copy(outputBuffer, outputOffset, this.IV, this.IV.Length - this._blockSize, this._blockSize);
+            Buffer.BlockCopy(this.IV, this._blockSize, this.IV, 0, this.IV.Length - this._blockSize);
+            Buffer.BlockCopy(outputBuffer, outputOffset, this.IV, this.IV.Length - this._blockSize, this._blockSize);
 
             return this._blockSize;
         }
@@ -88,8 +88,8 @@ namespace Renci.SshNet.Security.Cryptography.Ciphers.Modes
                 outputBuffer[outputOffset + i] = (byte)(this._ivOutput[i] ^ inputBuffer[inputOffset + i]);
             }
 
-            Array.Copy(this.IV, this._blockSize, this.IV, 0, this.IV.Length - this._blockSize);
-            Array.Copy(outputBuffer, outputOffset, this.IV, this.IV.Length - this._blockSize, this._blockSize);
+            Buffer.BlockCopy(this.IV, this._blockSize, this.IV, 0, this.IV.Length - this._blockSize);
+            Buffer.BlockCopy(outputBuffer, outputOffset, this.IV, this.IV.Length - this._blockSize, this._blockSize);
 
             return this._blockSize;
         }

+ 1 - 1
Renci.SshClient/Renci.SshNet/Security/Cryptography/Ciphers/Paddings/PKCS7Padding.cs

@@ -21,7 +21,7 @@ namespace Renci.SshNet.Security.Cryptography.Ciphers.Paddings
             var numOfPaddedBytes = blockSize - (input.Length % blockSize);
 
             var output = new byte[input.Length + numOfPaddedBytes];
-            Array.Copy(input, output, input.Length);
+            Buffer.BlockCopy(input, 0, output, 0, input.Length);
             for (int i = 0; i < numOfPaddedBytes; i++)
             {
                 output[input.Length + i] = output[input.Length - 1];

+ 2 - 2
Renci.SshClient/Renci.SshNet/Security/Cryptography/Ciphers/RsaCipher.cs

@@ -47,7 +47,7 @@ namespace Renci.SshNet.Security.Cryptography.Ciphers
                 paddedBlock[i] = 0xFF;
             }
 
-            Array.Copy(data, 0, paddedBlock, paddedBlock.Length - data.Length, data.Length);
+            Buffer.BlockCopy(data, 0, paddedBlock, paddedBlock.Length - data.Length, data.Length);
 
             return this.Transform(paddedBlock);
         }
@@ -72,7 +72,7 @@ namespace Renci.SshNet.Security.Cryptography.Ciphers
 
             var result = new byte[paddedBlock.Length - position];
 
-            Array.Copy(paddedBlock, position, result, 0, result.Length);
+            Buffer.BlockCopy(paddedBlock, position, result, 0, result.Length);
 
             return result;
         }

+ 1 - 1
Renci.SshClient/Renci.SshNet/Security/Cryptography/Ciphers/SerpentCipher.cs

@@ -239,7 +239,7 @@ namespace Renci.SshNet.Security.Cryptography.Ciphers
 				kPad[i] = RotateLeft(kPad[i - 8] ^ kPad[i - 5] ^ kPad[i - 3] ^ kPad[i - 1] ^ PHI ^ (i - 8), 11);
 			}
 
-			Array.Copy(kPad, 8, w, 0, 8);
+            Buffer.BlockCopy(kPad, 8, w, 0, 8);
 
 			//
 			// compute w8 to w136

+ 6 - 6
Renci.SshClient/Renci.SshNet/Security/Cryptography/Ciphers/TripleDesCipher.cs

@@ -54,8 +54,8 @@ namespace Renci.SshNet.Security.Cryptography.Ciphers
                 var part1 = new byte[8];
                 var part2 = new byte[8];
 
-                Array.Copy(this.Key, 0, part1, 0, 8);
-                Array.Copy(this.Key, 8, part2, 0, 8);
+                Buffer.BlockCopy(this.Key, 0, part1, 0, 8);
+                Buffer.BlockCopy(this.Key, 8, part2, 0, 8);
 
                 this._encryptionKey1 = this.GenerateWorkingKey(true, part1);
 
@@ -64,7 +64,7 @@ namespace Renci.SshNet.Security.Cryptography.Ciphers
                 if (this.Key.Length == 24)
                 {
                     var part3 = new byte[8];
-                    Array.Copy(this.Key, 16, part3, 0, 8);
+                    Buffer.BlockCopy(this.Key, 16, part3, 0, 8);
 
                     this._encryptionKey3 = this.GenerateWorkingKey(true, part3);
                 }
@@ -107,8 +107,8 @@ namespace Renci.SshNet.Security.Cryptography.Ciphers
                 var part1 = new byte[8];
                 var part2 = new byte[8];
 
-                Array.Copy(this.Key, 0, part1, 0, 8);
-                Array.Copy(this.Key, 8, part2, 0, 8);
+                Buffer.BlockCopy(this.Key, 0, part1, 0, 8);
+                Buffer.BlockCopy(this.Key, 8, part2, 0, 8);
 
                 this._decryptionKey1 = this.GenerateWorkingKey(false, part1);
                 this._decryptionKey2 = this.GenerateWorkingKey(true, part2);
@@ -116,7 +116,7 @@ namespace Renci.SshNet.Security.Cryptography.Ciphers
                 if (this.Key.Length == 24)
                 {
                     var part3 = new byte[8];
-                    Array.Copy(this.Key, 16, part3, 0, 8);
+                    Buffer.BlockCopy(this.Key, 16, part3, 0, 8);
 
                     this._decryptionKey3 = this.GenerateWorkingKey(false, part3);
                 }

+ 2 - 1
Renci.SshClient/Renci.SshNet/Security/Cryptography/RsaDigitalSignature.cs

@@ -13,7 +13,7 @@ namespace Renci.SshNet.Security.Cryptography
     /// </summary>
     public class RsaDigitalSignature : CipherDigitalSignature, IDisposable
     {
-        private HashAlgorithm _hash = new SHA1Hash();
+        private HashAlgorithm _hash;
 
         /// <summary>
         /// Initializes a new instance of the <see cref="RsaDigitalSignature"/> class.
@@ -22,6 +22,7 @@ namespace Renci.SshNet.Security.Cryptography
         public RsaDigitalSignature(RsaKey rsaKey)
             : base(new ObjectIdentifier(1, 3, 14, 3, 2, 26), new RsaCipher(rsaKey))
         {
+            this._hash = new SHA1Hash();
         }
 
         /// <summary>

+ 2 - 2
Renci.SshClient/Renci.SshNet/Session.cs

@@ -761,7 +761,7 @@ namespace Renci.SshNet
 
                 if (serverHash != null)
                 {
-                    Array.Copy(nextBlocks, nextBlocks.Length - serverHash.Length, serverHash, 0, serverHash.Length);
+                    Buffer.BlockCopy(nextBlocks, nextBlocks.Length - serverHash.Length, serverHash, 0, serverHash.Length);
                     nextBlocks = nextBlocks.Take(nextBlocks.Length - serverHash.Length).ToArray();
                 }
 
@@ -778,7 +778,7 @@ namespace Renci.SshNet
             var paddingLength = data[4];
 
             var messagePayload = new byte[packetLength - paddingLength - 1];
-            Array.Copy(data, 5, messagePayload, 0, messagePayload.Length);
+            Buffer.BlockCopy(data, 5, messagePayload, 0, messagePayload.Length);
 
             if (this._serverDecompression != null)
             {

+ 2 - 2
Renci.SshClient/Renci.SshNet/Sftp/SftpFileStream.cs

@@ -514,7 +514,7 @@ namespace Renci.SshNet.Sftp
                     }
 
                     // Copy stream data to the caller's buffer.
-                    Array.Copy(this._buffer, this._bufferPosn, buffer, offset, tempLen);
+                    Buffer.BlockCopy(this._buffer, this._bufferPosn, buffer, offset, tempLen);
 
                     // Advance to the next buffer positions.
                     readLen += tempLen;
@@ -827,7 +827,7 @@ namespace Renci.SshNet.Sftp
                     else
                     {
                         // No: copy the data to the write buffer first.
-                        Array.Copy(buffer, offset, this._buffer, this._bufferPosn, tempLen);
+                        Buffer.BlockCopy(buffer, offset, this._buffer, this._bufferPosn, tempLen);
                         this._bufferPosn += tempLen;
                     }
 

+ 1 - 1
Renci.SshClient/Renci.SshNet/SftpClient.cs

@@ -1232,7 +1232,7 @@ namespace Renci.SshNet
                 if (bytesRead < this.BufferSize)
                 {
                     var data = new byte[bytesRead];
-                    Array.Copy(buffer, data, bytesRead);
+                    Buffer.BlockCopy(buffer, 0, data, 0, bytesRead); 
                     using (var wait = new AutoResetEvent(false))
                     {
                         this._sftpSession.RequestWrite(handle, offset, data, wait);