public string GetCode(long counter)
        {
            _generator ??= Type switch
            {
                AuthenticatorType.Totp => new Totp(Secret, Period, Algorithm, Digits),
                AuthenticatorType.Hotp => new Hotp(Secret, Algorithm, Digits),
                AuthenticatorType.MobileOtp => new MobileOtp(Secret, Digits),
                AuthenticatorType.SteamOtp => new SteamOtp(Secret),
                _ => throw new ArgumentException("Unknown authenticator type.")
            };

            switch (Type.GetGenerationMethod())
            {
            case GenerationMethod.Time:
                _code = _generator.Compute(counter);
                break;

            case GenerationMethod.Counter when _lastCounter == Counter:
                return(_code);

            case GenerationMethod.Counter:
            {
                _code        = _generator.Compute(Counter);
                _lastCounter = Counter;
                break;
            }
            }

            return(_code);
        }
示例#2
0
        private string GenerateCode()
        {
            var code = CodeUtil.PadCode(_generator.Compute(), _digits);

            Interlocked.Exchange(ref _secondsRemaining, _period - (int)DateTimeOffset.Now.ToUnixTimeSeconds() % _period);
            return(code);
        }
示例#3
0
        private void UpdateCode()
        {
            var code = _generator.Compute();

            var spacesInserted = 0;
            var groupSize      = Math.Min(MaxCodeGroupSize, _digits / 2);

            for (var i = 0; i < _digits; ++i)
            {
                if (i % groupSize == 0 && i > 0)
                {
                    code = code.Insert(i + spacesInserted, " ");
                    spacesInserted++;
                }
            }

            _codeTextView.Text = code;

            _secondsRemaining = _period - (int)DateTimeOffset.Now.ToUnixTimeSeconds() % _period;
            var progress = (int)Math.Floor((double)_progressBar.Max * _secondsRemaining / _period);

            _progressBar.Progress = progress;

            var animator = ObjectAnimator.OfInt(_progressBar, "progress", 0);

            animator.SetDuration(_secondsRemaining * 1000);
            animator.SetInterpolator(new LinearInterpolator());
            animator.Start();
        }
示例#4
0
        private void Refresh(object sender = null, ElapsedEventArgs args = null)
        {
            var now = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
            var generationOffset = now - (now % _period);

            var code      = _generator.Compute(generationOffset);
            var renewTime = generationOffset + _period;

            var secondsRemaining = Math.Max(renewTime - now, 0);

            RunOnUiThread(delegate
            {
                _codeTextView.Text = CodeUtil.PadCode(code, _digits, _codeGroupSize);
                _authProgressLayout.StartTimer((_period - secondsRemaining) * 1000);
            });
        }
示例#5
0
        private void UpdateCode()
        {
            var code = CodeUtil.PadCode(_generator.Compute(), _digits);

            _codeTextView.Text = code;

            _secondsRemaining = _period - (int)DateTimeOffset.Now.ToUnixTimeSeconds() % _period;
            var progress = (int)Math.Floor((double)_progressBar.Max * _secondsRemaining / _period);

            _progressBar.Progress = progress;

            var animator = ObjectAnimator.OfInt(_progressBar, "progress", 0);

            animator.SetDuration(_secondsRemaining * 1000);
            animator.SetInterpolator(new LinearInterpolator());
            animator.Start();
        }
示例#6
0
        public string GetCode()
        {
            _generator ??= Type switch
            {
                AuthenticatorType.Totp => new Totp(Secret, Period, Algorithm, Digits),
                AuthenticatorType.Hotp => new Hotp(Secret, Algorithm, Digits, Counter),
                AuthenticatorType.MobileOtp => new MobileOtp(Secret, Digits, Period),
                AuthenticatorType.SteamOtp => new SteamOtp(Secret),
                _ => throw new ArgumentException("Unknown authenticator type.")
            };

            switch (Type.GetGenerationMethod())
            {
            case GenerationMethod.Counter:
            {
                if (_lastCounter == Counter)
                {
                    break;
                }

                var counterGenerator = (CounterBasedGenerator)_generator;
                counterGenerator.Counter = Counter;
                var oldCode = _code;
                _code        = counterGenerator.Compute();
                _lastCounter = Counter;

                if (oldCode != null || Counter == 1)
                {
                    TimeRenew = counterGenerator.GetRenewTime();
                }

                break;
            }

            // Disregard milliseconds in case of timer inaccuracies
            case GenerationMethod.Time when TimeRenew.ToUnixTimeSeconds() <= DateTimeOffset.UtcNow.ToUnixTimeSeconds():
                _code = _generator.Compute();

                TimeRenew = _generator.GetRenewTime();
                break;
            }

            return(_code);
        }
示例#7
0
        public string GetCode()
        {
            _generator ??= Type switch
            {
                AuthenticatorType.Totp => new Totp(Secret, Period, Algorithm, Digits),
                AuthenticatorType.Hotp => new Hotp(Secret, Algorithm, Digits, Counter),
                AuthenticatorType.MobileOtp => new MobileOtp(Secret, Digits, Period),
                AuthenticatorType.SteamOtp => new SteamOtp(Secret),
                _ => throw new ArgumentException("Unknown authenticator type.")
            };

            switch (Type.GetGenerationMethod())
            {
            case GenerationMethod.Counter:
            {
                if (_lastCounter == Counter)
                {
                    break;
                }

                var counterGenerator = (CounterBasedGenerator)_generator;
                counterGenerator.Counter = Counter;
                var oldCode = _code;
                _code        = counterGenerator.Compute();
                _lastCounter = Counter;

                if (oldCode != null || Counter == 1)
                {
                    TimeRenew = counterGenerator.GetRenewTime();
                }

                break;
            }

            case GenerationMethod.Time:
                _code     = _generator.Compute();
                TimeRenew = _generator.GetRenewTime();
                break;
            }

            return(_code);
        }
示例#8
0
        private void Tick(object sender = null, ElapsedEventArgs e = null)
        {
            var now = DateTimeOffset.UtcNow.ToUnixTimeSeconds();

            if (_renewTime <= now)
            {
                var generationOffset = now - now % _period;
                var code             = _generator.Compute(generationOffset);
                _renewTime = generationOffset + _period;

                RunOnUiThread(delegate
                {
                    _codeTextView.Text = CodeUtil.PadCode(code, _digits);
                    UpdateProgressBar(now);
                });
            }
            else if (_animationScale == 0)
            {
                RunOnUiThread(delegate { UpdateProgressBar(now); });
            }
        }