Skip to content

Commit 76d10e0

Browse files
feature/v2.1
- Added async methods for Hash and Hmac types. - Added async tests and tests for identical hash codes. - Updated version to 2.1.0
1 parent 70e5522 commit 76d10e0

34 files changed

+1470
-14
lines changed

OnixLabs.Core.UnitTests/Base16Tests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,21 @@ namespace OnixLabs.Core.UnitTests
1919
{
2020
public sealed class Base16Tests
2121
{
22+
[Fact(DisplayName = "Identical Base16 values produce identical hash codes.")]
23+
public void IdenticalBase16ValuesProduceIdenticalHashCodes()
24+
{
25+
// Arrange
26+
Base16 a = Base16.FromString("abcdefghijklmnopqrstuvwxyz");
27+
Base16 b = Base16.FromString("abcdefghijklmnopqrstuvwxyz");
28+
29+
// Act
30+
int hashCodeA = a.GetHashCode();
31+
int hashCodeB = b.GetHashCode();
32+
33+
// Assert
34+
Assert.Equal(hashCodeA, hashCodeB);
35+
}
36+
2237
[Theory(DisplayName = "Base16_FromString should produce the expected Base-16 value.")]
2338
[InlineData("31323334353637383930", "1234567890")]
2439
[InlineData("4142434445464748494a4b4c4d4e4f505152535455565758595a", "ABCDEFGHIJKLMNOPQRSTUVWXYZ")]

OnixLabs.Core.UnitTests/Base32Base32HexAlphabetTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,21 @@ namespace OnixLabs.Core.UnitTests
1919
{
2020
public sealed class Base32Base32HexAlphabetTests
2121
{
22+
[Fact(DisplayName = "Identical Base32 values produce identical hash codes.")]
23+
public void IdenticalBase32ValuesProduceIdenticalHashCodes()
24+
{
25+
// Arrange
26+
Base32 a = Base32.FromString("abcdefghijklmnopqrstuvwxyz", Base32Alphabet.Base32Hex);
27+
Base32 b = Base32.FromString("abcdefghijklmnopqrstuvwxyz", Base32Alphabet.Base32Hex);
28+
29+
// Act
30+
int hashCodeA = a.GetHashCode();
31+
int hashCodeB = b.GetHashCode();
32+
33+
// Assert
34+
Assert.Equal(hashCodeA, hashCodeB);
35+
}
36+
2237
[Theory(DisplayName = "Base32_FromString without padding should produce the expected Base-32 value.")]
2338
[InlineData("64P36D1L6ORJGE9G", "1234567890")]
2439
[InlineData("85146H258P3KGIAA9D64QJIFA18L4KQKALB5EM2PB8======", "ABCDEFGHIJKLMNOPQRSTUVWXYZ")]

OnixLabs.Core.UnitTests/Base32CrockfordAlphabetTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,21 @@ namespace OnixLabs.Core.UnitTests
1919
{
2020
public sealed class Base32CrockfordAlphabetTests
2121
{
22+
[Fact(DisplayName = "Identical Base32 values produce identical hash codes.")]
23+
public void IdenticalBase32ValuesProduceIdenticalHashCodes()
24+
{
25+
// Arrange
26+
Base32 a = Base32.FromString("abcdefghijklmnopqrstuvwxyz", Base32Alphabet.Crockford);
27+
Base32 b = Base32.FromString("abcdefghijklmnopqrstuvwxyz", Base32Alphabet.Crockford);
28+
29+
// Act
30+
int hashCodeA = a.GetHashCode();
31+
int hashCodeB = b.GetHashCode();
32+
33+
// Assert
34+
Assert.Equal(hashCodeA, hashCodeB);
35+
}
36+
2237
[Theory(DisplayName = "Base32_FromString without padding should produce the expected Base-32 value.")]
2338
[InlineData("64S36D1N6RVKGE9G", "1234567890")]
2439
[InlineData("85146H258S3MGJAA9D64TKJFA18N4MTMANB5EP2SB8======", "ABCDEFGHIJKLMNOPQRSTUVWXYZ")]

OnixLabs.Core.UnitTests/Base32DefaultAlphabetTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,21 @@ namespace OnixLabs.Core.UnitTests
1919
{
2020
public sealed class Base32DefaultAlphabetTests
2121
{
22+
[Fact(DisplayName = "Identical Base32 values produce identical hash codes.")]
23+
public void IdenticalBase32ValuesProduceIdenticalHashCodes()
24+
{
25+
// Arrange
26+
Base32 a = Base32.FromString("abcdefghijklmnopqrstuvwxyz", Base32Alphabet.Default);
27+
Base32 b = Base32.FromString("abcdefghijklmnopqrstuvwxyz", Base32Alphabet.Default);
28+
29+
// Act
30+
int hashCodeA = a.GetHashCode();
31+
int hashCodeB = b.GetHashCode();
32+
33+
// Assert
34+
Assert.Equal(hashCodeA, hashCodeB);
35+
}
36+
2237
[Theory(DisplayName = "Base32_FromString without padding should produce the expected Base-32 value.")]
2338
[InlineData("GEZDGNBVGY3TQOJQ", "1234567890")]
2439
[InlineData("IFBEGRCFIZDUQSKKJNGE2TSPKBIVEU2UKVLFOWCZLI======", "ABCDEFGHIJKLMNOPQRSTUVWXYZ")]

OnixLabs.Core.UnitTests/Base32ZBase32AlphabetTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,21 @@ namespace OnixLabs.Core.UnitTests
1919
{
2020
public sealed class Base32ZBase32AlphabetTests
2121
{
22+
[Fact(DisplayName = "Identical Base32 values produce identical hash codes.")]
23+
public void IdenticalBase32ValuesProduceIdenticalHashCodes()
24+
{
25+
// Arrange
26+
Base32 a = Base32.FromString("abcdefghijklmnopqrstuvwxyz", Base32Alphabet.ZBase32);
27+
Base32 b = Base32.FromString("abcdefghijklmnopqrstuvwxyz", Base32Alphabet.ZBase32);
28+
29+
// Act
30+
int hashCodeA = a.GetHashCode();
31+
int hashCodeB = b.GetHashCode();
32+
33+
// Assert
34+
Assert.Equal(hashCodeA, hashCodeB);
35+
}
36+
2237
[Theory(DisplayName = "Base32_FromString without padding should produce the expected Base-32 value.")]
2338
[InlineData("gr3dgpbiga5uoqjo", "1234567890")]
2439
[InlineData("efbrgtnfe3dwo1kkjpgr4u1xkbeirw4wkimfqsn3me======", "ABCDEFGHIJKLMNOPQRSTUVWXYZ")]

OnixLabs.Core.UnitTests/Base58DefaultAlphabetTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,21 @@ namespace OnixLabs.Core.UnitTests
1919
{
2020
public sealed class Base58DefaultAlphabetTests
2121
{
22+
[Fact(DisplayName = "Identical Base58 values produce identical hash codes.")]
23+
public void IdenticalBase58ValuesProduceIdenticalHashCodes()
24+
{
25+
// Arrange
26+
Base58 a = Base58.FromString("abcdefghijklmnopqrstuvwxyz", Base58Alphabet.Default);
27+
Base58 b = Base58.FromString("abcdefghijklmnopqrstuvwxyz", Base58Alphabet.Default);
28+
29+
// Act
30+
int hashCodeA = a.GetHashCode();
31+
int hashCodeB = b.GetHashCode();
32+
33+
// Assert
34+
Assert.Equal(hashCodeA, hashCodeB);
35+
}
36+
2237
[Theory(DisplayName = "Base58_FromString should produce the expected Base-58 value.")]
2338
[InlineData("3mJr7AoUCHxNqd", "1234567890")]
2439
[InlineData("2zuFXTJSTRK6ESktqhM2QDBkCnH1U46CnxaD", "ABCDEFGHIJKLMNOPQRSTUVWXYZ")]

OnixLabs.Core.UnitTests/Base58FlickrAlphabetTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,21 @@ namespace OnixLabs.Core.UnitTests
1919
{
2020
public sealed class Base58FlickrAlphabetTests
2121
{
22+
[Fact(DisplayName = "Identical Base58 values produce identical hash codes.")]
23+
public void IdenticalBase58ValuesProduceIdenticalHashCodes()
24+
{
25+
// Arrange
26+
Base58 a = Base58.FromString("abcdefghijklmnopqrstuvwxyz", Base58Alphabet.Flickr);
27+
Base58 b = Base58.FromString("abcdefghijklmnopqrstuvwxyz", Base58Alphabet.Flickr);
28+
29+
// Act
30+
int hashCodeA = a.GetHashCode();
31+
int hashCodeB = b.GetHashCode();
32+
33+
// Assert
34+
Assert.Equal(hashCodeA, hashCodeB);
35+
}
36+
2237
[Theory(DisplayName = "Base58_FromString should produce the expected Base-58 value.")]
2338
[InlineData("3LiR7aNtchXnQC", "1234567890")]
2439
[InlineData("2ZUfwsirsqj6erKTQGm2pdbKcMh1t46cMXzd", "ABCDEFGHIJKLMNOPQRSTUVWXYZ")]

OnixLabs.Core.UnitTests/Base58RippleAlphabetTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,21 @@ namespace OnixLabs.Core.UnitTests
1919
{
2020
public sealed class Base58RippleAlphabetTests
2121
{
22+
[Fact(DisplayName = "Identical Base58 values produce identical hash codes.")]
23+
public void IdenticalBase58ValuesProduceIdenticalHashCodes()
24+
{
25+
// Arrange
26+
Base58 a = Base58.FromString("abcdefghijklmnopqrstuvwxyz", Base58Alphabet.Ripple);
27+
Base58 b = Base58.FromString("abcdefghijklmnopqrstuvwxyz", Base58Alphabet.Ripple);
28+
29+
// Act
30+
int hashCodeA = a.GetHashCode();
31+
int hashCodeB = b.GetHashCode();
32+
33+
// Assert
34+
Assert.Equal(hashCodeA, hashCodeB);
35+
}
36+
2237
[Theory(DisplayName = "Base58_FromString should produce the expected Base-58 value.")]
2338
[InlineData("smJifwo7UHx4qd", "1234567890")]
2439
[InlineData("pzuEXTJSTRKaNSktq6MpQDBkU8Hr7haU8x2D", "ABCDEFGHIJKLMNOPQRSTUVWXYZ")]

OnixLabs.Core.UnitTests/Base64Tests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,21 @@ namespace OnixLabs.Core.UnitTests
1919
{
2020
public sealed class Base64Tests
2121
{
22+
[Fact(DisplayName = "Identical Base64 values produce identical hash codes.")]
23+
public void IdenticalBase64ValuesProduceIdenticalHashCodes()
24+
{
25+
// Arrange
26+
Base64 a = Base64.FromString("abcdefghijklmnopqrstuvwxyz");
27+
Base64 b = Base64.FromString("abcdefghijklmnopqrstuvwxyz");
28+
29+
// Act
30+
int hashCodeA = a.GetHashCode();
31+
int hashCodeB = b.GetHashCode();
32+
33+
// Assert
34+
Assert.Equal(hashCodeA, hashCodeB);
35+
}
36+
2237
[Theory(DisplayName = "Base64_FromString should produce the expected Base-64 value.")]
2338
[InlineData("MTIzNDU2Nzg5MA==", "1234567890")]
2439
[InlineData("QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVo=", "ABCDEFGHIJKLMNOPQRSTUVWXYZ")]

OnixLabs.Core/OnixLabs.Core.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
<Title>OnixLabs.Core</Title>
99
<Authors>ONIXLabs</Authors>
1010
<Description>ONIXLabs Core API for .NET</Description>
11-
<AssemblyVersion>2.0.0</AssemblyVersion>
11+
<AssemblyVersion>2.1.0</AssemblyVersion>
1212
<NeutralLanguage>en</NeutralLanguage>
1313
<Copyright>Copyright © ONIXLabs 2020-2021</Copyright>
1414
<RepositoryUrl>https://github.com/onix-labs/onixlabs-dotnet</RepositoryUrl>
15-
<PackageVersion>2.0.0</PackageVersion>
15+
<PackageVersion>2.1.0</PackageVersion>
1616
</PropertyGroup>
1717

1818
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2020-2021 ONIXLabs
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using Xunit;
16+
17+
namespace OnixLabs.Security.Cryptography.UnitTests
18+
{
19+
public sealed class HashAsyncTests
20+
{
21+
[Fact(DisplayName = "Identical hashes should be considered equal")]
22+
public async void IdenticalHashesShouldBeConsideredEqual()
23+
{
24+
// Arrange
25+
Hash a = await Hash.ComputeSha2Hash256Async("abcdefghijklmnopqrstuvwxyz");
26+
Hash b = await Hash.ComputeSha2Hash256Async("abcdefghijklmnopqrstuvwxyz");
27+
28+
// Assert
29+
Assert.Equal(a, b);
30+
}
31+
32+
[Fact(DisplayName = "Different hashes should not be considered equal")]
33+
public async void DifferentHashesShouldNotBeConsideredEqual()
34+
{
35+
// Arrange
36+
Hash a = await Hash.ComputeSha2Hash256Async("abcdefghijklmnopqrstuvwxyz");
37+
Hash b = await Hash.ComputeSha2Hash256Async("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
38+
39+
// Assert
40+
Assert.NotEqual(a, b);
41+
}
42+
}
43+
}

OnixLabs.Security.Cryptography.UnitTests/HashTests.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,27 @@ namespace OnixLabs.Security.Cryptography.UnitTests
1818
{
1919
public sealed class HashTests
2020
{
21+
[Fact(DisplayName = "Identical Hash values produce identical hash codes.")]
22+
public void IdenticalHashValuesProduceIdenticalHashCodes()
23+
{
24+
// Arrange
25+
Hash a = Hash.ComputeSha2Hash256("abcdefghijklmnopqrstuvwxyz");
26+
Hash b = Hash.ComputeSha2Hash256("abcdefghijklmnopqrstuvwxyz");
27+
28+
// Act
29+
int hashCodeA = a.GetHashCode();
30+
int hashCodeB = b.GetHashCode();
31+
32+
// Assert
33+
Assert.Equal(hashCodeA, hashCodeB);
34+
}
35+
2136
[Fact(DisplayName = "Identical hashes should be considered equal")]
2237
public void IdenticalHashesShouldBeConsideredEqual()
2338
{
2439
// Arrange
25-
Hash a = Hash.ComputeSha2Hash256("abc");
26-
Hash b = Hash.ComputeSha2Hash256("abc");
40+
Hash a = Hash.ComputeSha2Hash256("abcdefghijklmnopqrstuvwxyz");
41+
Hash b = Hash.ComputeSha2Hash256("abcdefghijklmnopqrstuvwxyz");
2742

2843
// Assert
2944
Assert.Equal(a, b);
@@ -33,8 +48,8 @@ public void IdenticalHashesShouldBeConsideredEqual()
3348
public void DifferentHashesShouldNotBeConsideredEqual()
3449
{
3550
// Arrange
36-
Hash a = Hash.ComputeSha2Hash256("abc");
37-
Hash b = Hash.ComputeSha2Hash256("xyz");
51+
Hash a = Hash.ComputeSha2Hash256("abcdefghijklmnopqrstuvwxyz");
52+
Hash b = Hash.ComputeSha2Hash256("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
3853

3954
// Assert
4055
Assert.NotEqual(a, b);
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2020-2021 ONIXLabs
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using Xunit;
16+
17+
namespace OnixLabs.Security.Cryptography.UnitTests
18+
{
19+
public sealed class HmacAsyncTests
20+
{
21+
[Fact(DisplayName = "Identical HMACs should be considered equal")]
22+
public async void IdenticalHashesShouldBeConsideredEqual()
23+
{
24+
// Arrange
25+
Hmac a = await Hmac.ComputeSha2Hmac256Async("abcdefghijklmnopqrstuvwxyz", "key");
26+
Hmac b = await Hmac.ComputeSha2Hmac256Async("abcdefghijklmnopqrstuvwxyz", "key");
27+
28+
// Assert
29+
Assert.Equal(a, b);
30+
}
31+
32+
[Fact(DisplayName = "Different HMACs should not be considered equal (different data)")]
33+
public async void DifferentHashesShouldNotBeConsideredEqualWithDifferentData()
34+
{
35+
// Arrange
36+
Hmac a = await Hmac.ComputeSha2Hmac256Async("abcdefghijklmnopqrstuvwxyz", "key");
37+
Hmac b = await Hmac.ComputeSha2Hmac256Async("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "key");
38+
39+
// Assert
40+
Assert.NotEqual(a, b);
41+
}
42+
43+
[Fact(DisplayName = "Different HMACs should not be considered equal (different keys)")]
44+
public async void DifferentHashesShouldNotBeConsideredEqualWithDifferentKeys()
45+
{
46+
// Arrange
47+
Hmac a = await Hmac.ComputeSha2Hmac256Async("abcdefghijklmnopqrstuvwxyz", "key");
48+
Hmac b = await Hmac.ComputeSha2Hmac256Async("abcdefghijklmnopqrstuvwxyz", "123");
49+
50+
// Assert
51+
Assert.NotEqual(a, b);
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)