public void Create_CalculatesSameMacForSameUserWithSameSessionComponent()
        {
            _helper = new AuthenticatedSessionIDHelper(_rng, new byte[32], new HmacSha256Helper());

            var session1 = _helper.Create("klings").AddBase64Padding();
            var session2 = _helper.Create("klings").AddBase64Padding();

            Assert.AreEqual(session1, session2);
        }
        public void Setup()
        {
            _rng = new PredictableNumberGenerator(0x05);
            _hmac = new Mock<IHmacHelper>().Object;
            Mock.Get(_hmac).Setup(h => h.CalculateMac(It.IsAny<byte[]>(), It.IsAny<byte[]>())).Returns(GetMockMac);

            var config = new SessionSecurityConfigurationSection();
            config.SessionIDAuthentication.Enabled = true;
            config.SessionIDAuthentication.AuthenticationKey = "0101010101010101010101010101010101010101010101010101010101010101";

            _helper = new AuthenticatedSessionIDHelper(_rng, new byte[32], _hmac);
        }
        public void Create_CalculatesDifferentMacForDifferentUsersWithSameSessionComponent()
        {
            _helper = new AuthenticatedSessionIDHelper(_rng, new byte[32], new HmacSha256Helper());

            var session1 = Convert.FromBase64String(_helper.Create("klings").AddBase64Padding());
            var session2 = Convert.FromBase64String(_helper.Create("klings2").AddBase64Padding());

            for (var i = 0; i < SessionIdComponentLength; i++)
            {
                Assert.AreEqual(session1[i], session2[i]);
            }

            var differs = false;
            for (var i = SessionIdComponentLength; i < session1.Length; i++)
            {
                differs = differs || session1[i] != session2[i];
            }
            Assert.IsTrue(differs, "MACs were equal.");
        }
        //[Test]
        public void ValidateMac_Timing()
        {
            _helper = new AuthenticatedSessionIDHelper(new RNGCryptoServiceProvider(), new byte[32], new HmacSha256Helper());
            var expectedMac = GetMockMac();
            Array.Resize(ref expectedMac, 16);
            var sessionID = new byte[]
            {
                0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
                0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
                0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
                0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10
            };
            var invalidSessionID = new byte[]
            {
                0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
                0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
                0x11, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
                0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10
            };
            var timer = new Stopwatch();
            
            //Warmup
            for (var i = 0; i < 100000; i++)
            {
                _helper.ValidateMac(expectedMac, invalidSessionID);
            }

            timer.Start();
            
            for (var i = 0; i < 10000000; i++)
            {
                _helper.ValidateMac(expectedMac, sessionID);
            }
            
            timer.Stop();
            var validElapsed = timer.ElapsedTicks;
            timer.Reset();
            timer.Start();
            
            for (var i = 0; i < 10000000; i++)
            {
                _helper.ValidateMac(expectedMac, invalidSessionID);
            }
            
            timer.Stop();
            var invalidElapsed = timer.ElapsedTicks;
            //Unlikely to be exactly the same, so will output the two values.
            Assert.AreEqual(validElapsed,invalidElapsed);
        }
 //[Test]
 public void Create_Performance()
 {
     _helper = new AuthenticatedSessionIDHelper(new RNGCryptoServiceProvider(), new byte[32], new HmacSha256Helper());
     foreach (var number in Enumerable.Range(0, 10000000))
     {
         _helper.Create("klings");
     }
 }