Skip to content

Commit 7de8efb

Browse files
authored
Add common interface for padding (TheAlgorithms#420)
1 parent 60d4de0 commit 7de8efb

File tree

8 files changed

+59
-10
lines changed

8 files changed

+59
-10
lines changed

Algorithms.Tests/Crypto/Paddings/Pkcs7PaddingTests.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,20 @@ public void RemovePadding_WhenInvalidPadding_ShouldThrowArgumentException()
141141
.WithMessage("Invalid padding");
142142
}
143143

144+
[Test]
145+
public void GetPaddingCount_WhenArrayIsNull_ShouldThrowArgumentNullException()
146+
{
147+
var padding = new Pkcs7Padding(DefaultBlockSize);
148+
149+
Action act = () => padding.GetPaddingCount(null!);
150+
151+
act.Should().Throw<ArgumentNullException>();
152+
}
153+
144154
[Test]
145155
public void GetPaddingCount_WhenInputArrayIsValid_ShouldReturnCorrectPaddingCount()
146156
{
147-
var paddingSize = 5;
157+
const int paddingSize = 5;
148158
var size32Input = new byte[32];
149159

150160
for (var i = 0; i < paddingSize; i++)

Algorithms.Tests/Crypto/Paddings/TbcPaddingTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public void GetPaddingBytes_WhenCalledWithPaddedData_ShouldReturnCorrectPaddingC
128128
var paddedData = new byte[] { 0x01, 0x02, 0x03, 0xff, 0xff };
129129
const int expectedPaddingCount = 2;
130130

131-
var result = padding.GetPaddingBytes(paddedData);
131+
var result = padding.GetPaddingCount(paddedData);
132132

133133
result.Should().Be(expectedPaddingCount);
134134
}
@@ -138,7 +138,7 @@ public void GetPaddingBytes_WhenCalledWithUnpaddedData_ShouldReturnZero()
138138
{
139139
var unpaddedData = new byte[] { 0x01, 0x02, 0x03 };
140140

141-
Action action = () => padding.GetPaddingBytes(unpaddedData);
141+
Action action = () => padding.GetPaddingCount(unpaddedData);
142142

143143
action.Should().Throw<ArgumentException>()
144144
.WithMessage("No padding found");
@@ -149,7 +149,7 @@ public void GetPaddingBytes_WhenCalledWithEmptyArray_ShouldReturnZero()
149149
{
150150
var emptyData = Array.Empty<byte>();
151151

152-
Action action = () => padding.GetPaddingBytes(emptyData);
152+
Action action = () => padding.GetPaddingCount(emptyData);
153153

154154
action.Should().Throw<ArgumentException>()
155155
.WithMessage("No padding found.");
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
3+
namespace Algorithms.Crypto.Paddings;
4+
5+
/// <summary>
6+
/// A common interface that all block cipher padding schemes should follow.
7+
/// </summary>
8+
public interface IBlockCipherPadding
9+
{
10+
/// <summary>
11+
/// Adds padding bytes to the end of the given block of the data and returns the number of bytes that were added.
12+
/// </summary>
13+
/// <param name="inputData">The input data array that needs padding.</param>
14+
/// <param name="inputOffset">The offset in the input array where the padding should start.</param>
15+
/// <returns>The number of bytes added.</returns>
16+
/// <remarks>
17+
/// This method expects that the input parameter <paramref name="inputData"/> contains the last block of plain text
18+
/// that needs to be padded. This means that the value of <paramref name="inputData"/> has to have the same value as
19+
/// the last block of plain text. The reason for this is that some modes such as the <see cref="TbcPadding"/> base the
20+
/// padding value on the last byte of the plain text.
21+
/// </remarks>
22+
public int AddPadding(byte[] inputData, int inputOffset);
23+
24+
/// <summary>
25+
/// Removes the padding bytes from the given block of data and returns the original data as a new array.
26+
/// </summary>
27+
/// <param name="inputData">The input data array containing the padding.</param>
28+
/// <returns>The input data without the padding as a new byte array.</returns>
29+
/// <exception cref="ArgumentException">Thrown when the input data has invalid padding.</exception>
30+
public byte[] RemovePadding(byte[] inputData);
31+
32+
/// <summary>
33+
/// Gets the number of padding bytes in the input data.
34+
/// </summary>
35+
/// <param name="input">The input data array that has padding.</param>
36+
/// <returns>The number of padding bytes in the input data.</returns>
37+
/// <exception cref="ArgumentException">Thrown when the input data has invalid padding.</exception>
38+
public int GetPaddingCount(byte[] input);
39+
}

Algorithms/Crypto/Paddings/Iso10126D2Padding.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace Algorithms.Crypto.Paddings;
1919
/// the end of the data.
2020
/// </para>
2121
/// </summary>
22-
public class Iso10126D2Padding
22+
public class Iso10126D2Padding : IBlockCipherPadding
2323
{
2424
/// <summary>
2525
/// Adds random padding to the input data array to make it a multiple of the block size according to the

Algorithms/Crypto/Paddings/Iso7816D4Padding.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace Algorithms.Crypto.Paddings;
2828
/// depend on any specific character encoding or representation.
2929
/// </para>
3030
/// </summary>
31-
public class Iso7816D4Padding
31+
public class Iso7816D4Padding : IBlockCipherPadding
3232
{
3333
/// <summary>
3434
/// Adds padding to the input data according to the ISO 7816-4 standard.

Algorithms/Crypto/Paddings/Pkcs7Padding.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace Algorithms.Crypto.Paddings;
2020
/// padding, such as AES.
2121
/// </para>
2222
/// </summary>
23-
public class Pkcs7Padding
23+
public class Pkcs7Padding : IBlockCipherPadding
2424
{
2525
private readonly int blockSize;
2626

Algorithms/Crypto/Paddings/TbcPadding.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace Algorithms.Crypto.Paddings;
1515
/// The padding bytes are added at the end of the data block until the desired length is reached.
1616
/// </para>
1717
/// </summary>
18-
public class TbcPadding
18+
public class TbcPadding : IBlockCipherPadding
1919
{
2020
/// <summary>
2121
/// Adds padding to the input array according to the TBC standard.
@@ -121,7 +121,7 @@ public byte[] RemovePadding(byte[] input)
121121
/// avoid branching. If the input array is not padded or has an invalid padding, the method may return incorrect
122122
/// results.
123123
/// </remarks>
124-
public int GetPaddingBytes(byte[] input)
124+
public int GetPaddingCount(byte[] input)
125125
{
126126
var length = input.Length;
127127

Algorithms/Crypto/Paddings/X932Padding.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace Algorithms.Crypto.Paddings;
1818
/// bytes.
1919
/// </para>
2020
/// </summary>
21-
public class X932Padding
21+
public class X932Padding : IBlockCipherPadding
2222
{
2323
private readonly bool useRandomPadding;
2424

0 commit comments

Comments
 (0)