Skip to content

Commit f5b4658

Browse files
authoredJan 11, 2025··
Added Length property to Hash and Salt structs. (#92)
Added Length property to Hash and Salt structs.
1 parent 10d86b4 commit f5b4658

File tree

6 files changed

+66
-7
lines changed

6 files changed

+66
-7
lines changed
 

‎Directory.Build.props

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
<NeutralLanguage>en</NeutralLanguage>
99
<Copyright>Copyright © ONIXLabs 2020</Copyright>
1010
<RepositoryUrl>https://github.com/onix-labs/onixlabs-dotnet</RepositoryUrl>
11-
<Version>11.1.0</Version>
12-
<PackageVersion>11.1.0</PackageVersion>
13-
<AssemblyVersion>11.1.0</AssemblyVersion>
11+
<Version>11.2.0</Version>
12+
<PackageVersion>11.2.0</PackageVersion>
13+
<AssemblyVersion>11.2.0</AssemblyVersion>
1414
</PropertyGroup>
1515
</Project>

‎OnixLabs.Security.Cryptography.UnitTests/HashTests.cs

+28
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public void HashShouldBeConstructableFromBytes()
3636

3737
// Then
3838
Assert.Equal(expected, actual);
39+
Assert.Equal(16, candidate.Length);
3940
}
4041

4142
[Fact(DisplayName = "Hash should be constructable from byte and length")]
@@ -52,6 +53,7 @@ public void HashShouldBeConstructableFromByteAndLength()
5253

5354
// Then
5455
Assert.Equal(expected, actual);
56+
Assert.Equal(16, candidate.Length);
5557
}
5658

5759
[Fact(DisplayName = "Hash value should not be modified when altering the original byte array")]
@@ -68,6 +70,7 @@ public void HashValueShouldNotBeModifiedWhenAlteringTheOriginalByteArray()
6870

6971
// Then
7072
Assert.Equal(expected, actual);
73+
Assert.Equal(4, candidate.Length);
7174
}
7275

7376
[Fact(DisplayName = "Hash value should not be modified when altering the obtained byte array")]
@@ -83,6 +86,7 @@ public void HashValueShouldNotBeModifiedWhenAlteringTheObtainedByteArray()
8386

8487
// Then
8588
Assert.Equal(expected, actual);
89+
Assert.Equal(4, candidate.Length);
8690
}
8791

8892
[Fact(DisplayName = "Identical default hash values should be considered equal")]
@@ -98,6 +102,9 @@ public void IdenticalDefaultHashValuesShouldBeConsideredEqual()
98102
Assert.True(left.Equals(right));
99103
Assert.True(left == right);
100104
Assert.False(left != right);
105+
106+
Assert.Equal(0, left.Length);
107+
Assert.Equal(0, right.Length);
101108
}
102109

103110
[Fact(DisplayName = "Identical hash values should be considered equal")]
@@ -113,6 +120,9 @@ public void IdenticalHashValuesShouldBeConsideredEqual()
113120
Assert.True(left.Equals(right));
114121
Assert.True(left == right);
115122
Assert.False(left != right);
123+
124+
Assert.Equal(4, left.Length);
125+
Assert.Equal(4, right.Length);
116126
}
117127

118128
[Fact(DisplayName = "Different hash values should not be considered equal")]
@@ -128,6 +138,9 @@ public void DifferentHashValuesShouldNotBeConsideredEqual()
128138
Assert.False(left.Equals(right));
129139
Assert.False(left == right);
130140
Assert.True(left != right);
141+
142+
Assert.Equal(4, left.Length);
143+
Assert.Equal(4, right.Length);
131144
}
132145

133146
[Fact(DisplayName = "Identical hash values should produce identical hash codes")]
@@ -143,6 +156,9 @@ public void IdenticalHashValuesShouldProduceIdenticalHashCodes()
143156

144157
// Then
145158
Assert.Equal(leftHashCode, rightHashCode);
159+
160+
Assert.Equal(4, left.Length);
161+
Assert.Equal(4, right.Length);
146162
}
147163

148164
[Fact(DisplayName = "Different hash values should produce different hash codes")]
@@ -158,6 +174,9 @@ public void DifferentHashValuesShouldProduceDifferentHashCodes()
158174

159175
// Then
160176
Assert.NotEqual(leftHashCode, rightHashCode);
177+
178+
Assert.Equal(4, left.Length);
179+
Assert.Equal(4, right.Length);
161180
}
162181

163182
[Fact(DisplayName = "Hashes should produce a negative-one sort order when the left-hand hash is lesser than the right-hand hash")]
@@ -173,6 +192,9 @@ public void HashesShouldProduceANegativeOneSortOrderWhenTheLeftHandHashIsLesserT
173192

174193
// Then
175194
Assert.Equal(expected, actual);
195+
196+
Assert.Equal(1, left.Length);
197+
Assert.Equal(1, right.Length);
176198
}
177199

178200
[Fact(DisplayName = "Hashes should produce a positive-one sort order when the left-hand hash is greater than the right-hand hash")]
@@ -188,6 +210,9 @@ public void HashesShouldProduceAPositiveOneSortOrderWhenTheLeftHandHashIsGreater
188210

189211
// Then
190212
Assert.Equal(expected, actual);
213+
214+
Assert.Equal(1, left.Length);
215+
Assert.Equal(1, right.Length);
191216
}
192217

193218
[Fact(DisplayName = "Hashes should produce a zero sort order when the left-hand hash is equal to the right-hand hash")]
@@ -203,6 +228,9 @@ public void HashesShouldProduceAZeroSortOrderWhenTheLeftHandHashIsEqualToTheRigh
203228

204229
// Then
205230
Assert.Equal(expected, actual);
231+
232+
Assert.Equal(1, left.Length);
233+
Assert.Equal(1, right.Length);
206234
}
207235

208236
[Theory(DisplayName = "Hash.Compute should produce the expected hash using a byte array")]

‎OnixLabs.Security.Cryptography.UnitTests/SaltTests.cs

+22
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public void SaltShouldBeConstructableFromBytes()
3232

3333
// Then
3434
Assert.Equal(expected, actual);
35+
Assert.Equal(16, candidate.Length);
3536
}
3637

3738
[Fact(DisplayName = "Salt value should not be modified when altering the original byte array")]
@@ -48,6 +49,7 @@ public void SaltValueShouldNotBeModifiedWhenAlteringTheOriginalByteArray()
4849

4950
// Then
5051
Assert.Equal(expected, actual);
52+
Assert.Equal(4, candidate.Length);
5153
}
5254

5355
[Fact(DisplayName = "Salt value should not be modified when altering the obtained byte array")]
@@ -63,6 +65,7 @@ public void SaltValueShouldNotBeModifiedWhenAlteringTheObtainedByteArray()
6365

6466
// Then
6567
Assert.Equal(expected, actual);
68+
Assert.Equal(4, candidate.Length);
6669
}
6770

6871
[Fact(DisplayName = "Identical default salt values should be considered equal")]
@@ -78,6 +81,9 @@ public void IdenticalDefaultSaltValuesShouldBeConsideredEqual()
7881
Assert.True(left.Equals(right));
7982
Assert.True(left == right);
8083
Assert.False(left != right);
84+
85+
Assert.Equal(0, left.Length);
86+
Assert.Equal(0, right.Length);
8187
}
8288

8389
[Fact(DisplayName = "Identical salt values should be considered equal")]
@@ -93,6 +99,9 @@ public void IdenticalSaltValuesShouldBeConsideredEqual()
9399
Assert.True(left.Equals(right));
94100
Assert.True(left == right);
95101
Assert.False(left != right);
102+
103+
Assert.Equal(4, left.Length);
104+
Assert.Equal(4, right.Length);
96105
}
97106

98107
[Fact(DisplayName = "Different salt values should not be considered equal")]
@@ -108,6 +117,9 @@ public void DifferentSaltValuesShouldNotBeConsideredEqual()
108117
Assert.False(left.Equals(right));
109118
Assert.False(left == right);
110119
Assert.True(left != right);
120+
121+
Assert.Equal(4, left.Length);
122+
Assert.Equal(4, right.Length);
111123
}
112124

113125
[Fact(DisplayName = "Identical salt values should produce identical hash codes")]
@@ -123,6 +135,9 @@ public void IdenticalSaltValuesShouldProduceIdenticalSaltCodes()
123135

124136
// Then
125137
Assert.Equal(leftHashCode, rightHashCode);
138+
139+
Assert.Equal(4, left.Length);
140+
Assert.Equal(4, right.Length);
126141
}
127142

128143
[Fact(DisplayName = "Different salt values should produce different hash codes")]
@@ -138,6 +153,9 @@ public void DifferentSaltValuesShouldProduceDifferentSaltCodes()
138153

139154
// Then
140155
Assert.NotEqual(leftHashCode, rightHashCode);
156+
157+
Assert.Equal(4, left.Length);
158+
Assert.Equal(4, right.Length);
141159
}
142160

143161
[Fact(DisplayName = "Salt.Create should produce a salt of the specified length")]
@@ -149,6 +167,8 @@ public void SaltCreateShouldProduceASaltOfTheSpecifiedLength()
149167

150168
// Then
151169
Assert.Equal(expected, candidate.AsReadOnlySpan().Length);
170+
171+
Assert.Equal(32, candidate.Length);
152172
}
153173

154174
[Fact(DisplayName = "Salt.CreateNonZero should produce a salt of the specified length of non-zero bytes")]
@@ -161,5 +181,7 @@ public void SaltCreateNonZeroShouldProduceASaltOfTheSpecifiedLengthOfNonZeroByte
161181
// Then
162182
Assert.Equal(expected, candidate.AsReadOnlySpan().Length);
163183
Assert.True(candidate.AsReadOnlySpan().ToArray().None(value => value is 0));
184+
185+
Assert.Equal(32, candidate.Length);
164186
}
165187
}

‎OnixLabs.Security.Cryptography/Hash.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace OnixLabs.Security.Cryptography;
2323
/// Represents a cryptographic hash.
2424
/// </summary>
2525
/// <param name="value">The underlying value of the cryptographic hash.</param>
26-
public readonly partial struct Hash(ReadOnlySpan<byte> value) : ICryptoPrimitive<Hash>, IValueComparable<Hash>, ISpanParsable<Hash>, ISpanBinaryConvertible
26+
public readonly partial struct Hash(ReadOnlySpan<byte> value) : ICryptoPrimitive<Hash>, IValueComparable<Hash>, ISpanParsable<Hash>
2727
{
2828
/// <summary>
2929
/// Initializes a new instance of the <see cref="Hash"/> struct.
@@ -42,4 +42,9 @@ public Hash(byte value, int length) : this(Enumerable.Repeat(value, length).ToAr
4242
}
4343

4444
private readonly byte[] value = value.ToArray();
45+
46+
/// <summary>
47+
/// Gets the length of the current <see cref="Hash"/> in bytes.
48+
/// </summary>
49+
public int Length => value?.Length ?? 0;
4550
}

‎OnixLabs.Security.Cryptography/MerkleTree.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@
1313
// limitations under the License.
1414

1515
using System.Security.Cryptography;
16-
using OnixLabs.Core;
1716

1817
namespace OnixLabs.Security.Cryptography;
1918

2019
/// <summary>
2120
/// Represents a Merkle tree.
2221
/// </summary>
23-
public abstract partial class MerkleTree : ICryptoPrimitive<MerkleTree>, ISpanBinaryConvertible
22+
public abstract partial class MerkleTree : ICryptoPrimitive<MerkleTree>
2423
{
2524
/// <summary>
2625
/// Initializes a new instance of the <see cref="MerkleTree"/> class.

‎OnixLabs.Security.Cryptography/Salt.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace OnixLabs.Security.Cryptography;
2222
/// Represents a cryptographically secure random number, otherwise known as a salt value.
2323
/// </summary>
2424
/// <param name="value">The underlying value of the salt.</param>
25-
public readonly partial struct Salt(ReadOnlySpan<byte> value) : ICryptoPrimitive<Salt>, ISpanBinaryConvertible
25+
public readonly partial struct Salt(ReadOnlySpan<byte> value) : ICryptoPrimitive<Salt>
2626
{
2727
/// <summary>
2828
/// Initializes a new instance of the <see cref="Salt"/> struct.
@@ -32,4 +32,9 @@ public readonly partial struct Salt(ReadOnlySpan<byte> value) : ICryptoPrimitive
3232
public Salt(ReadOnlySequence<byte> value) : this(ReadOnlySpan<byte>.Empty) => value.CopyTo(out this.value);
3333

3434
private readonly byte[] value = value.ToArray();
35+
36+
/// <summary>
37+
/// Gets the length of the current <see cref="Salt"/> in bytes.
38+
/// </summary>
39+
public int Length => value?.Length ?? 0;
3540
}

0 commit comments

Comments
 (0)
Please sign in to comment.