Răsfoiți Sursa

Added Pad overloads taking offset and length.

drieseng 9 ani în urmă
părinte
comite
ad3de364fd

+ 45 - 26
src/Renci.SshNet.Tests/Classes/Security/Cryptography/Ciphers/Paddings/PKCS5PaddingTest.cs

@@ -1,42 +1,61 @@
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Renci.SshNet.Security.Cryptography.Ciphers.Paddings;
-using Renci.SshNet.Tests.Common;
 
 namespace Renci.SshNet.Tests.Classes.Security.Cryptography.Ciphers.Paddings
 {
-    /// <summary>
-    ///This is a test class for PKCS5PaddingTest and is intended
-    ///to contain all PKCS5PaddingTest Unit Tests
-    ///</summary>
     [TestClass]
-    public class PKCS5PaddingTest : TestBase
+    public class PKCS5PaddingTest
     {
-        /// <summary>
-        ///A test for Pad
-        ///</summary>
+        private PKCS5Padding _padding;
+
+        [TestInitialize]
+        public void SetUp()
+        {
+            _padding = new PKCS5Padding();
+        }
+
+        [TestMethod]
+        public void Pad_BlockSizeAndInput_LessThanBlockSize()
+        {
+            var input = new byte[] {0x01, 0x02, 0x03, 0x04, 0x05};
+            var expected = new byte[] {0x01, 0x02, 0x03, 0x04, 0x05, 0x03, 0x03, 0x03};
+
+            var actual = _padding.Pad(8, input);
+
+            Assert.IsTrue(expected.IsEqualTo(actual));
+        }
+
         [TestMethod]
-        [Ignore] // placeholder for actual test
-        public void PadTest()
+        public void Pad_BlockSizeAndInput_MoreThanBlockSizeButNoMultipleOfBlockSize()
         {
-            PKCS5Padding target = new PKCS5Padding(); // TODO: Initialize to an appropriate value
-            int blockSize = 0; // TODO: Initialize to an appropriate value
-            byte[] input = null; // TODO: Initialize to an appropriate value
-            byte[] expected = null; // TODO: Initialize to an appropriate value
-            byte[] actual;
-            actual = target.Pad(blockSize, input);
-            Assert.AreEqual(expected, actual);
-            Assert.Inconclusive("Verify the correctness of this test method.");
+            var input = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 };
+            var expected = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07 };
+
+            var actual = _padding.Pad(8, input);
+
+            Assert.IsTrue(expected.IsEqualTo(actual));
+        }
+
+        [TestMethod]
+        public void Pad_BlockSizeAndInputAndOffsetAndLength_LessThanBlockSize()
+        {
+            var input = new byte[] { 0x0f, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
+            var expected = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x03, 0x03, 0x03 };
+
+            var actual = _padding.Pad(8, input, 1, input.Length - 2);
+
+            Assert.IsTrue(expected.IsEqualTo(actual));
         }
 
-        /// <summary>
-        ///A test for PKCS5Padding Constructor
-        ///</summary>
         [TestMethod]
-        [Ignore] // placeholder
-        public void PKCS5PaddingConstructorTest()
+        public void Pad_BlockSizeAndInputAndOffsetAndLength_MoreThanBlockSizeButNoMultipleOfBlockSize()
         {
-            PKCS5Padding target = new PKCS5Padding();
-            Assert.Inconclusive("TODO: Implement code to verify target");
+            var input = new byte[] { 0x0f, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10 };
+            var expected = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07 };
+
+            var actual = _padding.Pad(8, input, 1, input.Length - 2);
+
+            Assert.IsTrue(expected.IsEqualTo(actual));
         }
     }
 }

+ 46 - 27
src/Renci.SshNet.Tests/Classes/Security/Cryptography/Ciphers/Paddings/PKCS7PaddingTest.cs

@@ -1,42 +1,61 @@
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Renci.SshNet.Security.Cryptography.Ciphers.Paddings;
-using Renci.SshNet.Tests.Common;
 
 namespace Renci.SshNet.Tests.Classes.Security.Cryptography.Ciphers.Paddings
 {
-    /// <summary>
-    ///This is a test class for PKCS7PaddingTest and is intended
-    ///to contain all PKCS7PaddingTest Unit Tests
-    ///</summary>
-    [TestClass()]
-    public class PKCS7PaddingTest : TestBase
+    [TestClass]
+    public class PKCS7PaddingTest
     {
-        /// <summary>
-        ///A test for Pad
-        ///</summary>
+        private PKCS7Padding _padding;
+
+        [TestInitialize]
+        public void SetUp()
+        {
+            _padding = new PKCS7Padding();
+        }
+
+        [TestMethod]
+        public void Pad_BlockSizeAndInput_LessThanBlockSize()
+        {
+            var input = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05 };
+            var expected = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x03, 0x03, 0x03 };
+
+            var actual = _padding.Pad(8, input);
+
+            Assert.IsTrue(expected.IsEqualTo(actual));
+        }
+
         [TestMethod]
-        [Ignore] // placeholder for actual test
-        public void PadTest()
+        public void Pad_BlockSizeAndInput_MoreThanBlockSizeButNoMultipleOfBlockSize()
         {
-            PKCS7Padding target = new PKCS7Padding(); // TODO: Initialize to an appropriate value
-            int blockSize = 0; // TODO: Initialize to an appropriate value
-            byte[] input = null; // TODO: Initialize to an appropriate value
-            byte[] expected = null; // TODO: Initialize to an appropriate value
-            byte[] actual;
-            actual = target.Pad(blockSize, input);
-            Assert.AreEqual(expected, actual);
-            Assert.Inconclusive("Verify the correctness of this test method.");
+            var input = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 };
+            var expected = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07 };
+
+            var actual = _padding.Pad(8, input);
+
+            Assert.IsTrue(expected.IsEqualTo(actual));
+        }
+
+        [TestMethod]
+        public void Pad_BlockSizeAndInputAndOffsetAndLength_LessThanBlockSize()
+        {
+            var input = new byte[] { 0x0f, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
+            var expected = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x03, 0x03, 0x03 };
+
+            var actual = _padding.Pad(8, input, 1, input.Length - 2);
+
+            Assert.IsTrue(expected.IsEqualTo(actual));
         }
 
-        /// <summary>
-        ///A test for PKCS7Padding Constructor
-        ///</summary>
         [TestMethod]
-        [Ignore] // placeholder
-        public void PKCS7PaddingConstructorTest()
+        public void Pad_BlockSizeAndInputAndOffsetAndLength_MoreThanBlockSizeButNoMultipleOfBlockSize()
         {
-            PKCS7Padding target = new PKCS7Padding();
-            Assert.Inconclusive("TODO: Implement code to verify target");
+            var input = new byte[] { 0x0f, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10 };
+            var expected = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07 };
+
+            var actual = _padding.Pad(8, input, 1, input.Length - 2);
+
+            Assert.IsTrue(expected.IsEqualTo(actual));
         }
     }
 }

+ 38 - 6
src/Renci.SshNet/Security/Cryptography/Ciphers/CipherPadding.cs

@@ -6,21 +6,53 @@
     public abstract class CipherPadding
     {
         /// <summary>
-        /// Pads specified input to match block size.
+        /// Pads the specified input to match the block size.
         /// </summary>
         /// <param name="blockSize">Size of the block.</param>
         /// <param name="input">The input.</param>
-        /// <returns>Padded data array.</returns>
-        public abstract byte[] Pad(int blockSize, byte[] input);
+        /// <returns>
+        /// Padded data array.
+        /// </returns>
+        public byte[] Pad(int blockSize, byte[] input)
+        {
+            return Pad(blockSize, input, 0, input.Length);
+        }
+
+        /// <summary>
+        /// Pads the specified input to match the block size.
+        /// </summary>
+        /// <param name="blockSize">Size of the block.</param>
+        /// <param name="input">The input.</param>
+        /// <param name="offset">The zero-based offset in <paramref name="input"/> at which the data to pad starts.</param>
+        /// <param name="length">The number of bytes in <paramref name="input"/> to take into account.</param>
+        /// <returns>
+        /// The padded data array.
+        /// </returns>
+        public abstract byte[] Pad(int blockSize, byte[] input, int offset, int length);
+
+        /// <summary>
+        /// Pads the specified input with a given number of bytes.
+        /// </summary>
+        /// <param name="input">The input.</param>
+        /// <param name="paddinglength">The number of bytes to pad the input with.</param>
+        /// <returns>
+        /// The padded data array.
+        /// </returns>
+        public byte[] Pad(byte[] input, int paddinglength)
+        {
+            return Pad(input, 0, input.Length, paddinglength);
+        }
 
         /// <summary>
-        /// Pads specified input with a given number of bytes to match the block size.
+        /// Pads the specified input with a given number of bytes.
         /// </summary>
         /// <param name="input">The input.</param>
-        /// <param name="length">The number of bytes to pad the input with.</param>
+        /// <param name="offset">The zero-based offset in <paramref name="input"/> at which the data to pad starts.</param>
+        /// <param name="length">The number of bytes in <paramref name="input"/> to take into account.</param>
+        /// <param name="paddinglength">The number of bytes to pad the input with.</param>
         /// <returns>
         /// The padded data array.
         /// </returns>
-        public abstract byte[] Pad(byte[] input, int length);
+        public abstract byte[] Pad(byte[] input, int offset, int length, int paddinglength);
     }
 }

+ 17 - 13
src/Renci.SshNet/Security/Cryptography/Ciphers/Paddings/PKCS5Padding.cs

@@ -8,34 +8,38 @@ namespace Renci.SshNet.Security.Cryptography.Ciphers.Paddings
     public class PKCS5Padding : CipherPadding
     {
         /// <summary>
-        /// Transforms the specified input.
+        /// Pads the specified input to match the block size.
         /// </summary>
-        /// <param name="blockSize">Size of the block.</param>
+        /// <param name="blockSize">The size of the block.</param>
         /// <param name="input">The input.</param>
+        /// <param name="offset">The zero-based offset in <paramref name="input"/> at which the data to pad starts.</param>
+        /// <param name="length">The number of bytes in <paramref name="input"/> to take into account.</param>
         /// <returns>
-        /// Padded data array.
+        /// The padded data array.
         /// </returns>
-        public override byte[] Pad(int blockSize, byte[] input)
+        public override byte[] Pad(int blockSize, byte[] input, int offset, int length)
         {
-            var numOfPaddedBytes = blockSize - (input.Length % blockSize);
-            return Pad(input, numOfPaddedBytes);
+            var numOfPaddedBytes = blockSize - (length % blockSize);
+            return Pad(input, offset, length, numOfPaddedBytes);
         }
 
         /// <summary>
-        /// Pads specified input with a given number of bytes to match the block size.
+        /// Pads the specified input with a given number of bytes.
         /// </summary>
         /// <param name="input">The input.</param>
-        /// <param name="length">The number of bytes to pad the input with.</param>
+        /// <param name="offset">The zero-based offset in <paramref name="input"/> at which the data to pad starts.</param>
+        /// <param name="length">The number of bytes in <paramref name="input"/> to take into account.</param>
+        /// <param name="paddinglength">The number of bytes to pad the input with.</param>
         /// <returns>
         /// The padded data array.
         /// </returns>
-        public override byte[] Pad(byte[] input, int length)
+        public override byte[] Pad(byte[] input, int offset, int length, int paddinglength)
         {
-            var output = new byte[input.Length + length];
-            Buffer.BlockCopy(input, 0, output, 0, input.Length);
-            for (var i = 0; i < length; i++)
+            var output = new byte[length + paddinglength];
+            Buffer.BlockCopy(input, offset, output, 0, length);
+            for (var i = 0; i < paddinglength; i++)
             {
-                output[input.Length + i] = (byte) length;
+                output[length + i] = (byte) paddinglength;
             }
             return output;
         }

+ 17 - 14
src/Renci.SshNet/Security/Cryptography/Ciphers/Paddings/PKCS7Padding.cs

@@ -8,36 +8,39 @@ namespace Renci.SshNet.Security.Cryptography.Ciphers.Paddings
     public class PKCS7Padding : CipherPadding
     {
         /// <summary>
-        /// Transforms the specified input.
+        /// Pads the specified input to match the block size.
         /// </summary>
-        /// <param name="blockSize">Size of the block.</param>
+        /// <param name="blockSize">The size of the block.</param>
         /// <param name="input">The input.</param>
+        /// <param name="offset">The zero-based offset in <paramref name="input"/> at which the data to pad starts.</param>
+        /// <param name="length">The number of bytes in <paramref name="input"/> to take into account.</param>
         /// <returns>
-        /// Padded data array.
+        /// The padded data array.
         /// </returns>
-        public override byte[] Pad(int blockSize, byte[] input)
+        public override byte[] Pad(int blockSize, byte[] input, int offset, int length)
         {
-            var numOfPaddedBytes = blockSize - (input.Length % blockSize);
-            return Pad(input, numOfPaddedBytes);
+            var numOfPaddedBytes = blockSize - (length % blockSize);
+            return Pad(input, offset, length, numOfPaddedBytes);
         }
 
         /// <summary>
-        /// Pads specified input with a given number of bytes to match the block size.
+        /// Pads the specified input with a given number of bytes.
         /// </summary>
         /// <param name="input">The input.</param>
-        /// <param name="length">The number of bytes to pad the input with.</param>
+        /// <param name="offset">The zero-based offset in <paramref name="input"/> at which the data to pad starts.</param>
+        /// <param name="length">The number of bytes in <paramref name="input"/> to take into account.</param>
+        /// <param name="paddinglength">The number of bytes to pad the input with.</param>
         /// <returns>
         /// The padded data array.
         /// </returns>
-        public override byte[] Pad(byte[] input, int length)
+        public override byte[] Pad(byte[] input, int offset, int length, int paddinglength)
         {
-            var output = new byte[input.Length + length];
-            Buffer.BlockCopy(input, 0, output, 0, input.Length);
-            for (var i = 0; i < length; i++)
+            var output = new byte[length + paddinglength];
+            Buffer.BlockCopy(input, offset, output, 0, length);
+            for (var i = 0; i < paddinglength; i++)
             {
-                output[input.Length + i] = (byte) length;
+                output[length + i] = (byte) paddinglength;
             }
-
             return output;
         }
     }