Exemple #1
0
        private static void PerformTest(string expected, DateTime expectedValidUntil, TimeSpan timeSinceEpoch, int hashLevel)
        {
            using (var otpg = new TimeBasedPasswordGenerator())
            {
                if (hashLevel == 2)
                {
                    otpg.HashAlgorithm = new Sha256HashAlgorithm();
                    otpg.SetSecret((byte[])TestSecretKeySha256.Clone());
                }
                else if (hashLevel == 3)
                {
                    otpg.HashAlgorithm = new Sha512HashAlgorithm();
                    otpg.SetSecret((byte[])TestSecretKeySha512.Clone());
                }
                else
                {
                    otpg.HashAlgorithm = new Sha1HashAlgorithm();
                    otpg.SetSecret((byte[])TestSecretKeySha1.Clone());
                }

                otpg.Timestamp      = UnixEpoch.Add(timeSinceEpoch);
                otpg.PasswordLength = TestPasswordLength;
                Assert.AreEqual(expected, otpg.GeneratedPassword);
                Assert.AreEqual(expectedValidUntil, otpg.ValidUntilUtc);
            }
        }
Exemple #2
0
 public TOTP(byte[] secret)
 {
     this.totp = new TimeBasedPasswordGenerator(true, secret);
     this.totp.TimeInterval   = TimeSpan.FromSeconds(period);
     this.totp.PasswordLength = digits;
     this.secret = secret;
 }
Exemple #3
0
        public bool ConfirmCode(string code)
        {
            var time = DateTime.UtcNow;

            Debug.WriteLine(time);
            if (this.totp.GeneratedPassword.Equals(code))
            {
                return(true);
            }
            for (var i = 0; i < tolerance; i++)
            {
                var tmpTotp = new TimeBasedPasswordGenerator(true, this.secret);
                tmpTotp.TimeInterval   = TimeSpan.FromSeconds(period);
                tmpTotp.PasswordLength = digits;
                time = time.AddSeconds(-period);
                tmpTotp.Timestamp = time;
                var checkcode = tmpTotp.GeneratedPassword;
                if (checkcode.Equals(code))
                {
                    return(true);
                }
            }
            return(false);
        }