Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RemainingSeconds is the same for all codes in same time step. #59

Open
capegreg opened this issue Apr 12, 2024 · 1 comment
Open

RemainingSeconds is the same for all codes in same time step. #59

capegreg opened this issue Apr 12, 2024 · 1 comment

Comments

@capegreg
Copy link

Why does generating a new code within the same time step but with a different key not have a new RemainingSeconds?

If I call GetTotp two times with a step of 9 minutes, the second call has a RemainingSeconds of the first call.

GetTotp takes a byte[] and count, 1, 2, 3, etc. the byte[] key is a hashed salt and count which generates a new code. I found that I had to make the key more unique than the previous call to GetTotp because calling GetTotp again (like when user wants a new key) within the 9 minute step just returned the same code.

Example. time step is 540 seconds.

GetTotp(salt, 1);
"309960"
RemainingSeconds 539

wait some seconds and call again...

GetTotp(salt, 2);
"718933"
RemainingSeconds 460

I expected RemainingSeconds to be closer to 539

        internal string GetTotp(byte[] salt, int count)
        {
            string saltedKey = HashSomeStringWithSalt(salt, count.ToString());
            byte[] key = Convert.FromBase64String(saltedKey);

            var totp = new Totp(key, step: 540, mode: OtpHashMode.Sha256, totpSize: 6);

            var dateTimeNow = DateTime.UtcNow;
            var totpCode = totp.ComputeTotp(dateTimeNow);
            return totpCode;
        }


        internal int GetOTPRemainingSeconds(byte[] salt, int count)
        {
            string saltedKey = HashSomeStringWithSalt(salt, count.ToString());
            byte[] key = Convert.FromBase64String(key);
            var totp = new Totp(key, step: 540, mode: OtpHashMode.Sha256, totpSize: 6);

            return totp.RemainingSeconds();
        }


@kspearrin
Copy link
Owner

See RemainingSeconds implementation https://github.com/kspearrin/Otp.NET/blob/master/src/Otp.NET/Totp.cs#L181-L200

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants