コード例 #1
0
ファイル: KeePassOTPSetup.cs プロジェクト: numbnet/KeePassOTP
        private void UpdatePreview()
        {
            if (m_NoUpdate)
            {
                return;
            }
            if (StrUtil.IsDataUri(tbOTPSeed.Text))
            {
                try
                {
                    OTP.OTPAuthString = ParseFromImageByteArray(StrUtil.DataUriToData(tbOTPSeed.Text));
                    InitSettings(true);
                }
                catch { tbOTPSeed.Text = string.Empty; }
            }
            if (tbOTPSeed.Text.ToLowerInvariant().StartsWith("otpauth://"))
            {
                OTP.OTPAuthString = new ProtectedString(true, tbOTPSeed.Text);
                InitSettings(true);
            }
            else if (tbOTPSeed.Text.ToLowerInvariant().StartsWith("otpauth-migration://"))
            {
                m_NoUpdate = true;
                try
                {
                    int             iCount;
                    ProtectedString psGoogleAuth = PSConvert.ParseGoogleAuthExport(tbOTPSeed.Text, out iCount);
                    if ((iCount == 1) && (psGoogleAuth.Length > 0))
                    {
                        //tbOTPSeed.Text = psGoogleAuth.ReadString();
                        OTP.OTPAuthString = psGoogleAuth;
                        InitSettings(true);
                        return;
                    }
                    tbOTPSeed.Text = string.Empty;

                    if (iCount > 1)
                    {
                        Tools.ShowError(string.Format(PluginTranslate.ErrorGoogleAuthImportCount, iCount.ToString()));
                    }
                    else if (iCount == 0)
                    {
                        Tools.ShowError(PluginTranslate.ErrorGoogleAuthImport);
                    }
                    return;
                }
                finally { m_NoUpdate = false; }
            }

            else
            {
                OTP.OTPSeed = new ProtectedString(true, tbOTPSeed.Text);
            }

            if (cbOTPFormat.SelectedIndex == 0)
            {
                OTP.Encoding = KPOTPEncoding.BASE32;
            }
            if (cbOTPFormat.SelectedIndex == 1)
            {
                OTP.Encoding = KPOTPEncoding.BASE64;
            }
            if (cbOTPFormat.SelectedIndex == 2)
            {
                OTP.Encoding = KPOTPEncoding.HEX;
            }
            if (cbOTPFormat.SelectedIndex == 3)
            {
                OTP.Encoding = KPOTPEncoding.UTF8;
            }

            OTP.Length = cbOTPLength.SelectedIndex + 6;

            int dummy = -1;

            if (int.TryParse(tbTOTPTimestep.Text, out dummy))
            {
                OTP.TOTPTimestep = dummy;
            }
            if (int.TryParse(tbHOTPCounter.Text, out dummy))
            {
                OTP.HOTPCounter = dummy;
            }

            if (cbOTPHashFunc.SelectedIndex == 0)
            {
                OTP.Hash = KPOTPHash.SHA1;
            }
            if (cbOTPHashFunc.SelectedIndex == 1)
            {
                OTP.Hash = KPOTPHash.SHA256;
            }
            if (cbOTPHashFunc.SelectedIndex == 2)
            {
                OTP.Hash = KPOTPHash.SHA512;
            }

            if (cbOTPType.SelectedIndex == 0)
            {
                OTP.Type = KPOTPType.HOTP;
            }
            else if (cbOTPType.SelectedIndex == 1)
            {
                OTP.Type = KPOTPType.TOTP;
            }
            else if (cbOTPType.SelectedIndex == 2)
            {
                OTP.Type = KPOTPType.STEAM;
            }

            pbTOTPLifetime.Maximum = OTP.TOTPTimestep;
            pbTOTPLifetime.Value   = OTP.RemainingSeconds;
            gTime.Enabled          = tbTOTPTimestep.Visible = lTimestep.Visible = OTP.Type == KPOTPType.TOTP;
            pbTOTPLifetime.Visible = OTP.Type != KPOTPType.HOTP;
            tbHOTPCounter.Visible  = lCounter.Visible = OTP.Type == KPOTPType.HOTP;

            cbOTPLength.Enabled = OTP.Type != KPOTPType.STEAM;
            if (OTP.Type == KPOTPType.STEAM)
            {
                if (!cbOTPLength.Items.Contains("5"))
                {
                    cbOTPLength.Items.Add("5");
                }
                cbOTPLength.SelectedIndex = cbOTPLength.Items.Count - 1;
            }
            else if (cbOTPLength.Items.Contains("5"))
            {
                cbOTPLength.Items.RemoveAt(cbOTPLength.Items.Count - 1);
                cbOTPLength.SelectedIndex = 0;
            }

            if (!tbTOTPTimeCorrectionURL.Focused)
            {
                if (totpTimeCorrectionType.SelectedIndex == 0)
                {
                    if (!tbTOTPTimeCorrectionURL.ReadOnly)
                    {
                        m_BackupURL = tbTOTPTimeCorrectionURL.Text;
                    }
                    tbTOTPTimeCorrectionURL.Text     = string.Empty;
                    tbTOTPTimeCorrectionURL.ReadOnly = true;
                    OTP.TimeCorrectionUrlOwn         = false;
                    OTP.TimeCorrectionUrl            = string.Empty;
                }
                else if (totpTimeCorrectionType.SelectedIndex == 1)
                {
                    if (!tbTOTPTimeCorrectionURL.ReadOnly)
                    {
                        m_BackupURL = tbTOTPTimeCorrectionURL.Text;
                    }
                    tbTOTPTimeCorrectionURL.Text     = EntryUrl;
                    tbTOTPTimeCorrectionURL.ReadOnly = true;
                    OTP.TimeCorrectionUrlOwn         = true;
                    OTP.TimeCorrectionUrl            = EntryUrl;
                }
                else
                {
                    if (tbTOTPTimeCorrectionURL.ReadOnly)
                    {
                        tbTOTPTimeCorrectionURL.Text = string.IsNullOrEmpty(m_BackupURL) ? EntryUrl : m_BackupURL;
                    }
                    tbTOTPTimeCorrectionURL.ReadOnly = false;
                    OTP.TimeCorrectionUrlOwn         = false;
                    OTP.TimeCorrectionUrl            = tbTOTPTimeCorrectionURL.Text;
                }
            }
            totpTimeCorrectionValue.Text = OTP.OTPTimeCorrection.ToString();

            string otpValue = OTP.Valid ? OTP.ReadableOTP(OTP.GetOTP(false, true)) : PluginTranslate.Error;

            otpPreview.Text = "OTP: " + (string.IsNullOrEmpty(otpValue) ? "N/A" : otpValue);
            if ((OTP.Type != KPOTPType.HOTP) && OTP.RemainingSeconds <= Config.TOTPSoonExpiring)
            {
                otpPreview.ForeColor     = System.Drawing.Color.Red;
                pbTOTPLifetime.ForeColor = System.Drawing.Color.Red;
            }
            else
            {
                otpPreview.ForeColor     = System.Drawing.SystemColors.ControlText;
                pbTOTPLifetime.ForeColor = System.Drawing.SystemColors.Highlight;
            }

            otpValue            = OTP.Valid ? OTP.ReadableOTP(OTP.GetOTP(true, true)) : PluginTranslate.Error;
            otpPreviewNext.Text = "Next: " + (string.IsNullOrEmpty(otpValue) ? "N/A" : otpValue);
        }
コード例 #2
0
        private void UpdatePreview()
        {
            if (m_NoUpdate)
            {
                return;
            }
            if (tbOTPSeed.Text.ToLowerInvariant().StartsWith("otpauth://"))
            {
                OTP.OTPAuthString = new ProtectedString(true, tbOTPSeed.Text);
                InitSettings(true);
            }
            else
            {
                OTP.OTPSeed = new ProtectedString(true, tbOTPSeed.Text);
            }

            if (cbOTPFormat.SelectedIndex == 0)
            {
                OTP.Encoding = KPOTPEncoding.BASE32;
            }
            if (cbOTPFormat.SelectedIndex == 1)
            {
                OTP.Encoding = KPOTPEncoding.BASE64;
            }
            if (cbOTPFormat.SelectedIndex == 2)
            {
                OTP.Encoding = KPOTPEncoding.HEX;
            }
            if (cbOTPFormat.SelectedIndex == 3)
            {
                OTP.Encoding = KPOTPEncoding.UTF8;
            }

            OTP.Length = cbOTPLength.SelectedIndex + 6;

            int dummy = -1;

            if (int.TryParse(tbTOTPTimestep.Text, out dummy))
            {
                OTP.TOTPTimestep = dummy;
            }
            if (int.TryParse(tbHOTPCounter.Text, out dummy))
            {
                OTP.HOTPCounter = dummy;
            }

            if (cbOTPHashFunc.SelectedIndex == 0)
            {
                OTP.Hash = KPOTPHash.SHA1;
            }
            if (cbOTPHashFunc.SelectedIndex == 1)
            {
                OTP.Hash = KPOTPHash.SHA256;
            }
            if (cbOTPHashFunc.SelectedIndex == 2)
            {
                OTP.Hash = KPOTPHash.SHA512;
            }

            if (cbOTPType.SelectedIndex == 0)
            {
                OTP.Type = KPOTPType.HOTP;
            }
            if (cbOTPType.SelectedIndex == 1)
            {
                OTP.Type = KPOTPType.TOTP;
            }

            pbTOTPLifetime.Maximum = OTP.TOTPTimestep;
            pbTOTPLifetime.Value   = OTP.RemainingSeconds;
            gTime.Enabled          = pbTOTPLifetime.Visible = tbTOTPTimestep.Visible = lTimestep.Visible = OTP.Type == KPOTPType.TOTP;
            tbHOTPCounter.Visible  = lCounter.Visible = OTP.Type == KPOTPType.HOTP;

            if (!tbTOTPTimeCorrectionURL.Focused)
            {
                if (totpTimeCorrectionType.SelectedIndex == 0)
                {
                    if (!tbTOTPTimeCorrectionURL.ReadOnly)
                    {
                        m_BackupURL = tbTOTPTimeCorrectionURL.Text;
                    }
                    tbTOTPTimeCorrectionURL.Text     = string.Empty;
                    tbTOTPTimeCorrectionURL.ReadOnly = true;
                    OTP.TimeCorrectionUrlOwn         = false;
                    OTP.TimeCorrectionUrl            = string.Empty;
                }
                else if (totpTimeCorrectionType.SelectedIndex == 1)
                {
                    if (!tbTOTPTimeCorrectionURL.ReadOnly)
                    {
                        m_BackupURL = tbTOTPTimeCorrectionURL.Text;
                    }
                    tbTOTPTimeCorrectionURL.Text     = EntryUrl;
                    tbTOTPTimeCorrectionURL.ReadOnly = true;
                    OTP.TimeCorrectionUrlOwn         = true;
                    OTP.TimeCorrectionUrl            = EntryUrl;
                }
                else
                {
                    if (tbTOTPTimeCorrectionURL.ReadOnly)
                    {
                        tbTOTPTimeCorrectionURL.Text = string.IsNullOrEmpty(m_BackupURL) ? EntryUrl : m_BackupURL;
                    }
                    tbTOTPTimeCorrectionURL.ReadOnly = false;
                    OTP.TimeCorrectionUrlOwn         = false;
                    OTP.TimeCorrectionUrl            = tbTOTPTimeCorrectionURL.Text;
                }
            }
            totpTimeCorrectionValue.Text = OTP.OTPTimeCorrection.ToString();

            string otpValue = OTP.Valid ? OTP.ReadableOTP(OTP.GetOTP(false, true)) : PluginTranslate.Error;

            otpPreview.Text = "OTP: " + (string.IsNullOrEmpty(otpValue) ? "N/A" : otpValue);
            if ((OTP.Type == KPOTPType.TOTP) && OTP.RemainingSeconds <= 5)
            {
                otpPreview.ForeColor     = System.Drawing.Color.Red;
                pbTOTPLifetime.ForeColor = System.Drawing.Color.Red;
            }
            else
            {
                otpPreview.ForeColor     = System.Drawing.SystemColors.ControlText;
                pbTOTPLifetime.ForeColor = System.Drawing.SystemColors.Highlight;
            }

            otpValue            = OTP.Valid ? OTP.ReadableOTP(OTP.GetOTP(true, true)) : PluginTranslate.Error;
            otpPreviewNext.Text = "Next: " + (string.IsNullOrEmpty(otpValue) ? "N/A" : otpValue);
        }