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);

OnixLabs.Security.Cryptography/EcdsaPublicKey.Verify.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public override bool IsDataValid(DigitalSignature signature, byte[] unsignedData
2828
{
2929
using ECDsa publicKey = ECDsa.Create();
3030

31-
publicKey.ImportSubjectPublicKeyInfo(PublicKeyData, out int _);
31+
publicKey.ImportSubjectPublicKeyInfo(KeyData, out int _);
3232
byte[] signatureData = signature.ToByteArray();
3333
HashAlgorithmName name = AlgorithmType.GetHashAlgorithmName();
3434

@@ -45,7 +45,7 @@ public override bool IsHashValid(DigitalSignature signature, byte[] unsignedHash
4545
{
4646
using ECDsa publicKey = ECDsa.Create();
4747

48-
publicKey.ImportSubjectPublicKeyInfo(PublicKeyData, out int _);
48+
publicKey.ImportSubjectPublicKeyInfo(KeyData, out int _);
4949
byte[] signatureData = signature.ToByteArray();
5050

5151
return publicKey.VerifyHash(unsignedHash, signatureData);

OnixLabs.Security.Cryptography/Hash.Equatable.cs

Lines changed: 5 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
{
@@ -68,7 +69,10 @@ public override bool Equals(object? obj)
6869
/// <returns>A hash code for this instance.</returns>
6970
public override int GetHashCode()
7071
{
71-
return HashCode.Combine(Value, AlgorithmType);
72+
return new HashCode()
73+
.AddItem(AlgorithmType)
74+
.AddItems(Value)
75+
.ToHashCode();
7276
}
7377
}
7478
}

OnixLabs.Security.Cryptography/Hmac.Equatable.cs

Lines changed: 5 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
{
@@ -68,7 +69,10 @@ public override bool Equals(object? obj)
6869
/// <returns>A hash code for this instance.</returns>
6970
public override int GetHashCode()
7071
{
71-
return HashCode.Combine(Hash, Data);
72+
return new HashCode()
73+
.AddItem(Hash)
74+
.AddItems(Data)
75+
.ToHashCode();
7276
}
7377
}
7478
}

OnixLabs.Security.Cryptography/KeyPair.Equatable.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
using System;
16+
using OnixLabs.Core;
1617

1718
namespace OnixLabs.Security.Cryptography
1819
{
@@ -69,7 +70,10 @@ public override bool Equals(object? obj)
6970
/// <returns>A hash code for this instance.</returns>
7071
public override int GetHashCode()
7172
{
72-
return HashCode.Combine(PrivateKey, PublicKey);
73+
return new HashCode()
74+
.AddItem(PrivateKey)
75+
.AddItem(PublicKey)
76+
.ToHashCode();
7377
}
7478
}
7579
}

OnixLabs.Security.Cryptography/PrivateKey.Equatable.cs

Lines changed: 7 additions & 2 deletions
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
{
@@ -51,7 +52,7 @@ public virtual bool Equals(PrivateKey? other)
5152
return ReferenceEquals(this, other)
5253
|| other is not null
5354
&& other.GetType() == GetType()
54-
&& other.PrivateKeyData.SequenceEqual(PrivateKeyData)
55+
&& other.KeyData.SequenceEqual(KeyData)
5556
&& other.AlgorithmType == AlgorithmType;
5657
}
5758

@@ -71,7 +72,11 @@ public override bool Equals(object? obj)
7172
/// <returns>A hash code for this instance.</returns>
7273
public override int GetHashCode()
7374
{
74-
return HashCode.Combine(GetType(), PrivateKeyData, AlgorithmType);
75+
return new HashCode()
76+
.AddItem(GetType())
77+
.AddItem(AlgorithmType)
78+
.AddItems(KeyData)
79+
.ToHashCode();
7580
}
7681
}
7782
}

OnixLabs.Security.Cryptography/PrivateKey.To.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public abstract partial class PrivateKey
2626
/// <returns>Returns a <see cref="byte"/> array that represents the underlying private key data.</returns>
2727
public byte[] ToByteArray()
2828
{
29-
return PrivateKeyData.Copy();
29+
return KeyData.Copy();
3030
}
3131

3232
/// <summary>
@@ -35,7 +35,7 @@ public byte[] ToByteArray()
3535
/// <returns>Returns a <see cref="Base16"/> value that represents the underlying private key data.</returns>
3636
public Base16 ToBase16()
3737
{
38-
return Base16.FromByteArray(PrivateKeyData);
38+
return Base16.FromByteArray(KeyData);
3939
}
4040

4141
/// <summary>
@@ -54,7 +54,7 @@ public Base32 ToBase32()
5454
/// <returns>Returns a <see cref="Base32"/> value that represents the underlying private key data.</returns>
5555
public Base32 ToBase32(Base32Alphabet alphabet)
5656
{
57-
return Base32.FromByteArray(PrivateKeyData, alphabet);
57+
return Base32.FromByteArray(KeyData, alphabet);
5858
}
5959

6060
/// <summary>
@@ -73,7 +73,7 @@ public Base58 ToBase58()
7373
/// <returns>Returns a <see cref="Base58"/> value that represents the underlying private key data.</returns>
7474
public Base58 ToBase58(Base58Alphabet alphabet)
7575
{
76-
return Base58.FromByteArray(PrivateKeyData, alphabet);
76+
return Base58.FromByteArray(KeyData, alphabet);
7777
}
7878

7979
/// <summary>
@@ -82,7 +82,7 @@ public Base58 ToBase58(Base58Alphabet alphabet)
8282
/// <returns>Returns a <see cref="Base64"/> value that represents the underlying private key data.</returns>
8383
public Base64 ToBase64()
8484
{
85-
return Base64.FromByteArray(PrivateKeyData);
85+
return Base64.FromByteArray(KeyData);
8686
}
8787

8888
/// <summary>
@@ -91,7 +91,7 @@ public Base64 ToBase64()
9191
/// <returns>A <see cref="string"/> that represents the current object.</returns>
9292
public override string ToString()
9393
{
94-
return Convert.ToHexString(PrivateKeyData).ToLower();
94+
return Convert.ToHexString(KeyData).ToLower();
9595
}
9696
}
9797
}

OnixLabs.Security.Cryptography/PrivateKey.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ protected PrivateKey(byte[] data, HashAlgorithmType type)
2828
{
2929
type.GetHashAlgorithmName();
3030

31-
PrivateKeyData = data;
31+
KeyData = data;
3232
AlgorithmType = type;
3333
}
3434

3535
/// <summary>
3636
/// Gets the underlying private key data.
3737
/// </summary>
38-
protected byte[] PrivateKeyData { get; }
38+
protected byte[] KeyData { get; }
3939

4040
/// <summary>
4141
/// Gets the hash algorithm type for computing signature data.

OnixLabs.Security.Cryptography/PublicKey.Equatable.cs

Lines changed: 7 additions & 2 deletions
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
{
@@ -51,7 +52,7 @@ public virtual bool Equals(PublicKey? other)
5152
return ReferenceEquals(this, other)
5253
|| other is not null
5354
&& other.GetType() == GetType()
54-
&& other.PublicKeyData.SequenceEqual(PublicKeyData)
55+
&& other.KeyData.SequenceEqual(KeyData)
5556
&& other.AlgorithmType == AlgorithmType;
5657
}
5758

@@ -71,7 +72,11 @@ public override bool Equals(object? obj)
7172
/// <returns>A hash code for this instance.</returns>
7273
public override int GetHashCode()
7374
{
74-
return HashCode.Combine(GetType(), PublicKeyData, AlgorithmType);
75+
return new HashCode()
76+
.AddItem(GetType())
77+
.AddItem(AlgorithmType)
78+
.AddItems(KeyData)
79+
.ToHashCode();
7580
}
7681
}
7782
}

0 commit comments

Comments
 (0)