使用otp.net生成和验证OTP代码

时间:2019-11-26 12:44:53

标签: c# two-factor-authentication one-time-password totp

我正在使用https://www.nuget.org/packages/Otp.NET库来生成和验证OTP。我想使用TOTP算法。生成的OTP需要有效5分钟。库建议使用var totp = new Totp(secretKey, step: 300);这样。但是OTP在5分钟前失效了

完整代码

public static void GenarateTOTP()
        {
            var bytes = Base32Encoding.ToBytes("JBSWY3DPEHPK3PXP");

            var totp = new Totp(bytes, step: 300);

            var result = totp.ComputeTotp(DateTime.UtcNow);

            Console.WriteLine(result);

            var input = Console.ReadLine();
            long timeStepMatched;
            bool verify = totp.VerifyTotp(input, out timeStepMatched, window: null);

            Console.WriteLine("{0}-:{1}", "timeStepMatched",timeStepMatched);
            Console.WriteLine("{0}-:{1}", "Remaining seconds", totp.RemainingSeconds());
            Console.WriteLine("{0}-:{1}", "verify", verify);

        } 

0 个答案:

没有答案