小编Kus*_*era的帖子

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

我正在使用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);

        } 
Run Code Online (Sandbox Code Playgroud)

c# one-time-password two-factor-authentication totp

9
推荐指数
1
解决办法
2万
查看次数