private static void Encrypt_ShouldBeReversible_UsingSecureSymmetricKey(SymmetricAlgorithmSpecification algorithm, SecureSymmetricKeyDerivationMode derivationMode) { using (var randomnessProvider = RandomNumberGenerator.Create()) { // Arrange. var binarySerializer = new BinaryPassThroughSerializer(); var target = new SymmetricProcessor <Byte[]>(randomnessProvider, binarySerializer); var plaintextObject = new Byte[] { 0x01, 0x02, 0x03, 0x04, 0x05 }; using (var key = SecureSymmetricKey.New(algorithm, derivationMode)) { // Act. var ciphertextResult = target.Encrypt(plaintextObject, key); // Assert. ciphertextResult.Should().NotBeNullOrEmpty(); ciphertextResult.Count(value => value == 0x00).Should().NotBe(ciphertextResult.Length); // Act. var plaintextResult = target.Decrypt(ciphertextResult, key); // Assert. plaintextResult.Should().NotBeNullOrEmpty(); plaintextResult.Length.Should().Be(plaintextObject.Length); plaintextResult.Count(value => value == 0x00).Should().NotBe(plaintextResult.Length); for (var i = 0; i < plaintextResult.Length; i++) { // Assert. plaintextResult[i].Should().Be(plaintextObject[i]); } } } }
public void Serialize_ShouldReturnObjArgument_ForNonNullObjArgument() { // Arrange. var target = new BinaryPassThroughSerializer(); var serializationTarget = new Byte[] { 0x03 }; // Act. var result = target.Serialize(serializationTarget); // Assert. result.Should().NotBeNull(); result.Length.Should().Be(serializationTarget.Length); result[0].Should().Be(serializationTarget[0]); }
public void Deserialize_ShouldReturnBufferArgument_ForNonNullBufferArgument() { // Arrange. var target = new BinaryPassThroughSerializer(); var buffer = new Byte[] { 0x03 }; // Act. var result = target.Deserialize(buffer); // Assert. result.Should().NotBeNull(); result.Length.Should().Be(buffer.Length); result[0].Should().Be(buffer[0]); }
public void Serialize_ShouldRaiseArgumentNullException_ForNullObjArgument() { // Arrange. var target = new BinaryPassThroughSerializer(); var serializationTarget = (Byte[])null; // Act. var action = new Action(() => { var result = target.Serialize(serializationTarget); }); // Assert. action.Should().Throw <ArgumentNullException>(); }
public void Deserialize_ShouldRaiseArgumentNullException_ForNullBufferArgument() { // Arrange. var target = new BinaryPassThroughSerializer(); var buffer = (Byte[])null; // Act. var action = new Action(() => { var result = target.Deserialize(buffer); }); // Assert. action.Should().Throw <ArgumentNullException>(); }
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); } }
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()); } } }