Added Pad overloads taking offset and length.

This commit is contained in:
drieseng
2016-09-04 20:52:23 +02:00
parent 344e6e52c0
commit ad3de364fd
5 changed files with 182 additions and 105 deletions
@@ -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>
[TestMethod]
[Ignore] // placeholder for actual test
public void PadTest()
private PKCS5Padding _padding;
[TestInitialize]
public void SetUp()
{
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.");
_padding = new PKCS5Padding();
}
/// <summary>
///A test for PKCS5Padding Constructor
///</summary>
[TestMethod]
[Ignore] // placeholder
public void PKCS5PaddingConstructorTest()
public void Pad_BlockSizeAndInput_LessThanBlockSize()
{
PKCS5Padding target = new PKCS5Padding();
Assert.Inconclusive("TODO: Implement code to verify target");
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]
public void Pad_BlockSizeAndInput_MoreThanBlockSizeButNoMultipleOfBlockSize()
{
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));
}
[TestMethod]
public void Pad_BlockSizeAndInputAndOffsetAndLength_MoreThanBlockSizeButNoMultipleOfBlockSize()
{
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));
}
}
}
@@ -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>
[TestMethod]
[Ignore] // placeholder for actual test
public void PadTest()
private PKCS7Padding _padding;
[TestInitialize]
public void SetUp()
{
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.");
_padding = new PKCS7Padding();
}
/// <summary>
///A test for PKCS7Padding Constructor
///</summary>
[TestMethod]
[Ignore] // placeholder
public void PKCS7PaddingConstructorTest()
public void Pad_BlockSizeAndInput_LessThanBlockSize()
{
PKCS7Padding target = new PKCS7Padding();
Assert.Inconclusive("TODO: Implement code to verify target");
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]
public void Pad_BlockSizeAndInput_MoreThanBlockSizeButNoMultipleOfBlockSize()
{
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));
}
[TestMethod]
public void Pad_BlockSizeAndInputAndOffsetAndLength_MoreThanBlockSizeButNoMultipleOfBlockSize()
{
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));
}
}
}
@@ -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 specified input with a given number of bytes to match the 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>
/// <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>
/// <returns>
/// The padded data array.
/// </returns>
public abstract byte[] Pad(byte[] input, int length);
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 the specified input with a given number of bytes.
/// </summary>
/// <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>
/// <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 offset, int length, int paddinglength);
}
}
@@ -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>
/// <returns>
/// Padded data array.
/// </returns>
public override byte[] Pad(int blockSize, byte[] input)
{
var numOfPaddedBytes = blockSize - (input.Length % blockSize);
return Pad(input, numOfPaddedBytes);
}
/// <summary>
/// Pads specified input with a given number of bytes to match the block size.
/// </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>
/// <returns>
/// The padded data array.
/// </returns>
public override byte[] Pad(byte[] input, int length)
public override byte[] Pad(int blockSize, byte[] input, int offset, int length)
{
var output = new byte[input.Length + length];
Buffer.BlockCopy(input, 0, output, 0, input.Length);
for (var i = 0; i < length; i++)
var numOfPaddedBytes = blockSize - (length % blockSize);
return Pad(input, offset, length, numOfPaddedBytes);
}
/// <summary>
/// Pads the specified input with a given number of bytes.
/// </summary>
/// <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>
/// <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 offset, int length, int paddinglength)
{
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;
}
@@ -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>
/// <returns>
/// Padded data array.
/// </returns>
public override byte[] Pad(int blockSize, byte[] input)
{
var numOfPaddedBytes = blockSize - (input.Length % blockSize);
return Pad(input, numOfPaddedBytes);
}
/// <summary>
/// Pads specified input with a given number of bytes to match the block size.
/// </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>
/// <returns>
/// The padded data array.
/// </returns>
public override byte[] Pad(byte[] input, int length)
public override byte[] Pad(int blockSize, byte[] input, int offset, int length)
{
var output = new byte[input.Length + length];
Buffer.BlockCopy(input, 0, output, 0, input.Length);
for (var i = 0; i < length; i++)
{
output[input.Length + i] = (byte) length;
}
var numOfPaddedBytes = blockSize - (length % blockSize);
return Pad(input, offset, length, numOfPaddedBytes);
}
/// <summary>
/// Pads the specified input with a given number of bytes.
/// </summary>
/// <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>
/// <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 offset, int length, int paddinglength)
{
var output = new byte[length + paddinglength];
Buffer.BlockCopy(input, offset, output, 0, length);
for (var i = 0; i < paddinglength; i++)
{
output[length + i] = (byte) paddinglength;
}
return output;
}
}