Skip to content

Commit ad3de36

Browse files
committed
Added Pad overloads taking offset and length.
1 parent 344e6e5 commit ad3de36

File tree

5 files changed

+163
-86
lines changed

5 files changed

+163
-86
lines changed
Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,61 @@
11
using Microsoft.VisualStudio.TestTools.UnitTesting;
22
using Renci.SshNet.Security.Cryptography.Ciphers.Paddings;
3-
using Renci.SshNet.Tests.Common;
43

54
namespace Renci.SshNet.Tests.Classes.Security.Cryptography.Ciphers.Paddings
65
{
7-
/// <summary>
8-
///This is a test class for PKCS5PaddingTest and is intended
9-
///to contain all PKCS5PaddingTest Unit Tests
10-
///</summary>
116
[TestClass]
12-
public class PKCS5PaddingTest : TestBase
7+
public class PKCS5PaddingTest
138
{
14-
/// <summary>
15-
///A test for Pad
16-
///</summary>
9+
private PKCS5Padding _padding;
10+
11+
[TestInitialize]
12+
public void SetUp()
13+
{
14+
_padding = new PKCS5Padding();
15+
}
16+
17+
[TestMethod]
18+
public void Pad_BlockSizeAndInput_LessThanBlockSize()
19+
{
20+
var input = new byte[] {0x01, 0x02, 0x03, 0x04, 0x05};
21+
var expected = new byte[] {0x01, 0x02, 0x03, 0x04, 0x05, 0x03, 0x03, 0x03};
22+
23+
var actual = _padding.Pad(8, input);
24+
25+
Assert.IsTrue(expected.IsEqualTo(actual));
26+
}
27+
1728
[TestMethod]
18-
[Ignore] // placeholder for actual test
19-
public void PadTest()
29+
public void Pad_BlockSizeAndInput_MoreThanBlockSizeButNoMultipleOfBlockSize()
2030
{
21-
PKCS5Padding target = new PKCS5Padding(); // TODO: Initialize to an appropriate value
22-
int blockSize = 0; // TODO: Initialize to an appropriate value
23-
byte[] input = null; // TODO: Initialize to an appropriate value
24-
byte[] expected = null; // TODO: Initialize to an appropriate value
25-
byte[] actual;
26-
actual = target.Pad(blockSize, input);
27-
Assert.AreEqual(expected, actual);
28-
Assert.Inconclusive("Verify the correctness of this test method.");
31+
var input = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 };
32+
var expected = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07 };
33+
34+
var actual = _padding.Pad(8, input);
35+
36+
Assert.IsTrue(expected.IsEqualTo(actual));
37+
}
38+
39+
[TestMethod]
40+
public void Pad_BlockSizeAndInputAndOffsetAndLength_LessThanBlockSize()
41+
{
42+
var input = new byte[] { 0x0f, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
43+
var expected = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x03, 0x03, 0x03 };
44+
45+
var actual = _padding.Pad(8, input, 1, input.Length - 2);
46+
47+
Assert.IsTrue(expected.IsEqualTo(actual));
2948
}
3049

31-
/// <summary>
32-
///A test for PKCS5Padding Constructor
33-
///</summary>
3450
[TestMethod]
35-
[Ignore] // placeholder
36-
public void PKCS5PaddingConstructorTest()
51+
public void Pad_BlockSizeAndInputAndOffsetAndLength_MoreThanBlockSizeButNoMultipleOfBlockSize()
3752
{
38-
PKCS5Padding target = new PKCS5Padding();
39-
Assert.Inconclusive("TODO: Implement code to verify target");
53+
var input = new byte[] { 0x0f, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10 };
54+
var expected = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07 };
55+
56+
var actual = _padding.Pad(8, input, 1, input.Length - 2);
57+
58+
Assert.IsTrue(expected.IsEqualTo(actual));
4059
}
4160
}
4261
}
Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,61 @@
11
using Microsoft.VisualStudio.TestTools.UnitTesting;
22
using Renci.SshNet.Security.Cryptography.Ciphers.Paddings;
3-
using Renci.SshNet.Tests.Common;
43

54
namespace Renci.SshNet.Tests.Classes.Security.Cryptography.Ciphers.Paddings
65
{
7-
/// <summary>
8-
///This is a test class for PKCS7PaddingTest and is intended
9-
///to contain all PKCS7PaddingTest Unit Tests
10-
///</summary>
11-
[TestClass()]
12-
public class PKCS7PaddingTest : TestBase
6+
[TestClass]
7+
public class PKCS7PaddingTest
138
{
14-
/// <summary>
15-
///A test for Pad
16-
///</summary>
9+
private PKCS7Padding _padding;
10+
11+
[TestInitialize]
12+
public void SetUp()
13+
{
14+
_padding = new PKCS7Padding();
15+
}
16+
17+
[TestMethod]
18+
public void Pad_BlockSizeAndInput_LessThanBlockSize()
19+
{
20+
var input = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05 };
21+
var expected = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x03, 0x03, 0x03 };
22+
23+
var actual = _padding.Pad(8, input);
24+
25+
Assert.IsTrue(expected.IsEqualTo(actual));
26+
}
27+
1728
[TestMethod]
18-
[Ignore] // placeholder for actual test
19-
public void PadTest()
29+
public void Pad_BlockSizeAndInput_MoreThanBlockSizeButNoMultipleOfBlockSize()
2030
{
21-
PKCS7Padding target = new PKCS7Padding(); // TODO: Initialize to an appropriate value
22-
int blockSize = 0; // TODO: Initialize to an appropriate value
23-
byte[] input = null; // TODO: Initialize to an appropriate value
24-
byte[] expected = null; // TODO: Initialize to an appropriate value
25-
byte[] actual;
26-
actual = target.Pad(blockSize, input);
27-
Assert.AreEqual(expected, actual);
28-
Assert.Inconclusive("Verify the correctness of this test method.");
31+
var input = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 };
32+
var expected = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07 };
33+
34+
var actual = _padding.Pad(8, input);
35+
36+
Assert.IsTrue(expected.IsEqualTo(actual));
37+
}
38+
39+
[TestMethod]
40+
public void Pad_BlockSizeAndInputAndOffsetAndLength_LessThanBlockSize()
41+
{
42+
var input = new byte[] { 0x0f, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
43+
var expected = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x03, 0x03, 0x03 };
44+
45+
var actual = _padding.Pad(8, input, 1, input.Length - 2);
46+
47+
Assert.IsTrue(expected.IsEqualTo(actual));
2948
}
3049

31-
/// <summary>
32-
///A test for PKCS7Padding Constructor
33-
///</summary>
3450
[TestMethod]
35-
[Ignore] // placeholder
36-
public void PKCS7PaddingConstructorTest()
51+
public void Pad_BlockSizeAndInputAndOffsetAndLength_MoreThanBlockSizeButNoMultipleOfBlockSize()
3752
{
38-
PKCS7Padding target = new PKCS7Padding();
39-
Assert.Inconclusive("TODO: Implement code to verify target");
53+
var input = new byte[] { 0x0f, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10 };
54+
var expected = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07 };
55+
56+
var actual = _padding.Pad(8, input, 1, input.Length - 2);
57+
58+
Assert.IsTrue(expected.IsEqualTo(actual));
4059
}
4160
}
4261
}

src/Renci.SshNet/Security/Cryptography/Ciphers/CipherPadding.cs

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,53 @@
66
public abstract class CipherPadding
77
{
88
/// <summary>
9-
/// Pads specified input to match block size.
9+
/// Pads the specified input to match the block size.
1010
/// </summary>
1111
/// <param name="blockSize">Size of the block.</param>
1212
/// <param name="input">The input.</param>
13-
/// <returns>Padded data array.</returns>
14-
public abstract byte[] Pad(int blockSize, byte[] input);
13+
/// <returns>
14+
/// Padded data array.
15+
/// </returns>
16+
public byte[] Pad(int blockSize, byte[] input)
17+
{
18+
return Pad(blockSize, input, 0, input.Length);
19+
}
20+
21+
/// <summary>
22+
/// Pads the specified input to match the block size.
23+
/// </summary>
24+
/// <param name="blockSize">Size of the block.</param>
25+
/// <param name="input">The input.</param>
26+
/// <param name="offset">The zero-based offset in <paramref name="input"/> at which the data to pad starts.</param>
27+
/// <param name="length">The number of bytes in <paramref name="input"/> to take into account.</param>
28+
/// <returns>
29+
/// The padded data array.
30+
/// </returns>
31+
public abstract byte[] Pad(int blockSize, byte[] input, int offset, int length);
32+
33+
/// <summary>
34+
/// Pads the specified input with a given number of bytes.
35+
/// </summary>
36+
/// <param name="input">The input.</param>
37+
/// <param name="paddinglength">The number of bytes to pad the input with.</param>
38+
/// <returns>
39+
/// The padded data array.
40+
/// </returns>
41+
public byte[] Pad(byte[] input, int paddinglength)
42+
{
43+
return Pad(input, 0, input.Length, paddinglength);
44+
}
1545

1646
/// <summary>
17-
/// Pads specified input with a given number of bytes to match the block size.
47+
/// Pads the specified input with a given number of bytes.
1848
/// </summary>
1949
/// <param name="input">The input.</param>
20-
/// <param name="length">The number of bytes to pad the input with.</param>
50+
/// <param name="offset">The zero-based offset in <paramref name="input"/> at which the data to pad starts.</param>
51+
/// <param name="length">The number of bytes in <paramref name="input"/> to take into account.</param>
52+
/// <param name="paddinglength">The number of bytes to pad the input with.</param>
2153
/// <returns>
2254
/// The padded data array.
2355
/// </returns>
24-
public abstract byte[] Pad(byte[] input, int length);
56+
public abstract byte[] Pad(byte[] input, int offset, int length, int paddinglength);
2557
}
2658
}

src/Renci.SshNet/Security/Cryptography/Ciphers/Paddings/PKCS5Padding.cs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,38 @@ namespace Renci.SshNet.Security.Cryptography.Ciphers.Paddings
88
public class PKCS5Padding : CipherPadding
99
{
1010
/// <summary>
11-
/// Transforms the specified input.
11+
/// Pads the specified input to match the block size.
1212
/// </summary>
13-
/// <param name="blockSize">Size of the block.</param>
13+
/// <param name="blockSize">The size of the block.</param>
1414
/// <param name="input">The input.</param>
15+
/// <param name="offset">The zero-based offset in <paramref name="input"/> at which the data to pad starts.</param>
16+
/// <param name="length">The number of bytes in <paramref name="input"/> to take into account.</param>
1517
/// <returns>
16-
/// Padded data array.
18+
/// The padded data array.
1719
/// </returns>
18-
public override byte[] Pad(int blockSize, byte[] input)
20+
public override byte[] Pad(int blockSize, byte[] input, int offset, int length)
1921
{
20-
var numOfPaddedBytes = blockSize - (input.Length % blockSize);
21-
return Pad(input, numOfPaddedBytes);
22+
var numOfPaddedBytes = blockSize - (length % blockSize);
23+
return Pad(input, offset, length, numOfPaddedBytes);
2224
}
2325

2426
/// <summary>
25-
/// Pads specified input with a given number of bytes to match the block size.
27+
/// Pads the specified input with a given number of bytes.
2628
/// </summary>
2729
/// <param name="input">The input.</param>
28-
/// <param name="length">The number of bytes to pad the input with.</param>
30+
/// <param name="offset">The zero-based offset in <paramref name="input"/> at which the data to pad starts.</param>
31+
/// <param name="length">The number of bytes in <paramref name="input"/> to take into account.</param>
32+
/// <param name="paddinglength">The number of bytes to pad the input with.</param>
2933
/// <returns>
3034
/// The padded data array.
3135
/// </returns>
32-
public override byte[] Pad(byte[] input, int length)
36+
public override byte[] Pad(byte[] input, int offset, int length, int paddinglength)
3337
{
34-
var output = new byte[input.Length + length];
35-
Buffer.BlockCopy(input, 0, output, 0, input.Length);
36-
for (var i = 0; i < length; i++)
38+
var output = new byte[length + paddinglength];
39+
Buffer.BlockCopy(input, offset, output, 0, length);
40+
for (var i = 0; i < paddinglength; i++)
3741
{
38-
output[input.Length + i] = (byte) length;
42+
output[length + i] = (byte) paddinglength;
3943
}
4044
return output;
4145
}

src/Renci.SshNet/Security/Cryptography/Ciphers/Paddings/PKCS7Padding.cs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,39 @@ namespace Renci.SshNet.Security.Cryptography.Ciphers.Paddings
88
public class PKCS7Padding : CipherPadding
99
{
1010
/// <summary>
11-
/// Transforms the specified input.
11+
/// Pads the specified input to match the block size.
1212
/// </summary>
13-
/// <param name="blockSize">Size of the block.</param>
13+
/// <param name="blockSize">The size of the block.</param>
1414
/// <param name="input">The input.</param>
15+
/// <param name="offset">The zero-based offset in <paramref name="input"/> at which the data to pad starts.</param>
16+
/// <param name="length">The number of bytes in <paramref name="input"/> to take into account.</param>
1517
/// <returns>
16-
/// Padded data array.
18+
/// The padded data array.
1719
/// </returns>
18-
public override byte[] Pad(int blockSize, byte[] input)
20+
public override byte[] Pad(int blockSize, byte[] input, int offset, int length)
1921
{
20-
var numOfPaddedBytes = blockSize - (input.Length % blockSize);
21-
return Pad(input, numOfPaddedBytes);
22+
var numOfPaddedBytes = blockSize - (length % blockSize);
23+
return Pad(input, offset, length, numOfPaddedBytes);
2224
}
2325

2426
/// <summary>
25-
/// Pads specified input with a given number of bytes to match the block size.
27+
/// Pads the specified input with a given number of bytes.
2628
/// </summary>
2729
/// <param name="input">The input.</param>
28-
/// <param name="length">The number of bytes to pad the input with.</param>
30+
/// <param name="offset">The zero-based offset in <paramref name="input"/> at which the data to pad starts.</param>
31+
/// <param name="length">The number of bytes in <paramref name="input"/> to take into account.</param>
32+
/// <param name="paddinglength">The number of bytes to pad the input with.</param>
2933
/// <returns>
3034
/// The padded data array.
3135
/// </returns>
32-
public override byte[] Pad(byte[] input, int length)
36+
public override byte[] Pad(byte[] input, int offset, int length, int paddinglength)
3337
{
34-
var output = new byte[input.Length + length];
35-
Buffer.BlockCopy(input, 0, output, 0, input.Length);
36-
for (var i = 0; i < length; i++)
38+
var output = new byte[length + paddinglength];
39+
Buffer.BlockCopy(input, offset, output, 0, length);
40+
for (var i = 0; i < paddinglength; i++)
3741
{
38-
output[input.Length + i] = (byte) length;
42+
output[length + i] = (byte) paddinglength;
3943
}
40-
4144
return output;
4245
}
4346
}

0 commit comments

Comments
 (0)