コード例 #1
0
        public void TimeCorrection_TotpWithCorrectionRemainingSeconds()
        {
            var correction = new TimeCorrection(DateTime.UtcNow.AddSeconds(5));

            var totp          = new Totp(OtpCalculationTests.RfcTestKey);
            var correctedTotp = new Totp(OtpCalculationTests.RfcTestKey, timeCorrection: correction);

            var standardRemaining  = totp.RemainingSeconds();
            var correctedRemaining = correctedTotp.RemainingSeconds();

            Assert.AreNotEqual(standardRemaining, correctedRemaining, "Correction wasn't applied");

            int difference = 0;

            if (standardRemaining > correctedRemaining)
            {
                difference = standardRemaining - correctedRemaining;
            }
            else
            {
                difference = (standardRemaining + 30) - correctedRemaining;
            }

            Assert.AreEqual(5, difference);
        }
コード例 #2
0
        public void TimeCorrection_Basic()
        {
            var hypotheticallyCorrectUtcTime = DateTime.UtcNow.AddMilliseconds(12500);
            var correction = new TimeCorrection(hypotheticallyCorrectUtcTime);

            var correctedTime = correction.CorrectedUtcNow;
            var difference    = hypotheticallyCorrectUtcTime - correctedTime;

            Assert.IsTrue(Math.Abs(difference.TotalMilliseconds) <= 64, "The corrected value is wrong");
        }
コード例 #3
0
        public void TimeCorrection_BasicWithSpecificReference()
        {
            var baseTime = DateTime.UtcNow;
            var incorrectReferenceTime = baseTime.AddMilliseconds(-12500);

            var correction = new TimeCorrection(baseTime, incorrectReferenceTime);

            Assert.AreEqual(baseTime, correction.GetCorrectedTime(incorrectReferenceTime));
            Assert.AreEqual(TimeSpan.FromMilliseconds(-12500), correction.CorrectionFactor);
        }
コード例 #4
0
        public void TimeCorrection_TotpComputeTotpSpecificDateWithCorrection()
        {
            var correction    = new TimeCorrection(DateTime.UtcNow.AddSeconds(100)); // 100 ensures that at a minimum we are 3 steps away
            var correctedTotp = new Totp(OtpCalculationTests.RfcTestKey, timeCorrection: correction);

            var specificCode = correctedTotp.ComputeTotp(DateTime.UtcNow);
            var utcCode      = correctedTotp.ComputeTotp();

            Assert.AreEqual(specificCode, utcCode, "The 2 compute totp overloads didn't produce the same results");
        }
コード例 #5
0
        public void TimeCorrection_TotpRemainingSecondsSpecificDateWithCorrection()
        {
            var correction    = new TimeCorrection(DateTime.UtcNow.AddSeconds(15));
            var correctedTotp = new Totp(OtpCalculationTests.RfcTestKey, timeCorrection: correction);

            var specificRemaining = correctedTotp.RemainingSeconds(DateTime.UtcNow);
            var utcRemaining      = correctedTotp.RemainingSeconds();

            Assert.AreEqual(specificRemaining, utcRemaining, "The 2 remaining seconds overloads didn't produce the same results");
        }
コード例 #6
0
    protected void btnCreateTOTP_Click(object sender, EventArgs e)
    {
        byte[] secretArray = Base32Encoding.ToBytes(txtEnteredSecret.Text);

        var correction = new TimeCorrection(DateTime.UtcNow);

        TotpState.Totp = new Totp(secretArray, mode: OtpHashMode.Sha256, step: 30, timeCorrection: correction);

        var totpcode = TotpState.Totp.ComputeTotp(DateTime.Now);

        txtTOTPCode.Text = totpcode;
    }
コード例 #7
0
        public void TimeCorrection_TotpWithCorrectionGeneration()
        {
            var correction = new TimeCorrection(DateTime.UtcNow.AddSeconds(100)); // 100 ensures that at a minimum we are 3 steps away

            var correctedTotp   = new Totp(OtpCalculationTests.RfcTestKey, timeCorrection: correction);
            var uncorrectedTotp = new Totp(OtpCalculationTests.RfcTestKey);

            var uncorrectedCode = uncorrectedTotp.ComputeTotp(DateTime.UtcNow.AddSeconds(100));
            var correctedCode   = correctedTotp.ComputeTotp();

            Assert.AreEqual(uncorrectedCode, correctedCode);
        }
コード例 #8
0
        public void TimeCorrection_TotpVerifyTotpSpecificDateWithCorrection()
        {
            var correction    = new TimeCorrection(DateTime.UtcNow.AddSeconds(100)); // 100 ensures that at a minimum we are 3 steps away
            var correctedTotp = new Totp(OtpCalculationTests.RfcTestKey, timeCorrection: correction);

            var totpCode = correctedTotp.ComputeTotp();

            long specificStep, utcStep;

            Assert.IsTrue(correctedTotp.VerifyTotp(totpCode, out utcStep));
            Assert.IsTrue(correctedTotp.VerifyTotp(DateTime.UtcNow, totpCode, out specificStep));

            Assert.AreEqual(specificStep, utcStep, "The 2 verify totp overloads didn't produce the same results");
        }
コード例 #9
0
ファイル: Troubleshooting.cs プロジェクト: shugaoye/keeotp
        private void buttonPingGoogle_Click(object sender, EventArgs e)
        {
            // this is kind of an odd flow.

            TimeCorrection timeCorrection = null;

            // set up the asyn operation complete with continuation, catch and finally delegates
            // the async operation will use the synchronization context to post the continuation,
            // catch, and finally delegates to run on the UI thread.  Since we are creating this object
            // on the UI thread it will use the UI synchronization context.
            AsyncOperation getTimeCorrectionOperation = new AsyncOperation(() => // on background threadpool thread
            {
                timeCorrection = Ntp.GetTimeCorrectionFromGoogle();
            },
                                                                           () => // continuation on UI thread
            {
                var offset = timeCorrection.CorrectionFactor;

                var totalSeconds = Math.Abs(offset.TotalSeconds);
                if (totalSeconds == 0)
                {
                    MessageBox.Show("Your time is perfect according to Google's servers");
                }
                else if (totalSeconds <= 5)
                {
                    MessageBox.Show("Your time is off by five seconds or less from Google's servers.  You should be just fine.");
                }
                else if (totalSeconds <= 30)
                {
                    MessageBox.Show(string.Format("Your time is off by {0} seconds from Google's servers.  You are probably OK but correcting couldn't hurt.", totalSeconds));
                }
                else
                {
                    MessageBox.Show(string.Format("Your time is off by {0} seconds from Google's servers.  Try correcting the difference and try again.", totalSeconds));
                }
            },
                                                                           ex => MessageBox.Show(ex.Message, "Error"), // catch on UI thread
                                                                           () =>                                       // finally on UI thread
            {
                this.buttonPingGoogle.Visible = true;
                this.progressBarGettingTimeCorrection.Visible = false;
            });


            this.buttonPingGoogle.Visible = false;
            this.progressBarGettingTimeCorrection.Visible = true;

            getTimeCorrectionOperation.Run();
        }
コード例 #10
0
        public void TimeCorrection_TotpWithCorrectionVerification()
        {
            var correction = new TimeCorrection(DateTime.UtcNow.AddSeconds(100)); // 100 ensures that at a minimum we are 3 steps away

            var correctedTotp   = new Totp(OtpCalculationTests.RfcTestKey, timeCorrection: correction);
            var uncorrectedTotp = new Totp(OtpCalculationTests.RfcTestKey);

            var code = correctedTotp.ComputeTotp();

            long correctedStep, uncorrectedStep;

            Assert.IsTrue(correctedTotp.VerifyTotp(code, out correctedStep));
            Assert.IsTrue(uncorrectedTotp.VerifyTotp(DateTime.UtcNow.AddSeconds(100), code, out uncorrectedStep));

            Assert.AreEqual(uncorrectedStep, correctedStep);
        }
コード例 #11
0
        private void EqualTime(DateTime UTC, List <DateTime> expectTime)
        {
            TimeCorrection timeCorrection = new TimeCorrection();       //真正的被观察者
            ClockObserver  clockObserver  = new ClockObserver();        //观察者

            timeCorrection.ChangeTimeEvent += clockObserver.PekingTime; //注册事件
            timeCorrection.ChangeTimeEvent += clockObserver.LondonTime;
            timeCorrection.ChangeTimeEvent += clockObserver.MoscowTime;
            timeCorrection.ChangeTimeEvent += clockObserver.SydneyTime;
            timeCorrection.ChangeTimeEvent += clockObserver.NewyorkTime;
            timeCorrection.SomeValue        = UTC;                  //被观察者改变状态
            List <DateTime> actualTime = clockObserver.SomeValue(); //观察者反馈现象

            for (int i = 0; i < expectTime.Count; i++)
            {
                Assert.AreEqual(expectTime[i], actualTime[i]);
            }
        }
コード例 #12
0
ファイル: URLExpireOtp.cs プロジェクト: Inha-Tien/Inha
        /// <summary>
        /// Verify OTP
        /// </summary>
        /// <param name="otp"></param>
        /// <param name="step"></param>
        /// <param name="key"></param>
        /// <param name="mode"></param>
        /// <param name="totpSize"></param>
        /// <param name="timeCorrection"></param>
        /// <returns></returns>
        public static bool IsVerify(string otp, string key = "TC1S@ssc!@#", int step = 600, OtpHashMode mode = OtpHashMode.Sha1, int totpSize = 8, TimeCorrection timeCorrection = null)
        {
            Totp otpCalc = new Totp(Encoding.UTF8.GetBytes(key), step, OtpHashMode.Sha1, totpSize, timeCorrection);

            return(otpCalc.VerifyTotp(DateTime.Now, otp, out long timestemp));
        }
コード例 #13
0
ファイル: URLExpireOtp.cs プロジェクト: Inha-Tien/Inha
        //private static string key = "TC1S@ssc!@#";
        /// <summary>
        /// Tạo OTP
        /// </summary>
        /// <param name="key">Key dùng để mã hóa</param>
        /// <param name="step">Mã OTP sẽ được tạo mới sau [step] giây mặc định là 10 phút</param>
        /// <param name="mode">Các kiểu mã hóa</param>
        /// <param name="totpSize">Chiều dài mã OTP</param>
        /// <param name="timeCorrection">Thời gian</param>
        /// <returns></returns>
        public static string GenerateOtpCode(string key = "TC1S@ssc!@#", int step = 600, OtpHashMode mode = OtpHashMode.Sha1, int totpSize = 8, TimeCorrection timeCorrection = null)
        {
            Totp otpCalc = new Totp(Encoding.UTF8.GetBytes(key), step, OtpHashMode.Sha1, totpSize, timeCorrection);

            return(otpCalc.ComputeTotp(DateTime.Now));
        }