Exemplo n.º 1
0
 private void linkLabelLoadUriScanQR_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
 {
     if (scanQRMode)
     {
         if (MessageBox.Show(KeeOtp2Statics.MessageBoxScanQrCode, KeeOtp2Statics.OtpInformationScanQr, MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
         {
             scanQRCode();
         }
     }
     else
     {
         try
         {
             this.data = OtpAuthUtils.uriToOtpAuthData(new Uri(textBoxKey.Text));
             loadData();
         }
         catch (InvalidBase32FormatException ex)
         {
             MessageBox.Show(ex.Message, KeeOtp2Statics.InvalidBase32Format, MessageBoxButtons.OK, MessageBoxIcon.Error);
         }
         catch (InvalidUriFormat ex)
         {
             MessageBox.Show(ex.Message, KeeOtp2Statics.InvalidUriFormat, MessageBoxButtons.OK, MessageBoxIcon.Error);
         }
         catch (Exception ex)
         {
             MessageBox.Show(String.Format(KeeOtp2Statics.MessageBoxException, ex.Message), KeeOtp2Statics.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
         }
     }
 }
Exemplo n.º 2
0
        private void scanQRCode()
        {
            Uri            uri    = null;
            IBarcodeReader reader = new BarcodeReader();
            Bitmap         bmpScreenshot;
            Graphics       gfxScreenshot;

            this.Hide();
            foreach (Screen sc in Screen.AllScreens)
            {
                bmpScreenshot = new Bitmap(sc.Bounds.Width, sc.Bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                gfxScreenshot = Graphics.FromImage(bmpScreenshot);
                gfxScreenshot.CopyFromScreen(sc.Bounds.X, sc.Bounds.Y, 0, 0, sc.Bounds.Size, CopyPixelOperation.SourceCopy);
                var result = reader.Decode(bmpScreenshot);
                if (result != null)
                {
                    if (result.ToString().StartsWith("otpauth"))
                    {
                        uri = new Uri(result.ToString());
                    }
                }
            }

            this.Show();

            if (uri != null)
            {
                MessageBox.Show(KeeOtp2Statics.MessageBoxQrCodeFound, KeeOtp2Statics.OtpInformationScanQr, MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.data = OtpAuthUtils.uriToOtpAuthData(uri);
                loadData();
            }
            else
            {
                if (MessageBox.Show(KeeOtp2Statics.MessageBoxQrCodeNotFound, KeeOtp2Statics.OtpInformationScanQr, MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning) == DialogResult.Retry)
                {
                    scanQRCode();
                }
            }
        }
Exemplo n.º 3
0
        private OtpAuthData readData(bool skipType = false)
        {
            if (OtpAuthUtils.checkUriString(textBoxKey.Text))
            {
                OtpAuthData data = OtpAuthUtils.uriToOtpAuthData(new Uri(textBoxKey.Text));
                if (this.data != null)
                {
                    data.loadedFields = this.data.loadedFields;
                }
                return(data);
            }
            else
            {
                OtpAuthData data = new OtpAuthData();

                if (this.data != null)
                {
                    data = (OtpAuthData)this.data.Clone();
                }

                string secret = textBoxKey.Text.Replace(" ", string.Empty).Replace("-", string.Empty);
                if (string.IsNullOrEmpty(this.textBoxKey.Text))
                {
                    throw new InvalidOtpConfiguration(KeeOtp2Statics.InvalidOtpConfigurationMissingSecret);
                }

                if (checkBoxCustomSettings.Checked)
                {
                    if (this.radioButtonBase32.Checked)
                    {
                        data.Encoding = OtpSecretEncoding.Base32;
                    }
                    else if (this.radioButtonBase64.Checked)
                    {
                        data.Encoding = OtpSecretEncoding.Base64;
                    }
                    else if (this.radioButtonHex.Checked)
                    {
                        data.Encoding = OtpSecretEncoding.Hex;
                    }
                    else if (this.radioButtonUtf8.Checked)
                    {
                        data.Encoding = OtpSecretEncoding.UTF8;
                    }
                }

                // Secret validation, will throw an error if invalid
                secret = OtpAuthUtils.correctPlainSecret(secret, data.Encoding);
                OtpAuthUtils.validatePlainSecret(secret, data.Encoding);

                if (checkBoxCustomSettings.Checked)
                {
                    if (!skipType)
                    {
                        data.Type = comboBoxTypeIndexValue[comboBoxType.SelectedIndex];
                    }

                    if (data.Type == OtpType.Totp || data.Type == OtpType.Steam)
                    {
                        int period = 30;
                        if (int.TryParse(this.textBoxPeriodCounter.Text, out period))
                        {
                            if (period <= 0)
                            {
                                throw new InvalidOtpConfiguration(KeeOtp2Statics.InvalidOtpConfigurationInvalidInteger);
                            }
                        }
                        else
                        {
                            throw new InvalidOtpConfiguration(KeeOtp2Statics.InvalidOtpConfigurationInvalidInteger);
                        }
                        data.Period = period;
                    }
                    else if (data.Type == OtpType.Hotp)
                    {
                        int counter = 0;
                        if (int.TryParse(this.textBoxPeriodCounter.Text, out counter))
                        {
                            if (counter < 0)
                            {
                                throw new InvalidOtpConfiguration(KeeOtp2Statics.InvalidOtpConfigurationInvalidInteger);
                            }
                        }
                        else
                        {
                            throw new InvalidOtpConfiguration(KeeOtp2Statics.InvalidOtpConfigurationInvalidInteger);
                        }
                        data.Counter = counter;
                    }

                    data.Digits = comboBoxLengthIndexValue[comboBoxLength.SelectedIndex];

                    if (this.radioButtonSha1.Checked)
                    {
                        data.Algorithm = OtpHashMode.Sha1;
                    }
                    else if (this.radioButtonSha256.Checked)
                    {
                        data.Algorithm = OtpHashMode.Sha256;
                    }
                    else if (this.radioButtonSha512.Checked)
                    {
                        data.Algorithm = OtpHashMode.Sha512;
                    }

                    data.KeeOtp1Mode = checkboxOldKeeOtp.Checked;
                }

                data.SetPlainSecret(secret);

                return(data);
            }
        }