private static void CalculateHash_ValidateDeterminism(HashingAlgorithmSpecification algorithm, SaltingMode saltingMode, Boolean shouldBeDeterministic) { using (var randomnessProvider = RandomNumberGenerator.Create()) { // Arrange. var target = new HashingStringProcessor(randomnessProvider); var plaintextObject = "䆟`ಮ䷆ʘ‣⦸⏹ⰄͶa✰ṁ亡Zᨖ0༂⽔9㗰"; // Act. var firstHashValue = target.CalculateHash(plaintextObject, algorithm, saltingMode); // Assert. firstHashValue.Should().NotBeNullOrEmpty(); firstHashValue.Count(value => value == 0x00).Should().NotBe(firstHashValue.Length); // 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 Constructor_ShouldProduceDesiredResults(Int32 blockCount, HashingAlgorithmSpecification algorithm) { using (var randomnessProvider = RandomNumberGenerator.Create()) { // Arrange. var hashingProcessor = new HashingStringProcessor(randomnessProvider); var blocks = new String[blockCount]; randomnessProvider.FillStringArray(blocks, 1, 8, false, true, true, true, false, false, false); // Act. var target = new HashTree <String>(hashingProcessor, algorithm, blocks); var duplicate = new HashTree <String>(hashingProcessor, algorithm, blocks); // Assert. target.LeafCount.Should().Be(blockCount); target.RootNode.Should().NotBeNull(); target.RootNode.Value.Should().NotBeNull(); target.RootNode.Value.ComputeThirtyTwoBitHash().Should().Be(duplicate.RootNode.Value.ComputeThirtyTwoBitHash()); target.RootNode.ToString().Should().Be(Convert.ToBase64String(target.RootNode.Value)); ValidateDigestLength(blockCount, target.RootNode.Value, algorithm); ValidateTreeHeight(blockCount, target.RootNode.Height); } }