Skip to content

Commit 70e5522

Browse files
feature/v2.1
- Added hash code extensions. - Updated GetHashCode implementations to correctly hash arrays.
1 parent 3a04470 commit 70e5522

24 files changed

+135
-44
lines changed

OnixLabs.Core/Enumeration.Equatable.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ public override bool Equals(object? obj)
7070
/// <returns>A hash code for this instance.</returns>
7171
public override int GetHashCode()
7272
{
73-
return HashCode.Combine(GetType(), Name, Value);
73+
return new HashCode()
74+
.AddItem(GetType())
75+
.AddItem(Name)
76+
.AddItem(Value)
77+
.ToHashCode();
7478
}
7579
}
7680
}

OnixLabs.Core/HashCodeExtensions.cs

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 System;
16+
using System.Collections.Generic;
17+
using System.ComponentModel;
18+
using OnixLabs.Core.Linq;
19+
20+
namespace OnixLabs.Core
21+
{
22+
/// <summary>
23+
/// Provides extension methods for hash codes.
24+
/// </summary>
25+
[EditorBrowsable(EditorBrowsableState.Never)]
26+
public static class HashCodeExtensions
27+
{
28+
/// <summary>
29+
/// Adds an item to be hashed into a <see cref="HashCode"/> instance.
30+
/// </summary>
31+
/// <param name="hashCode">The <see cref="HashCode"/> which will receive the item to hash.</param>
32+
/// <param name="item">The item to hash into the <see cref="HashCode"/>.</param>
33+
/// <typeparam name="T">The underlying type of the item to hash.</typeparam>
34+
/// <returns>Returns the <see cref="HashCode"/> containing the added item.</returns>
35+
public static HashCode AddItem<T>(this HashCode hashCode, T item)
36+
{
37+
hashCode.Add(item);
38+
return hashCode;
39+
}
40+
41+
/// <summary>
42+
/// Adds the items to be hashed into a <see cref="HashCode"/> instance.
43+
/// </summary>
44+
/// <param name="hashCode">The <see cref="HashCode"/> which will receive the items to hash.</param>
45+
/// <param name="items">The items to hash into the <see cref="HashCode"/>.</param>
46+
/// <typeparam name="T">The underlying type of the items to hash.</typeparam>
47+
/// <returns>Returns the <see cref="HashCode"/> containing the added items.</returns>
48+
public static HashCode AddItems<T>(this HashCode hashCode, IEnumerable<T> items)
49+
{
50+
items.ForEach(hashCode.Add);
51+
return hashCode;
52+
}
53+
}
54+
}

OnixLabs.Core/Text/Base16.Equatable.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ public override bool Equals(object? obj)
6767
/// <returns>A hash code for this instance.</returns>
6868
public override int GetHashCode()
6969
{
70-
return HashCode.Combine(Value);
70+
return new HashCode()
71+
.AddItems(Value)
72+
.ToHashCode();
7173
}
7274
}
7375
}

OnixLabs.Core/Text/Base32.Equatable.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ public override bool Equals(object? obj)
6969
/// <returns>A hash code for this instance.</returns>
7070
public override int GetHashCode()
7171
{
72-
return HashCode.Combine(Value);
72+
return new HashCode()
73+
.AddItems(Value)
74+
.ToHashCode();
7375
}
7476
}
7577
}

OnixLabs.Core/Text/Base58.Equatable.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ public override bool Equals(object? obj)
6868
/// <returns>A hash code for this instance.</returns>
6969
public override int GetHashCode()
7070
{
71-
return HashCode.Combine(Value);
71+
return new HashCode()
72+
.AddItems(Value)
73+
.ToHashCode();
7274
}
7375
}
7476
}

OnixLabs.Core/Text/Base64.Equatable.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ public override bool Equals(object? obj)
6767
/// <returns>A hash code for this instance.</returns>
6868
public override int GetHashCode()
6969
{
70-
return HashCode.Combine(Value);
70+
return new HashCode()
71+
.AddItems(Value)
72+
.ToHashCode();
7173
}
7274
}
7375
}

OnixLabs.Security.Cryptography/DigitalSignature.Equatable.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
using System;
1616
using System.Linq;
17+
using OnixLabs.Core;
1718

1819
namespace OnixLabs.Security.Cryptography
1920
{
@@ -67,7 +68,9 @@ public override bool Equals(object? obj)
6768
/// <returns>A hash code for this instance.</returns>
6869
public override int GetHashCode()
6970
{
70-
return HashCode.Combine(Value);
71+
return new HashCode()
72+
.AddItems(Value)
73+
.ToHashCode();
7174
}
7275
}
7376
}

OnixLabs.Security.Cryptography/EcdsaPrivateKey.Export.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public override byte[] ExportPkcs8Key()
2727
{
2828
using ECDsa privateKey = ECDsa.Create();
2929

30-
privateKey.ImportECPrivateKey(PrivateKeyData, out int _);
30+
privateKey.ImportECPrivateKey(KeyData, out int _);
3131

3232
return privateKey.ExportPkcs8PrivateKey();
3333
}
@@ -42,7 +42,7 @@ public override byte[] ExportPkcs8Key(ReadOnlySpan<char> password, PbeParameters
4242
{
4343
using ECDsa privateKey = ECDsa.Create();
4444

45-
privateKey.ImportECPrivateKey(PrivateKeyData, out int _);
45+
privateKey.ImportECPrivateKey(KeyData, out int _);
4646

4747
return privateKey.ExportEncryptedPkcs8PrivateKey(password, parameters);
4848
}

OnixLabs.Security.Cryptography/EcdsaPrivateKey.Get.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public override PublicKey GetPublicKey()
2626
{
2727
using ECDsa privateKey = ECDsa.Create();
2828

29-
privateKey.ImportECPrivateKey(PrivateKeyData, out int _);
29+
privateKey.ImportECPrivateKey(KeyData, out int _);
3030
byte[] publicKey = privateKey.ExportSubjectPublicKeyInfo();
3131

3232
return new EcdsaPublicKey(publicKey, AlgorithmType);

OnixLabs.Security.Cryptography/EcdsaPrivateKey.Sign.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public override DigitalSignature SignData(byte[] unsignedData)
2727
{
2828
using ECDsa privateKey = ECDsa.Create();
2929

30-
privateKey.ImportECPrivateKey(PrivateKeyData, out int _);
30+
privateKey.ImportECPrivateKey(KeyData, out int _);
3131
HashAlgorithmName name = AlgorithmType.GetHashAlgorithmName();
3232
byte[] signedData = privateKey.SignData(unsignedData, name);
3333

@@ -43,7 +43,7 @@ public override DigitalSignature SignHash(byte[] unsignedHash)
4343
{
4444
using ECDsa privateKey = ECDsa.Create();
4545

46-
privateKey.ImportECPrivateKey(PrivateKeyData, out int _);
46+
privateKey.ImportECPrivateKey(KeyData, out int _);
4747
byte[] signedData = privateKey.SignHash(unsignedHash);
4848

4949
return DigitalSignature.FromByteArray(signedData);

0 commit comments

Comments
 (0)