Skip to content

Commit 211f644

Browse files
committed
Add implementation for Ascon Hash
1 parent 9eb2196 commit 211f644

15 files changed

+1478
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.Text;
3+
using Algorithms.Crypto.Digests;
4+
using FluentAssertions;
5+
using NUnit.Framework;
6+
7+
namespace Algorithms.Tests.Crypto.Digests;
8+
9+
[NonParallelizable]
10+
public class AsconDigestTests
11+
{
12+
private readonly AsconDigest asconHash = new AsconDigest(AsconDigest.AsconParameters.AsconHash);
13+
private readonly AsconDigest asconHashA = new AsconDigest(AsconDigest.AsconParameters.AsconHashA);
14+
15+
[TestCase("a", "02a9d471afab12914197af7090f00d16c41b6e30be0a63bbfd00bc13064de548")]
16+
[TestCase("abc", "d37fe9f1d10dbcfad8408a6804dbe91124a8912693322bb23ec1701e19e3fd51")]
17+
[TestCase("Hello","d80f38d94ad72bd18718879f753a44870e8446925ff64bd7441db5fe020b6c0c")]
18+
[TestCase("message digest","e8848979c5adfd21bfcf29e54be1dd085ee523d251e8e6876f2654d6368da0ca")]
19+
[TestCase("abcdefghijklmnopqrstuvwxyz","c62368674e1b2301f19f46c50bb7f87a988a3e41205d68ab9d7882d2a15e917b")]
20+
[TestCase("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "4ff71928d740524735b5ab12bb1598463054f88089f3c5f9760b6bdcd23f897b")]
21+
[TestCase("12345678901234567890123456789012345678901234567890123456789012345678901234567890", "2dae8b553b93841120e88ee77b9ccb8b512a32318db6012025f3f1c482b1def8")]
22+
public void AsconHash_ReturnsCorrectValue(string input, string expected)
23+
{
24+
var inputBytes = Encoding.ASCII.GetBytes(input);
25+
var result = asconHash.Digest(inputBytes);
26+
27+
result.Should().Be(expected);
28+
}
29+
30+
[TestCase("a", "062bb0346671da00da4f460308b4d2c4d9877c3e2827d6229ff5361332d36527")]
31+
[TestCase("abc", "836a5ddba0142b011ce3425ea9789fd6a21628d619195a48c1540f847667a84e")]
32+
[TestCase("Hello","15f245df8af697dc540e86083822809ab7299575d8ad6c2e17ecc603a7ab79dd")]
33+
[TestCase("message digest","3f18a1f398a40a77e0e9477aa6cb50e9e1abecff651c1874f9717c02c8a165ba")]
34+
[TestCase("abcdefghijklmnopqrstuvwxyz","406b809260f361e12dcf0bf924bfe1ffd2f987fc18d90b94fc544ff80dc2946b")]
35+
[TestCase("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "5c6c69ff3ee83361391b7236c8eb6718f52df43de5a61a4f4d2819d40430dc19")]
36+
[TestCase("12345678901234567890123456789012345678901234567890123456789012345678901234567890", "d8e38fc50d682550cd176decda61adb7fd1c793cdafa825f17f3a002d65847be")]
37+
public void AsconHashA_ReturnsCorrectValue(string input, string expected)
38+
{
39+
var inputBytes = Encoding.ASCII.GetBytes(input);
40+
var result = asconHashA.Digest(inputBytes);
41+
42+
result.Should().Be(expected);
43+
}
44+
45+
private static string ToHexString(byte[] bytes)
46+
{
47+
return BitConverter.ToString(bytes).Replace("-", "").ToLower();
48+
}
49+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using Algorithms.Crypto.Exceptions;
2+
using NUnit.Framework;
3+
using FluentAssertions;
4+
using System;
5+
6+
7+
namespace Algorithms.Tests.Crypto.Exceptions
8+
{
9+
[TestFixture]
10+
public class CryptoExceptionTests
11+
{
12+
[Test]
13+
public void CryptoException_ShouldBeCreatedWithoutMessageOrInnerException()
14+
{
15+
// Act
16+
var exception = new CryptoException();
17+
18+
// Assert
19+
exception.Should().BeOfType<CryptoException>()
20+
.And.Subject.As<CryptoException>()
21+
.Message.Should().NotBeNullOrEmpty();
22+
exception.InnerException.Should().BeNull();
23+
}
24+
25+
[Test]
26+
public void CryptoException_ShouldSetMessage()
27+
{
28+
// Arrange
29+
var expectedMessage = "This is a custom cryptographic error.";
30+
31+
// Act
32+
var exception = new CryptoException(expectedMessage);
33+
34+
// Assert
35+
exception.Should().BeOfType<CryptoException>()
36+
.And.Subject.As<CryptoException>()
37+
.Message.Should().Be(expectedMessage);
38+
exception.InnerException.Should().BeNull();
39+
}
40+
41+
[Test]
42+
public void CryptoException_ShouldSetMessageAndInnerException()
43+
{
44+
// Arrange
45+
var expectedMessage = "An error occurred during encryption.";
46+
var innerException = new InvalidOperationException("Invalid operation");
47+
48+
// Act
49+
var exception = new CryptoException(expectedMessage, innerException);
50+
51+
// Assert
52+
exception.Should().BeOfType<CryptoException>()
53+
.And.Subject.As<CryptoException>()
54+
.Message.Should().Be(expectedMessage);
55+
exception.InnerException.Should().Be(innerException);
56+
}
57+
58+
[Test]
59+
public void CryptoException_MessageShouldNotBeNullWhenUsingDefaultConstructor()
60+
{
61+
// Act
62+
var exception = new CryptoException();
63+
64+
// Assert
65+
exception.Message.Should().NotBeNullOrEmpty(); // Even the default Exception message is not null or empty.
66+
}
67+
}
68+
}
69+
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using NUnit.Framework;
2+
using FluentAssertions;
3+
using System;
4+
using Algorithms.Crypto.Exceptions;
5+
6+
namespace Algorithms.Tests.Crypto.Exceptions
7+
{
8+
[TestFixture]
9+
public class DataLengthExceptionTests
10+
{
11+
[Test]
12+
public void DataLengthException_ShouldBeCreatedWithoutMessageOrInnerException()
13+
{
14+
// Act
15+
var exception = new DataLengthException();
16+
17+
// Assert
18+
exception.Should().BeOfType<DataLengthException>()
19+
.And.Subject.As<DataLengthException>()
20+
.Message.Should().NotBeNullOrEmpty();
21+
exception.InnerException.Should().BeNull();
22+
}
23+
24+
[Test]
25+
public void DataLengthException_ShouldSetMessage()
26+
{
27+
// Arrange
28+
var expectedMessage = "Data length is invalid.";
29+
30+
// Act
31+
var exception = new DataLengthException(expectedMessage);
32+
33+
// Assert
34+
exception.Should().BeOfType<DataLengthException>()
35+
.And.Subject.As<DataLengthException>()
36+
.Message.Should().Be(expectedMessage);
37+
exception.InnerException.Should().BeNull();
38+
}
39+
40+
[Test]
41+
public void DataLengthException_ShouldSetMessageAndInnerException()
42+
{
43+
// Arrange
44+
var expectedMessage = "An error occurred due to incorrect data length.";
45+
var innerException = new ArgumentException("Invalid argument");
46+
47+
// Act
48+
var exception = new DataLengthException(expectedMessage, innerException);
49+
50+
// Assert
51+
exception.Should().BeOfType<DataLengthException>()
52+
.And.Subject.As<DataLengthException>()
53+
.Message.Should().Be(expectedMessage);
54+
exception.InnerException.Should().Be(innerException);
55+
}
56+
57+
[Test]
58+
public void DataLengthException_MessageShouldNotBeNullWhenUsingDefaultConstructor()
59+
{
60+
// Act
61+
var exception = new DataLengthException();
62+
63+
// Assert
64+
exception.Message.Should().NotBeNullOrEmpty(); // Even the default Exception message is not null or empty.
65+
}
66+
}
67+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using NUnit.Framework;
2+
using FluentAssertions;
3+
using System;
4+
using Algorithms.Crypto.Exceptions;
5+
6+
namespace Algorithms.Tests.Crypto.Exceptions
7+
{
8+
[TestFixture]
9+
public class OutputLengthExceptionTests
10+
{
11+
[Test]
12+
public void OutputLengthException_ShouldBeCreatedWithoutMessageOrInnerException()
13+
{
14+
// Act
15+
var exception = new OutputLengthException();
16+
17+
// Assert
18+
exception.Should().BeOfType<OutputLengthException>()
19+
.And.Subject.As<OutputLengthException>()
20+
.Message.Should().NotBeNullOrEmpty();
21+
exception.InnerException.Should().BeNull();
22+
}
23+
24+
[Test]
25+
public void OutputLengthException_ShouldSetMessage()
26+
{
27+
// Arrange
28+
var expectedMessage = "Output buffer is too short.";
29+
30+
// Act
31+
var exception = new OutputLengthException(expectedMessage);
32+
33+
// Assert
34+
exception.Should().BeOfType<OutputLengthException>()
35+
.And.Subject.As<OutputLengthException>()
36+
.Message.Should().Be(expectedMessage);
37+
exception.InnerException.Should().BeNull();
38+
}
39+
40+
[Test]
41+
public void OutputLengthException_ShouldSetMessageAndInnerException()
42+
{
43+
// Arrange
44+
var expectedMessage = "Output length error.";
45+
var innerException = new ArgumentException("Invalid argument");
46+
47+
// Act
48+
var exception = new OutputLengthException(expectedMessage, innerException);
49+
50+
// Assert
51+
exception.Should().BeOfType<OutputLengthException>()
52+
.And.Subject.As<OutputLengthException>()
53+
.Message.Should().Be(expectedMessage);
54+
exception.InnerException.Should().Be(innerException);
55+
}
56+
57+
[Test]
58+
public void OutputLengthException_MessageShouldNotBeNullWhenUsingDefaultConstructor()
59+
{
60+
// Act
61+
var exception = new OutputLengthException();
62+
63+
// Assert
64+
exception.Message.Should().NotBeNullOrEmpty(); // Even the default Exception message is not null or empty.
65+
}
66+
}
67+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using NUnit.Framework;
2+
using FluentAssertions;
3+
using System;
4+
using Algorithms.Crypto.Utils;
5+
6+
namespace Algorithms.Tests.Crypto.Utils
7+
{
8+
[TestFixture]
9+
public class ByteEncodingUtilsTests
10+
{
11+
[Test]
12+
public void BigEndianToUint64_ByteArray_ShouldConvertCorrectly()
13+
{
14+
// Arrange
15+
byte[] input = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF };
16+
var expected = 0x0123456789ABCDEFUL;
17+
18+
// Act
19+
var result = ByteEncodingUtils.BigEndianToUint64(input, 0);
20+
21+
// Assert
22+
result.Should().Be(expected);
23+
}
24+
25+
[Test]
26+
public void BigEndianToUint64_ByteArray_WithOffset_ShouldConvertCorrectly()
27+
{
28+
// Arrange
29+
byte[] input = { 0x00, 0x00, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF };
30+
var expected = 0x0123456789ABCDEFUL;
31+
32+
// Act
33+
var result = ByteEncodingUtils.BigEndianToUint64(input, 2);
34+
35+
// Assert
36+
result.Should().Be(expected);
37+
}
38+
39+
[Test]
40+
public void BigEndianToUint64_Span_ShouldConvertCorrectly()
41+
{
42+
// Arrange
43+
Span<byte> input = stackalloc byte[] { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF };
44+
var expected = 0x0123456789ABCDEFUL;
45+
46+
// Act
47+
var result = ByteEncodingUtils.BigEndianToUint64(input);
48+
49+
// Assert
50+
result.Should().Be(expected);
51+
}
52+
53+
[Test]
54+
public void UInt64ToBigEndian_ShouldWriteCorrectly()
55+
{
56+
// Arrange
57+
var value = 0x0123456789ABCDEFUL;
58+
Span<byte> output = stackalloc byte[8];
59+
byte[] expected = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF };
60+
61+
// Act
62+
ByteEncodingUtils.UInt64ToBigEndian(value, output);
63+
64+
// Assert
65+
output.ToArray().Should().Equal(expected);
66+
}
67+
68+
[Test]
69+
public void BigEndianToUint64_InvalidOffset_ShouldThrowException()
70+
{
71+
// Arrange
72+
byte[] input = { 0x01, 0x23 };
73+
74+
// Act
75+
Action act = () => ByteEncodingUtils.BigEndianToUint64(input, 1);
76+
77+
// Assert
78+
act.Should().Throw<ArgumentOutOfRangeException>();
79+
}
80+
}
81+
}

0 commit comments

Comments
 (0)