public void OnShowQRCode(object sender, DynamicMenuEventArgs e)
        {
            var key = e.Tag as string;

            if (key == null)
            {
                return;
            }

            var pe = host.MainWindow.GetSelectedEntry(true);

            if (pe == null)
            {
                return;
            }

            var context = new SprContext(pe, host.Database, SprCompileFlags.All);
            var value   = SprEngine.Compile(pe.Strings.GetSafe(key).ReadString(), context);

            try
            {
                var data = new QRCodeGenerator().CreateQrCode(value, QRCodeGenerator.ECCLevel.L);
                if (data != null)
                {
                    var form = new ShowQRCodeForm(
                        host,
                        data.GetBitmap(10, Color.Black, Color.White),
                        SprEngine.Compile(pe.Strings.GetSafe(PwDefs.TitleField).ReadString(), context),
                        TryTranslateKey(key)
                        );
                    form.ShowDialog();
                }
            }
            catch
            {
                MessageBox.Show("The data can't be displayed as a QR Code.");
            }
        }
Example #2
0
        public void OnShowQRCode(object sender, DynamicMenuEventArgs e)
        {
            var key = e.Tag as string;

            if (key == null)
            {
                return;
            }

            var pe = host.MainWindow.GetSelectedEntry(true);

            if (pe == null)
            {
                return;
            }

            var context = new SprContext(pe, host.Database, SprCompileFlags.All);
            var value   = SprEngine.Compile(pe.Strings.GetSafe(key).ReadString(), context);

            if (key == "otp" && value.StartsWith("key="))
            {
                var title = SprEngine.Compile(pe.Strings.GetSafe("Title").ReadString(), context);
                var user  = SprEngine.Compile(pe.Strings.GetSafe("UserName").ReadString(), context);

                string label = null;
                if (title == string.Empty && user == string.Empty)
                {
                    label = "Unknown"; //edge case when entry has no title or user, nobody would use KeePass like this but just to be bulletproof
                }
                else if (title == string.Empty || user == string.Empty)
                {
                    label = title + user;
                }
                else
                {
                    label = title + " (" + user + ")"; //Not RFC 5234, but more readable in the auth app
                }

                label = Uri.EscapeDataString(label);
                value = string.Format("otpauth://totp/{0}?secret={1}", label, value.Replace("key=", ""));
            }

            try
            {
                var data = new QRCodeGenerator().CreateQrCode(value, QRCodeGenerator.ECCLevel.L);
                if (data != null)
                {
                    var form = new ShowQRCodeForm(
                        host,
                        data.GetBitmap(10, Color.Black, Color.White),
                        SprEngine.Compile(pe.Strings.GetSafe(PwDefs.TitleField).ReadString(), context),
                        TryTranslateKey(key)
                        );
                    form.ShowDialog();
                }
            }
            catch
            {
                MessageBox.Show("The data can't be displayed as a QR Code.");
            }
        }