Skip to content

Commit

Permalink
Added Length property to Hash and Salt structs. (#92)
Browse files Browse the repository at this point in the history
Added Length property to Hash and Salt structs.
  • Loading branch information
MrMatthewLayton authored Jan 11, 2025
1 parent 10d86b4 commit f5b4658
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 7 deletions.
6 changes: 3 additions & 3 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<NeutralLanguage>en</NeutralLanguage>
<Copyright>Copyright © ONIXLabs 2020</Copyright>
<RepositoryUrl>https://github.com/onix-labs/onixlabs-dotnet</RepositoryUrl>
<Version>11.1.0</Version>
<PackageVersion>11.1.0</PackageVersion>
<AssemblyVersion>11.1.0</AssemblyVersion>
<Version>11.2.0</Version>
<PackageVersion>11.2.0</PackageVersion>
<AssemblyVersion>11.2.0</AssemblyVersion>
</PropertyGroup>
</Project>
28 changes: 28 additions & 0 deletions OnixLabs.Security.Cryptography.UnitTests/HashTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public void HashShouldBeConstructableFromBytes()

// Then
Assert.Equal(expected, actual);
Assert.Equal(16, candidate.Length);
}

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

// Then
Assert.Equal(expected, actual);
Assert.Equal(16, candidate.Length);
}

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

// Then
Assert.Equal(expected, actual);
Assert.Equal(4, candidate.Length);
}

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

// Then
Assert.Equal(expected, actual);
Assert.Equal(4, candidate.Length);
}

[Fact(DisplayName = "Identical default hash values should be considered equal")]
Expand All @@ -98,6 +102,9 @@ public void IdenticalDefaultHashValuesShouldBeConsideredEqual()
Assert.True(left.Equals(right));
Assert.True(left == right);
Assert.False(left != right);

Assert.Equal(0, left.Length);
Assert.Equal(0, right.Length);
}

[Fact(DisplayName = "Identical hash values should be considered equal")]
Expand All @@ -113,6 +120,9 @@ public void IdenticalHashValuesShouldBeConsideredEqual()
Assert.True(left.Equals(right));
Assert.True(left == right);
Assert.False(left != right);

Assert.Equal(4, left.Length);
Assert.Equal(4, right.Length);
}

[Fact(DisplayName = "Different hash values should not be considered equal")]
Expand All @@ -128,6 +138,9 @@ public void DifferentHashValuesShouldNotBeConsideredEqual()
Assert.False(left.Equals(right));
Assert.False(left == right);
Assert.True(left != right);

Assert.Equal(4, left.Length);
Assert.Equal(4, right.Length);
}

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

// Then
Assert.Equal(leftHashCode, rightHashCode);

Assert.Equal(4, left.Length);
Assert.Equal(4, right.Length);
}

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

// Then
Assert.NotEqual(leftHashCode, rightHashCode);

Assert.Equal(4, left.Length);
Assert.Equal(4, right.Length);
}

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

// Then
Assert.Equal(expected, actual);

Assert.Equal(1, left.Length);
Assert.Equal(1, right.Length);
}

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

// Then
Assert.Equal(expected, actual);

Assert.Equal(1, left.Length);
Assert.Equal(1, right.Length);
}

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

// Then
Assert.Equal(expected, actual);

Assert.Equal(1, left.Length);
Assert.Equal(1, right.Length);
}

[Theory(DisplayName = "Hash.Compute should produce the expected hash using a byte array")]
Expand Down
22 changes: 22 additions & 0 deletions OnixLabs.Security.Cryptography.UnitTests/SaltTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public void SaltShouldBeConstructableFromBytes()

// Then
Assert.Equal(expected, actual);
Assert.Equal(16, candidate.Length);
}

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

// Then
Assert.Equal(expected, actual);
Assert.Equal(4, candidate.Length);
}

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

// Then
Assert.Equal(expected, actual);
Assert.Equal(4, candidate.Length);
}

[Fact(DisplayName = "Identical default salt values should be considered equal")]
Expand All @@ -78,6 +81,9 @@ public void IdenticalDefaultSaltValuesShouldBeConsideredEqual()
Assert.True(left.Equals(right));
Assert.True(left == right);
Assert.False(left != right);

Assert.Equal(0, left.Length);
Assert.Equal(0, right.Length);
}

[Fact(DisplayName = "Identical salt values should be considered equal")]
Expand All @@ -93,6 +99,9 @@ public void IdenticalSaltValuesShouldBeConsideredEqual()
Assert.True(left.Equals(right));
Assert.True(left == right);
Assert.False(left != right);

Assert.Equal(4, left.Length);
Assert.Equal(4, right.Length);
}

[Fact(DisplayName = "Different salt values should not be considered equal")]
Expand All @@ -108,6 +117,9 @@ public void DifferentSaltValuesShouldNotBeConsideredEqual()
Assert.False(left.Equals(right));
Assert.False(left == right);
Assert.True(left != right);

Assert.Equal(4, left.Length);
Assert.Equal(4, right.Length);
}

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

// Then
Assert.Equal(leftHashCode, rightHashCode);

Assert.Equal(4, left.Length);
Assert.Equal(4, right.Length);
}

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

// Then
Assert.NotEqual(leftHashCode, rightHashCode);

Assert.Equal(4, left.Length);
Assert.Equal(4, right.Length);
}

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

// Then
Assert.Equal(expected, candidate.AsReadOnlySpan().Length);

Assert.Equal(32, candidate.Length);
}

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

Assert.Equal(32, candidate.Length);
}
}
7 changes: 6 additions & 1 deletion OnixLabs.Security.Cryptography/Hash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace OnixLabs.Security.Cryptography;
/// Represents a cryptographic hash.
/// </summary>
/// <param name="value">The underlying value of the cryptographic hash.</param>
public readonly partial struct Hash(ReadOnlySpan<byte> value) : ICryptoPrimitive<Hash>, IValueComparable<Hash>, ISpanParsable<Hash>, ISpanBinaryConvertible
public readonly partial struct Hash(ReadOnlySpan<byte> value) : ICryptoPrimitive<Hash>, IValueComparable<Hash>, ISpanParsable<Hash>
{
/// <summary>
/// Initializes a new instance of the <see cref="Hash"/> struct.
Expand All @@ -42,4 +42,9 @@ public Hash(byte value, int length) : this(Enumerable.Repeat(value, length).ToAr
}

private readonly byte[] value = value.ToArray();

/// <summary>
/// Gets the length of the current <see cref="Hash"/> in bytes.
/// </summary>
public int Length => value?.Length ?? 0;
}
3 changes: 1 addition & 2 deletions OnixLabs.Security.Cryptography/MerkleTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@
// limitations under the License.

using System.Security.Cryptography;
using OnixLabs.Core;

namespace OnixLabs.Security.Cryptography;

/// <summary>
/// Represents a Merkle tree.
/// </summary>
public abstract partial class MerkleTree : ICryptoPrimitive<MerkleTree>, ISpanBinaryConvertible
public abstract partial class MerkleTree : ICryptoPrimitive<MerkleTree>
{
/// <summary>
/// Initializes a new instance of the <see cref="MerkleTree"/> class.
Expand Down
7 changes: 6 additions & 1 deletion OnixLabs.Security.Cryptography/Salt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace OnixLabs.Security.Cryptography;
/// Represents a cryptographically secure random number, otherwise known as a salt value.
/// </summary>
/// <param name="value">The underlying value of the salt.</param>
public readonly partial struct Salt(ReadOnlySpan<byte> value) : ICryptoPrimitive<Salt>, ISpanBinaryConvertible
public readonly partial struct Salt(ReadOnlySpan<byte> value) : ICryptoPrimitive<Salt>
{
/// <summary>
/// Initializes a new instance of the <see cref="Salt"/> struct.
Expand All @@ -32,4 +32,9 @@ public readonly partial struct Salt(ReadOnlySpan<byte> value) : ICryptoPrimitive
public Salt(ReadOnlySequence<byte> value) : this(ReadOnlySpan<byte>.Empty) => value.CopyTo(out this.value);

private readonly byte[] value = value.ToArray();

/// <summary>
/// Gets the length of the current <see cref="Salt"/> in bytes.
/// </summary>
public int Length => value?.Length ?? 0;
}

0 comments on commit f5b4658

Please sign in to comment.