Skip to content

Commit

Permalink
Applied patch from Anonymous user (https://sourceforge.net/p/acryptoh…
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreyRusyaev committed Aug 27, 2021
1 parent 81cc436 commit f8a046b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="BaseHashTest.cs" />
<Compile Include="BlockAlgorithmTests.cs" />
<Compile Include="CounterTests\IntCounterTest.cs" />
<Compile Include="Haval\Haval128Pass3Test.cs" />
<Compile Include="Haval\Haval128Pass4Test.cs" />
Expand Down
39 changes: 39 additions & 0 deletions src/ACryptoHashNet.UnitTests/BlockAlgorithmTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Security.Cryptography;
using System.Text;

using NUnit.Framework;

namespace Home.Andir.Cryptography.UnitTests
{
[TestFixture]
[Category("BlockAlgorithmTests")]
public class BlockAlgorithmTests
{
[Test]
public static void BlockAlgorithmTest()
{
byte[] b1 = Encoding.UTF8.GetBytes("hello");
byte[] b2 = Encoding.UTF8.GetBytes(" world");

HashAlgorithm sha1Managed = new SHA1Managed();
sha1Managed.TransformBlock(b1, 0, b1.Length, null, 0);
var expected =
sha1Managed.TransformFinalBlock(b2, 0, b2.Length);

HashAlgorithm sha1 = new SHA1();
sha1.TransformBlock(b1, 0, b1.Length, null, 0);
byte[] actual =
sha1.TransformFinalBlock(b2, 0, b2.Length);

if (expected.Length != actual.Length)
{
Assert.Fail("Final block has wrong lengh");
}

for (int ii = 0; ii < expected.Length; ii++)
{
Assert.AreEqual(expected[ii], actual[ii], string.Format("Final block differ at {0} position", ii));
}
}
}
}
33 changes: 21 additions & 12 deletions src/ACryptoHashNet/Base/BlockHashAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,33 +67,42 @@ protected override void HashCore(byte[] array, int offset, int length)
int currentOffset = offset;
int currentLength = length;

if (lastBlockLength > 0)
int lastBlockRemaining = lastBlock.Length - lastBlockLength;
if (lastBlockLength > 0 && length >= lastBlockRemaining)
{
// we were left with a partial block and we have enough data to fill it now
Buffer.BlockCopy(
array, currentOffset,
lastBlock, lastBlockLength,
lastBlock.Length - lastBlockLength
);
array,
currentOffset,
lastBlock,
lastBlockLength,
lastBlockRemaining
);

ProcessBlock(lastBlock, 0);

currentOffset += lastBlockLength;
currentLength -= lastBlockLength;
currentOffset += lastBlockRemaining;
currentLength -= lastBlockRemaining;

lastBlockLength = 0;
}

int blockCount = currentLength / BlockSize;
lastBlockLength = currentLength % BlockSize;

for (int ii = 0; ii < blockCount; ii++, currentOffset += BlockSize)
ProcessBlock(array, currentOffset);

if (lastBlockLength != 0)
int blockBoundaryOffset = currentLength % BlockSize;
if (blockBoundaryOffset != 0) // current data length does not fall on a block boundary
{
Buffer.BlockCopy(
array, currentOffset,
lastBlock, 0,
lastBlockLength);
array,
currentOffset,
lastBlock,
lastBlockLength,
blockBoundaryOffset);
lastBlockLength += blockBoundaryOffset;
}
}

/// <summary>
Expand Down

0 comments on commit f8a046b

Please sign in to comment.