-
Notifications
You must be signed in to change notification settings - Fork 0
/
StatComputer.cs
178 lines (154 loc) · 7.19 KB
/
StatComputer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
KeeStats - A plugin for Keepass Password Manager
Copyright (C) 2014 Andrea Decorte
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
using System;
using System.Collections.Generic;
using KeePassLib;
namespace KeeStats
{
/// <summary>
/// A static class which takes care of statistic computation process
/// </summary>
public static class StatComputer
{
// How many days maximum since the last access date to be considered recent
const int DAYS_RECENT_LAST_ACCESS_ENTRIES = 30;
/// <summary>
/// Computes the statistics
/// </summary>
/// <param name="group">The group on which to calculate the statistics</param>
/// <param name="genericStats">List that will be populated with generic stats</param>
/// <param name="qualityStats">Extended quality stats</param>
/// <param name="isRecursive">Whether the stats should also be calculated on subfolders</param>
/// <returns>False if there are no password in the selected group</returns>
public static bool ComputeStats(PwGroup group, ref List<StatItem> genericStats, ref List<ExtendedStatItem> qualityStats, bool isRecursive)
{
// average length for unique passwords + distribution
// most common?
// lower upper numbers special_chars
// 4 (single)
// 4*3*2*1 (double)
// (1+1 + 1 )
// 1
// 32 possibilità, unicita all'interno di ogni gruppo
// Average of last access ?
uint totalNumber = group.GetEntriesCount(isRecursive);
if (totalNumber == 0) {
return false;
}
genericStats.Add(new StatItem("Count", group.GetEntriesCount(isRecursive)));
genericStats.Add(new StatItem("Number of groups", group.GetGroups(isRecursive).UCount));
// HashSet
int shortestLength = 1000;
string shortestPass = "";
PwEntry shortestEntry = null;
int longestLength = 0;
string longestPass = "";
PwEntry longestEntry = null;
int passwordsAccessedRecently = 0;
int emptyPasswords = 0;
int totalLength = 0;
int entriesWithURL = 0;
int referencedPasswords = 0;
int lowercaseOnlyPasswords = 0;
int uppercaseOnlyPasswords = 0;
int numericOnlyPasswords = 0;
int alphanumericOnlyPasswords = 0;
int notAlphanumericOnlyPasswords = 0;
// Matches pattern {REF: }, which should be reference to other fields
const string refPattern = "^\\{REF:.*\\}$";
const string lowercaseOnlyPattern = "^[a-z]+$";
const string uppercaseOnlyPattern = "^[A-Z]+$";
const string numericOnlyPattern = "^[0-9]+$";
const string alphanumericOnlyPattern = "^(?=.*[A-Za-z])(?=.*[0-9])[A-Za-z0-9]+$";
const string notAlphanumericOnlyPattern = "^[^A-Za-z0-9]+$";
var refRegex = new System.Text.RegularExpressions.Regex(refPattern);
var lowercaseOnlyRegex = new System.Text.RegularExpressions.Regex(lowercaseOnlyPattern);
var uppercaseOnlyRegex = new System.Text.RegularExpressions.Regex(uppercaseOnlyPattern);
var numericOnlyRegex = new System.Text.RegularExpressions.Regex(numericOnlyPattern);
var alphanumericOnlyRegex = new System.Text.RegularExpressions.Regex(alphanumericOnlyPattern);
var notAlphanumericOnlyRegex = new System.Text.RegularExpressions.Regex(notAlphanumericOnlyPattern);
var passwords = new Dictionary<string, PwEntry>();
foreach (PwEntry aPasswordObject in group.GetEntries(isRecursive)) {
try {
if (aPasswordObject.Strings.ReadSafe(PwDefs.UrlField).Length > 0) {
entriesWithURL++;
}
// Recent access = less than DAYS_RECENT_LAST_ACCESS_ENTRIES days ago
if (aPasswordObject.LastAccessTime.AddDays(DAYS_RECENT_LAST_ACCESS_ENTRIES) >= DateTime.Now) {
passwordsAccessedRecently++;
}
string thePasswordString = aPasswordObject.Strings.ReadSafe(PwDefs.PasswordField);
int thePasswordStringLength = thePasswordString.Length;
// special case for empty passwords
if (thePasswordStringLength == 0) {
emptyPasswords++;
continue;
}
if (refRegex.IsMatch(thePasswordString)) {
referencedPasswords++;
continue;
}
// quality stats on password content. Currently exlusive content. Doesn't scale currently
if (lowercaseOnlyRegex.IsMatch(thePasswordString)) {
// only lowercase
lowercaseOnlyPasswords++;
} else if (uppercaseOnlyRegex.IsMatch(thePasswordString)) {
uppercaseOnlyPasswords++;
} else if (numericOnlyRegex.IsMatch(thePasswordString)) {
numericOnlyPasswords++;
} else if (alphanumericOnlyRegex.IsMatch(thePasswordString)) {
alphanumericOnlyPasswords++;
} else if (notAlphanumericOnlyRegex.IsMatch(thePasswordString)) {
notAlphanumericOnlyPasswords++;
}
passwords.Add(thePasswordString, aPasswordObject);
if (thePasswordStringLength < shortestLength) {
shortestPass = thePasswordString;
shortestLength = thePasswordStringLength;
shortestEntry = aPasswordObject;
}
if (thePasswordStringLength > longestLength) {
longestPass = thePasswordString;
longestLength = thePasswordStringLength;
longestEntry = aPasswordObject;
}
totalLength += thePasswordStringLength;
} catch(ArgumentException) {
// We want only unique passwords, so don't do anything
continue;
}
}
genericStats.Add(new StatItem("Empty passwords", emptyPasswords));
genericStats.Add(new StatItem("Unique pwds", passwords.Count));
genericStats.Add(new StatItem("% of unique pwds", (passwords.Count/(float) totalNumber)*100));
genericStats.Add(new StatItem("Average length", (totalLength / (float) passwords.Count)));
genericStats.Add(new StatItem("Percentage of entries with an URL", (entriesWithURL / (float) totalNumber)*100));
genericStats.Add(new StatItem("Referenced passwords", referencedPasswords));
genericStats.Add(new StatItem("% of passwords accessed recently", (passwordsAccessedRecently/(float) totalNumber)*100));
// if 0, we didn't see any password
if (longestLength > 0) {
qualityStats.Add(new ExtendedStatItem("Shortest password", shortestLength, shortestEntry));
qualityStats.Add(new ExtendedStatItem("Longest password", longestLength, longestEntry));
qualityStats.Add(new ExtendedStatItem("Lowercase only passwords", lowercaseOnlyPasswords));
qualityStats.Add(new ExtendedStatItem("Uppercase only passwords", uppercaseOnlyPasswords));
qualityStats.Add(new ExtendedStatItem("Numeric only passwords", numericOnlyPasswords));
qualityStats.Add(new ExtendedStatItem("Alphanumeric only passwords", alphanumericOnlyPasswords));
qualityStats.Add(new ExtendedStatItem("Not alphanumeric passwords", notAlphanumericOnlyPasswords));
}
return true;
}
}
}