コード例 #1
0
        public void OverloadsReturnSamePIN()
        {
            var  secret         = "JBSWY3DPEHPK3PXP";
            var  secretAsBytes  = Encoding.UTF8.GetBytes(secret);
            var  secretAsBase32 = Base32Encoding.ToString(secretAsBytes);
            long counter        = 54615912;
            var  expected       = "508826";

            var subject = new TwoFactorAuthenticator();

            var pinFromString = subject.GeneratePINAtInterval(secret, counter);
            var pinFromBytes  = subject.GeneratePINAtInterval(secretAsBytes, counter);
            var pinFromBase32 = subject.GeneratePINAtInterval(secretAsBase32, counter, secretIsBase32: true);

            pinFromString.ShouldBe(expected);
            pinFromBytes.ShouldBe(expected);
            pinFromBase32.ShouldBe(expected);
        }
コード例 #2
0
        /// <summary>
        /// Generate max QR bằng code
        /// </summary>
        /// <param name="userName">tên player khi đăng ký xong</param>
        /// <returns></returns>
        public string GetQrCodeText(string userName)
        {
            TwoFactorAuthenticator _tfa = new TwoFactorAuthenticator();
            var key    = _tfa.GeneratePINAtInterval(userName, 360, 6);
            var result = _tfa.GenerateSetupCode(userName, userName, userName, 300, 300);
            var QRtext = result.ManualEntryKey;

            return(QRtext);
        }
コード例 #3
0
        /// <summary>
        /// Generate mã QR bằng hình ảnh
        /// </summary>
        /// <param name="userName">tên player khi đăng ký xong</param>
        /// <returns></returns>
        public string GetQrCodeImage(string userName)
        {
            TwoFactorAuthenticator _tfa = new TwoFactorAuthenticator();
            var key     = _tfa.GeneratePINAtInterval(userName, 360, 6);
            var result  = _tfa.GenerateSetupCode(userName, userName, userName, 300, 300);
            var urlImag = result.QrCodeSetupImageUrl;

            return(urlImag);
        }
コード例 #4
0
        public void BasicAuthCodeTest()
        {
            string secretKey = "PJWUMZKAUUFQKJBAMD6VGJ6RULFVW4ZH";
            string expected  = "551508";

            TwoFactorAuthenticator tfa = new TwoFactorAuthenticator();

            long currentTime = 1416643820;

            // I actually think you are supposed to divide the time by 30 seconds? Maybe need an overload that takes a DateTime?
            var actual = tfa.GeneratePINAtInterval(secretKey, currentTime, 6);

            actual.ShouldBe(expected);
        }
コード例 #5
0
        public void Base32AuthCodeTest()
        {
            var secretKey = Base32Encoding.ToString(Encoding.UTF8.GetBytes("PJWUMZKAUUFQKJBAMD6VGJ6RULFVW4ZH"));
            var expected  = "551508";

            var tfa = new TwoFactorAuthenticator();

            var currentTime = 1416643820;

            // I actually think you are supposed to divide the time by 30 seconds?
            // Maybe need an overload that takes a DateTime?
            var actual = tfa.GeneratePINAtInterval(secretKey, currentTime, 6, true);

            actual.ShouldBe(expected);
        }
コード例 #6
0
        public void FindIterationNumber()
        {
            string secretKey = "PJWUMZKAUUFQKJBAMD6VGJ6RULFVW4ZH";
            string targetCode = "267762";

            TwoFactorAuthenticator tfa = new TwoFactorAuthenticator();
            var mins = DateTime.UtcNow.Subtract(_epoch).TotalMinutes;

            long currentTime = 1416643820;

            for (long i = currentTime; i >= 0; i=i-60)
            {
                var result = tfa.GeneratePINAtInterval(secretKey, i, 6);
                if (result == targetCode)
                {
                    Assert.IsTrue(true);
                }
            }

            Assert.IsTrue(false);
        }
コード例 #7
0
        public void FindIterationNumber()
        {
            string secretKey  = "PJWUMZKAUUFQKJBAMD6VGJ6RULFVW4ZH";
            string targetCode = "267762";

            TwoFactorAuthenticator tfa = new TwoFactorAuthenticator();
            var mins = DateTime.UtcNow.Subtract(_epoch).TotalMinutes;

            long currentTime = 1416643820;

            for (long i = currentTime; i >= 0; i = i - 60)
            {
                var result = tfa.GeneratePINAtInterval(secretKey, i, 6);
                if (result == targetCode)
                {
                    Assert.IsTrue(true);
                }
            }

            Assert.IsTrue(false);
        }