diff --git a/Directory.Build.props b/Directory.Build.props index 4e9bc56..23700e2 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -8,8 +8,8 @@ en Copyright © ONIXLabs 2020 https://github.com/onix-labs/onixlabs-dotnet - 11.1.0 - 11.1.0 - 11.1.0 + 11.2.0 + 11.2.0 + 11.2.0 diff --git a/OnixLabs.Security.Cryptography.UnitTests/HashTests.cs b/OnixLabs.Security.Cryptography.UnitTests/HashTests.cs index 4024f98..34b2408 100644 --- a/OnixLabs.Security.Cryptography.UnitTests/HashTests.cs +++ b/OnixLabs.Security.Cryptography.UnitTests/HashTests.cs @@ -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")] @@ -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")] @@ -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")] @@ -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")] @@ -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")] @@ -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")] @@ -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")] @@ -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")] @@ -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")] @@ -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")] @@ -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")] @@ -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")] diff --git a/OnixLabs.Security.Cryptography.UnitTests/SaltTests.cs b/OnixLabs.Security.Cryptography.UnitTests/SaltTests.cs index 49de23c..6274431 100644 --- a/OnixLabs.Security.Cryptography.UnitTests/SaltTests.cs +++ b/OnixLabs.Security.Cryptography.UnitTests/SaltTests.cs @@ -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")] @@ -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")] @@ -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")] @@ -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")] @@ -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")] @@ -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")] @@ -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")] @@ -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")] @@ -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")] @@ -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); } } diff --git a/OnixLabs.Security.Cryptography/Hash.cs b/OnixLabs.Security.Cryptography/Hash.cs index dbb38b1..171e342 100644 --- a/OnixLabs.Security.Cryptography/Hash.cs +++ b/OnixLabs.Security.Cryptography/Hash.cs @@ -23,7 +23,7 @@ namespace OnixLabs.Security.Cryptography; /// Represents a cryptographic hash. /// /// The underlying value of the cryptographic hash. -public readonly partial struct Hash(ReadOnlySpan value) : ICryptoPrimitive, IValueComparable, ISpanParsable, ISpanBinaryConvertible +public readonly partial struct Hash(ReadOnlySpan value) : ICryptoPrimitive, IValueComparable, ISpanParsable { /// /// Initializes a new instance of the struct. @@ -42,4 +42,9 @@ public Hash(byte value, int length) : this(Enumerable.Repeat(value, length).ToAr } private readonly byte[] value = value.ToArray(); + + /// + /// Gets the length of the current in bytes. + /// + public int Length => value?.Length ?? 0; } diff --git a/OnixLabs.Security.Cryptography/MerkleTree.cs b/OnixLabs.Security.Cryptography/MerkleTree.cs index 56b561b..7da96eb 100644 --- a/OnixLabs.Security.Cryptography/MerkleTree.cs +++ b/OnixLabs.Security.Cryptography/MerkleTree.cs @@ -13,14 +13,13 @@ // limitations under the License. using System.Security.Cryptography; -using OnixLabs.Core; namespace OnixLabs.Security.Cryptography; /// /// Represents a Merkle tree. /// -public abstract partial class MerkleTree : ICryptoPrimitive, ISpanBinaryConvertible +public abstract partial class MerkleTree : ICryptoPrimitive { /// /// Initializes a new instance of the class. diff --git a/OnixLabs.Security.Cryptography/Salt.cs b/OnixLabs.Security.Cryptography/Salt.cs index 1ca47db..e86db09 100644 --- a/OnixLabs.Security.Cryptography/Salt.cs +++ b/OnixLabs.Security.Cryptography/Salt.cs @@ -22,7 +22,7 @@ namespace OnixLabs.Security.Cryptography; /// Represents a cryptographically secure random number, otherwise known as a salt value. /// /// The underlying value of the salt. -public readonly partial struct Salt(ReadOnlySpan value) : ICryptoPrimitive, ISpanBinaryConvertible +public readonly partial struct Salt(ReadOnlySpan value) : ICryptoPrimitive { /// /// Initializes a new instance of the struct. @@ -32,4 +32,9 @@ public readonly partial struct Salt(ReadOnlySpan value) : ICryptoPrimitive public Salt(ReadOnlySequence value) : this(ReadOnlySpan.Empty) => value.CopyTo(out this.value); private readonly byte[] value = value.ToArray(); + + /// + /// Gets the length of the current in bytes. + /// + public int Length => value?.Length ?? 0; }