private static void CalculateHash_ValidateDeterminism(HashingAlgorithmSpecification algorithm, SaltingMode saltingMode, Boolean shouldBeDeterministic) { using (var randomnessProvider = RandomNumberGenerator.Create()) { // Arrange. var binarySerializer = new BinaryPassThroughSerializer(); var target = new HashingProcessor <Byte[]>(randomnessProvider, binarySerializer); var plaintextObject = new Byte[] { 0x01, 0x02, 0x03, 0x04, 0x05 }; // Act. var firstHashValue = target.CalculateHash(plaintextObject, algorithm, saltingMode); // Assert. firstHashValue.Should().NotBeNullOrEmpty(); firstHashValue.Count(value => value == 0x00).Should().NotBe(firstHashValue.Length); ValidateDigestLength(firstHashValue, algorithm, saltingMode, target.SaltLengthInBytes); // Act. var secondHashValue = target.CalculateHash(plaintextObject, algorithm, saltingMode); // Assert. secondHashValue.Should().NotBeNullOrEmpty(); secondHashValue.Length.Should().Be(firstHashValue.Length); secondHashValue.Count(value => value == 0x00).Should().NotBe(secondHashValue.Length); if (shouldBeDeterministic) { // Assert. firstHashValue.ComputeThirtyTwoBitHash().Should().Be(secondHashValue.ComputeThirtyTwoBitHash()); } else { // Assert. firstHashValue.ComputeThirtyTwoBitHash().Should().NotBe(secondHashValue.ComputeThirtyTwoBitHash()); } } }
private static void EvaluateHash_ShouldProduceDesiredResults(HashingAlgorithmSpecification algorithm, SaltingMode saltingMode) { using (var randomnessProvider = RandomNumberGenerator.Create()) { // Arrange. var binarySerializer = new BinaryPassThroughSerializer(); var target = new HashingProcessor <Byte[]>(randomnessProvider, binarySerializer); var plaintextObject = new Byte[] { 0x01, 0x02, 0x03, 0x04, 0x05 }; var matchingHashValue = target.CalculateHash(plaintextObject, algorithm, saltingMode); var nonMatchingHashValue = matchingHashValue.PerformCircularBitShift(Core.BitShiftDirection.Left, 16); // Act. var resultOne = target.EvaluateHash(matchingHashValue, plaintextObject, algorithm, saltingMode); var resultTwo = target.EvaluateHash(nonMatchingHashValue, plaintextObject, algorithm, saltingMode); // Assert. resultOne.Should().BeTrue(); resultTwo.Should().BeFalse(); ValidateDigestLength(matchingHashValue, algorithm, saltingMode, target.SaltLengthInBytes); } }