private void AddGroupEntries(PwGroup group)
        {
            // Build the entries ViewModels
            if (group.GetEntriesCount(false) > 0)
            {
                foreach (var item in group.GetEntries(false))
                {
                    _entries.Add(new PwEntryViewModel(item));
                }

                if (SettingsViewModel.Instance.SortingEnabled)
                {
                    _entries = _entries.OrderBy(e => e.Title).ToList();
                }
            }
        }
Example #2
0
        private static PwGroup WithParentGroups(PwGroup pg, PwDatabase pd)
        {
            if (pg == null)
            {
                Debug.Assert(false); return(null);
            }
            if (pd == null)
            {
                Debug.Assert(false); return(pg);
            }

            Dictionary <PwUuid, bool> dUuids = CollectUuids(pg);

            PwGroup pgNew = FilterCloneGroup(pd.RootGroup, dUuids);

            Debug.Assert(pgNew.GetEntriesCount(true) == pg.GetEntriesCount(true));
            return(pgNew);
        }
 public DBExpiringEntries(PwDatabase db, PwGroup pg)
 {
     this.db    = db;
     this.pg    = pg;
     EntryCount = pg != null?pg.GetEntriesCount(true) : 0;
 }
Example #4
0
        private static List <List <PwEntry> > FindDuplicatePasswordsEx(PwDatabase pd,
                                                                       IStatusLogger sl)
        {
            if ((pd == null) || !pd.IsOpen)
            {
                Debug.Assert(false); return(null);
            }
            PwGroup pg = pd.RootGroup;

            if (pg == null)
            {
                Debug.Assert(false); return(null);
            }

            uint uEntries     = pg.GetEntriesCount(true);
            uint uEntriesDone = 0;
            Dictionary <string, List <PwEntry> > d =
                new Dictionary <string, List <PwEntry> >();

            EntryHandler eh = delegate(PwEntry pe)
            {
                if ((sl != null) && (uEntries != 0))
                {
                    uint u = (uEntriesDone * 100) / uEntries;
                    if (!sl.SetProgress(u))
                    {
                        return(false);
                    }

                    ++uEntriesDone;
                }

                if (!pe.GetSearchingEnabled())
                {
                    return(true);
                }

                SprContext ctx = new SprContext(pe, pd, SprCompileFlags.NonActive);
                string     str = SprEngine.Compile(pe.Strings.ReadSafe(
                                                       PwDefs.PasswordField), ctx);
                if (str.Length != 0)
                {
                    List <PwEntry> l;
                    if (d.TryGetValue(str, out l))
                    {
                        l.Add(pe);
                    }
                    else
                    {
                        l = new List <PwEntry>();
                        l.Add(pe);
                        d[str] = l;
                    }
                }

                return(true);
            };

            if (!pg.TraverseTree(TraversalMethod.PreOrder, null, eh))
            {
                return(null);
            }

            List <List <PwEntry> > lRes = new List <List <PwEntry> >();

            foreach (List <PwEntry> l in d.Values)
            {
                if (l.Count <= 1)
                {
                    continue;
                }
                lRes.Add(l);
            }
            lRes.Sort(EntryUtil.CompareListSizeDesc);

            return(lRes);
        }
Example #5
0
        private static List <KeyValuePair <PwEntry, ulong> > CreatePwQualityListEx(
            PwDatabase pd, IStatusLogger sl)
        {
            if ((pd == null) || !pd.IsOpen)
            {
                Debug.Assert(false); return(null);
            }
            PwGroup pg = pd.RootGroup;

            if (pg == null)
            {
                Debug.Assert(false); return(null);
            }

            uint uEntries     = pg.GetEntriesCount(true);
            uint uEntriesDone = 0;
            List <KeyValuePair <PwEntry, ulong> > l = new List <KeyValuePair <PwEntry, ulong> >();

            EntryHandler eh = delegate(PwEntry pe)
            {
                if ((sl != null) && (uEntries != 0))
                {
                    uint u = (uEntriesDone * 100) / uEntries;
                    if (!sl.SetProgress(u))
                    {
                        return(false);
                    }
                }
                ++uEntriesDone;                 // Also used for sorting, see below

                if (!pe.GetSearchingEnabled())
                {
                    return(true);
                }

                SprContext ctx = new SprContext(pe, pd, SprCompileFlags.NonActive);
                string     str = SprEngine.Compile(pe.Strings.ReadSafe(
                                                       PwDefs.PasswordField), ctx);
                if (str.Length != 0)
                {
                    uint q = QualityEstimation.EstimatePasswordBits(str.ToCharArray());
                    l.Add(new KeyValuePair <PwEntry, ulong>(pe,
                                                            ((ulong)q << 32) | (ulong)uEntriesDone));
                }

                return(true);
            };

            if (!pg.TraverseTree(TraversalMethod.PreOrder, null, eh))
            {
                return(null);
            }

            Comparison <KeyValuePair <PwEntry, ulong> > fCompare = delegate(
                KeyValuePair <PwEntry, ulong> x, KeyValuePair <PwEntry, ulong> y)
            {
                return(x.Value.CompareTo(y.Value));
            };

            l.Sort(fCompare);

            return(l);
        }
Example #6
0
        private static List <EuSimilarPasswords> FindSimilarPasswordsEx(
            PwDatabase pd, IStatusLogger sl)
        {
            if ((pd == null) || !pd.IsOpen)
            {
                Debug.Assert(false); return(null);
            }
            PwGroup pg = pd.RootGroup;

            if (pg == null)
            {
                Debug.Assert(false); return(null);
            }

            const uint uPrePct  = 33;
            const long cPostPct = 67;

            uint           uEntries     = pg.GetEntriesCount(true);
            uint           uEntriesDone = 0;
            List <PwEntry> lEntries     = new List <PwEntry>();
            List <string>  lPasswords   = new List <string>();

            EntryHandler eh = delegate(PwEntry pe)
            {
                if ((sl != null) && (uEntries != 0))
                {
                    uint u = (uEntriesDone * uPrePct) / uEntries;
                    if (!sl.SetProgress(u))
                    {
                        return(false);
                    }

                    ++uEntriesDone;
                }

                if (!pe.GetSearchingEnabled())
                {
                    return(true);
                }

                SprContext ctx = new SprContext(pe, pd, SprCompileFlags.NonActive);
                string     str = SprEngine.Compile(pe.Strings.ReadSafe(
                                                       PwDefs.PasswordField), ctx);
                if (str.Length != 0)
                {
                    lEntries.Add(pe);
                    lPasswords.Add(str);
                }

                return(true);
            };

            if (!pg.TraverseTree(TraversalMethod.PreOrder, null, eh))
            {
                return(null);
            }

            Debug.Assert(TextSimilarity.LevenshteinDistance("Columns", "Comments") == 4);
            Debug.Assert(TextSimilarity.LevenshteinDistance("File/URL", "Field Value") == 8);

            long cTotal = ((long)lEntries.Count * (long)(lEntries.Count - 1)) / 2L;
            long cDone  = 0;

            List <EuSimilarPasswords> l = new List <EuSimilarPasswords>();

            for (int i = 0; i < (lEntries.Count - 1); ++i)
            {
                string strA = lPasswords[i];
                Debug.Assert(strA.Length != 0);                 // See above

                for (int j = i + 1; j < lEntries.Count; ++j)
                {
                    string strB = lPasswords[j];

                    if (strA != strB)
                    {
                        l.Add(new EuSimilarPasswords(lEntries[i], lEntries[j], 1.0f -
                                                     ((float)TextSimilarity.LevenshteinDistance(strA, strB) /
                                                      (float)Math.Max(strA.Length, strB.Length))));
                    }

                    if (sl != null)
                    {
                        ++cDone;

                        uint u = uPrePct + (uint)((cDone * cPostPct) / cTotal);
                        if (!sl.SetProgress(u))
                        {
                            return(null);
                        }
                    }
                }
            }
            Debug.Assert((cDone == cTotal) || (sl == null));

            Comparison <EuSimilarPasswords> fCmp = delegate(EuSimilarPasswords x,
                                                            EuSimilarPasswords y)
            {
                return(y.Similarity.CompareTo(x.Similarity));                // Descending
            };

            l.Sort(fCmp);

            int ciMax = Math.Max(lEntries.Count / 2, 20);

            if (l.Count > ciMax)
            {
                l.RemoveRange(ciMax, l.Count - ciMax);
            }

            return(l);
        }