Ejemplo n.º 1
0
        public string Encrypt(string message)
        {
            byte[]        result = null;
            StringBuilder hash   = new StringBuilder();

            switch (HashSize)
            {
            case HashFunctionSize.Bits224:
                result = Sha3.Sha3224().ComputeHash(Encoding.UTF8.GetBytes(message));
                break;

            case HashFunctionSize.Bits256:
                result = Sha3.Sha3256().ComputeHash(Encoding.UTF8.GetBytes(message));
                break;

            case HashFunctionSize.Bits384:
                result = Sha3.Sha3384().ComputeHash(Encoding.UTF8.GetBytes(message));
                break;

            case HashFunctionSize.Bits512:
                result = Sha3.Sha3512().ComputeHash(Encoding.UTF8.GetBytes(message));
                break;
            }

            for (int i = 0; i < result.Length; i++)
            {
                hash.Append(result[i].ToString("x2"));
            }

            return(hash.ToString());
        }
Ejemplo n.º 2
0
        public TicketScanResult CheckTicket(DigitalTicket scannedTicket, Ticket actualTicket)
        {
            if (scannedTicket is null)
            {
                throw new ArgumentNullException(nameof(scannedTicket), "Cannot check null ticket");
            }

            if (!scannedTicket.Seat.Equals(actualTicket.Seat))
            {
                throw new ArgumentException(nameof(actualTicket), "Seats do not match");
            }

            byte[] scannedSecretHash;
            try
            {
                using var hasher  = Sha3.Sha3224();
                scannedSecretHash = hasher.ComputeHash(scannedTicket.Secret);
            }
            catch (ArgumentException e)
            {
                _logger.LogWarning(e.Message);
                return(null);
            }

            if (!actualTicket.Secret.SequenceEqual(scannedSecretHash))
            {
                return(new TicketScanResult(false, string.Empty));
            }

            if (actualTicket.CustomerIdentifier is null)
            {
                return(new TicketScanResult(true, string.Empty));
            }

            string plainTextCustomerIdentifier;

            try
            {
                using var aes = _cipherFactory.CreateCbcProvider();
                plainTextCustomerIdentifier = aes.Decrypt(actualTicket.CustomerIdentifier, scannedTicket.NameKey, scannedTicket.NameIV);
            }
            catch (CryptographicException e)
            {
                _logger.LogDebug(e.Message);
                return(new TicketScanResult(true, string.Empty));
            }
            catch (ArgumentException e)
            {
                _logger.LogWarning(e.Message);
                return(null);
            }

            return(new TicketScanResult(true, plainTextCustomerIdentifier));
        }
Ejemplo n.º 3
0
        public void CheckTicket_ProvidedSecretMatchesCustomerIdentifierDecrypted_ReturnsResultOwnsTicketNameMatchesDecryptedValue()
        {
            // Arrange
            var plainTextSecret = "f09aIm3-hH9379c";

            byte[] hashedSecret;
            using (var hasher = Sha3.Sha3224())
            {
                hashedSecret = hasher.ComputeHash(plainTextSecret);
            }

            var customerIdentifier = new byte[16] {
                33, 93, 23, 252, 24, 38, 43, 94, 224, 10, 12, 232, 28, 211, 64, 99
            };
            var name = "Benjamin Swift";

            var scannedTicket = new DigitalTicket
            {
                Seat = new Seat {
                    Number = 1, Letter = 'A'
                },
                Secret = plainTextSecret
            };

            var actualTicket = new Ticket
            {
                Seat = new Seat {
                    Number = 1, Letter = 'A'
                },
                Secret             = hashedSecret,
                CustomerIdentifier = customerIdentifier
            };

            _cbc.Setup(callTo => callTo.Decrypt(customerIdentifier, It.IsAny <byte[]>(), It.IsAny <byte[]>())).Returns(name);

            // Act
            var result = _ticketChecker.CheckTicket(scannedTicket, actualTicket);

            // Assert
            Assert.Multiple(() =>
            {
                Assert.That(result.OwnsTicket, Is.True, nameof(TicketScanResult.OwnsTicket));
                Assert.That(result.Name, Is.EqualTo(name), nameof(TicketScanResult.Name));
            });
        }
Ejemplo n.º 4
0
        public void CheckTicket_ProvidedSecretMatchesDecryptCustomerIdentifierThrowsArgumentException_ReturnsNull()
        {
            // Arrange
            var plainTextSecret = "f09aIm3-hH9379c";

            byte[] hashedSecret;
            using (var hasher = Sha3.Sha3224())
            {
                hashedSecret = hasher.ComputeHash(plainTextSecret);
            }

            var customerIdentifier = new byte[16] {
                33, 93, 23, 252, 24, 38, 43, 94, 224, 10, 12, 232, 28, 211, 64, 99
            };

            var scannedTicket = new DigitalTicket
            {
                Seat = new Seat {
                    Number = 1, Letter = 'A'
                },
                Secret = plainTextSecret
            };

            var actualTicket = new Ticket
            {
                Seat = new Seat {
                    Number = 1, Letter = 'A'
                },
                Secret             = hashedSecret,
                CustomerIdentifier = customerIdentifier
            };

            _cbc.Setup(callTo => callTo.Decrypt(customerIdentifier, It.IsAny <byte[]>(), It.IsAny <byte[]>())).Throws <ArgumentException>();

            // Act
            var result = _ticketChecker.CheckTicket(scannedTicket, actualTicket);

            // Assert
            Assert.That(result, Is.Null);
        }
Ejemplo n.º 5
0
        public void CheckTicket_ProvidedSecretMatchesCustomerIdentifierNull_ReturnsResultOwnsTicketNameEmpty()
        {
            // Arrange
            var plainTextSecret = "f09aIm3-hH9379c";

            byte[] hashedSecret;
            using (var hasher = Sha3.Sha3224())
            {
                hashedSecret = hasher.ComputeHash(plainTextSecret);
            }

            var scannedTicket = new DigitalTicket
            {
                Seat = new Seat {
                    Number = 1, Letter = 'A'
                },
                Secret = plainTextSecret
            };

            var actualTicket = new Ticket
            {
                Seat = new Seat {
                    Number = 1, Letter = 'A'
                },
                Secret = hashedSecret
            };

            // Act
            var result = _ticketChecker.CheckTicket(scannedTicket, actualTicket);

            // Assert
            Assert.Multiple(() =>
            {
                Assert.That(result.OwnsTicket, Is.True, nameof(TicketScanResult.OwnsTicket));
                Assert.That(result.Name, Is.Empty, nameof(TicketScanResult.Name));
            });
        }
Ejemplo n.º 6
0
        public void Sha3224ComputeHash_WithControlledInput_ResultMatchesExpected(string input, string expected)
        {
            var hash = Sha3.Sha3224().ComputeHash(Encoding.UTF8.GetBytes(input));

            hash.ToHexString().Should().Be(expected);
        }