Esempio n. 1
0
        public async Task NotSameTotp_OutOfTimeStep()
        {
            while (true)
            {
                if (DateTime.Now.Second % 5 == 0)
                {
                    break;
                }
                await Task.Delay(800);
            }
            var totp = new Totp()
                       .Secret(Key)
                       .Length(8)
                       .ValidFor(TimeSpan.FromSeconds(5));

            var value1 = totp.Compute();

            await Task.Delay(6000);

            var value2 = totp.Compute();

            Assert.AreNotEqual(value1, value2);
        }
Esempio n. 2
0
        public void ComputeTest(long offset, string expectedResult, HashAlgorithm algorithm)
        {
            var secret = algorithm switch
            {
                HashAlgorithm.Sha1 => Sha1Secret,
                HashAlgorithm.Sha256 => Sha256Secret,
                HashAlgorithm.Sha512 => Sha512Secret,
                _ => throw new ArgumentOutOfRangeException(nameof(algorithm))
            };

            var totp = new Totp(secret, 30, algorithm, 8);

            Assert.Equal(totp.Compute(offset), expectedResult);
        }
    }
Esempio n. 3
0
        public static void Main(string[] args)
        {
            var otp       = new Totp(OtpHashAlgorithm.SHA1, 4);
            var secretKey = "12345678901234567890";
            var output    = otp.Compute(secretKey);

            Console.WriteLine($"output: {output}");
            Thread.Sleep(1000 * 30);
            var verifyResult = otp.Verify(secretKey, output);

            Console.WriteLine($"Verify result: {verifyResult}");
            verifyResult = otp.Verify(secretKey, output, TimeSpan.FromSeconds(60));
            Console.WriteLine($"Verify result: {verifyResult}");

            Console.ReadLine();
        }