Skip to content

Commit 471e684

Browse files
authored
Rename EntropyMeter -> Estimate (#1250)
* EntropyMeter -> Estimate * Cleaning estimate * Documentation * clang-formatting /cli
1 parent 18c4df9 commit 471e684

File tree

12 files changed

+201
-169
lines changed

12 files changed

+201
-169
lines changed

src/cli/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ set(cli_SOURCES
2222
Command.h
2323
Edit.cpp
2424
Edit.h
25-
EntropyMeter.cpp
26-
EntropyMeter.h
25+
Estimate.cpp
26+
Estimate.h
2727
Extract.cpp
2828
Extract.h
2929
List.cpp

src/cli/Clip.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@ int Clip::execute(QStringList arguments)
5555
parser.addOption(keyFile);
5656
parser.addPositionalArgument("entry", QObject::tr("Path of the entry to clip."));
5757
parser.addPositionalArgument(
58-
"timeout",
59-
QObject::tr("Timeout in seconds before clearing the clipboard."),
60-
QString("[timeout]"));
58+
"timeout", QObject::tr("Timeout in seconds before clearing the clipboard."), QString("[timeout]"));
6159
parser.process(arguments);
6260

6361
const QStringList args = parser.positionalArguments();

src/cli/Command.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
#include "Command.h"
2424

2525
#include "Add.h"
26-
#include "Edit.h"
2726
#include "Clip.h"
28-
#include "EntropyMeter.h"
27+
#include "Edit.h"
28+
#include "Estimate.h"
2929
#include "Extract.h"
3030
#include "List.h"
3131
#include "Locate.h"
@@ -62,7 +62,7 @@ void populateCommands()
6262
commands.insert(QString("add"), new Add());
6363
commands.insert(QString("clip"), new Clip());
6464
commands.insert(QString("edit"), new Edit());
65-
commands.insert(QString("entropy-meter"), new EntropyMeter());
65+
commands.insert(QString("estimate"), new Estimate());
6666
commands.insert(QString("extract"), new Extract());
6767
commands.insert(QString("locate"), new Locate());
6868
commands.insert(QString("ls"), new List());

src/cli/EntropyMeter.cpp

Lines changed: 0 additions & 141 deletions
This file was deleted.

src/cli/Estimate.cpp

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
/*
2+
* Copyright (C) 2017 KeePassXC Team <[email protected]>
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 2 or (at your option)
7+
* version 3 of the License.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
#include "Estimate.h"
19+
20+
#include <QCommandLineParser>
21+
#include <QTextStream>
22+
23+
#include <stdio.h>
24+
#include <stdlib.h>
25+
#include <string.h>
26+
#include <zxcvbn.h>
27+
28+
/* For pre-compiled headers under windows */
29+
#ifdef _WIN32
30+
#ifndef __MINGW32__
31+
#include "stdafx.h"
32+
#endif
33+
#endif
34+
35+
Estimate::Estimate()
36+
{
37+
this->name = QString("estimate");
38+
this->description = QObject::tr("Estimate the entropy of a password.");
39+
}
40+
41+
Estimate::~Estimate()
42+
{
43+
}
44+
45+
static void calculate(const char* pwd, bool advanced)
46+
{
47+
double e;
48+
int len = strlen(pwd);
49+
if (!advanced) {
50+
e = ZxcvbnMatch(pwd, 0, 0);
51+
printf("Pass '%s' \tLength %d\tEntropy %.3f\tLog10 %.3f\n", pwd, len, e, e * 0.301029996);
52+
} else {
53+
int ChkLen;
54+
ZxcMatch_t *info, *p;
55+
double m = 0.0;
56+
e = ZxcvbnMatch(pwd, 0, &info);
57+
for (p = info; p; p = p->Next) {
58+
m += p->Entrpy;
59+
}
60+
m = e - m;
61+
printf("Pass '%s' \tLength %d\tEntropy %.3f\tLog10 %.3f\n Multi-word extra bits %.1f\n",
62+
pwd,
63+
len,
64+
e,
65+
e * 0.301029996,
66+
m);
67+
p = info;
68+
ChkLen = 0;
69+
while (p) {
70+
int n;
71+
switch (static_cast<int>(p->Type)) {
72+
case BRUTE_MATCH:
73+
printf(" Type: Bruteforce ");
74+
break;
75+
case DICTIONARY_MATCH:
76+
printf(" Type: Dictionary ");
77+
break;
78+
case DICT_LEET_MATCH:
79+
printf(" Type: Dict+Leet ");
80+
break;
81+
case USER_MATCH:
82+
printf(" Type: User Words ");
83+
break;
84+
case USER_LEET_MATCH:
85+
printf(" Type: User+Leet ");
86+
break;
87+
case REPEATS_MATCH:
88+
printf(" Type: Repeated ");
89+
break;
90+
case SEQUENCE_MATCH:
91+
printf(" Type: Sequence ");
92+
break;
93+
case SPATIAL_MATCH:
94+
printf(" Type: Spatial ");
95+
break;
96+
case DATE_MATCH:
97+
printf(" Type: Date ");
98+
break;
99+
case BRUTE_MATCH + MULTIPLE_MATCH:
100+
printf(" Type: Bruteforce(Rep)");
101+
break;
102+
case DICTIONARY_MATCH + MULTIPLE_MATCH:
103+
printf(" Type: Dictionary(Rep)");
104+
break;
105+
case DICT_LEET_MATCH + MULTIPLE_MATCH:
106+
printf(" Type: Dict+Leet(Rep) ");
107+
break;
108+
case USER_MATCH + MULTIPLE_MATCH:
109+
printf(" Type: User Words(Rep)");
110+
break;
111+
case USER_LEET_MATCH + MULTIPLE_MATCH:
112+
printf(" Type: User+Leet(Rep) ");
113+
break;
114+
case REPEATS_MATCH + MULTIPLE_MATCH:
115+
printf(" Type: Repeated(Rep) ");
116+
break;
117+
case SEQUENCE_MATCH + MULTIPLE_MATCH:
118+
printf(" Type: Sequence(Rep) ");
119+
break;
120+
case SPATIAL_MATCH + MULTIPLE_MATCH:
121+
printf(" Type: Spatial(Rep) ");
122+
break;
123+
case DATE_MATCH + MULTIPLE_MATCH:
124+
printf(" Type: Date(Rep) ");
125+
break;
126+
127+
default:
128+
printf(" Type: Unknown%d ", p->Type);
129+
break;
130+
}
131+
ChkLen += p->Length;
132+
printf(" Length %d Entropy %6.3f (%.2f) ", p->Length, p->Entrpy, p->Entrpy * 0.301029996);
133+
for (n = 0; n < p->Length; ++n, ++pwd) {
134+
printf("%c", *pwd);
135+
}
136+
printf("\n");
137+
p = p->Next;
138+
}
139+
ZxcvbnFreeInfo(info);
140+
if (ChkLen != len) {
141+
printf("*** Password length (%d) != sum of length of parts (%d) ***\n", len, ChkLen);
142+
}
143+
}
144+
}
145+
146+
int Estimate::execute(QStringList arguments)
147+
{
148+
QTextStream inputTextStream(stdin, QIODevice::ReadOnly);
149+
QTextStream outputTextStream(stdout, QIODevice::WriteOnly);
150+
151+
QCommandLineParser parser;
152+
parser.setApplicationDescription(this->description);
153+
parser.addPositionalArgument("password", QObject::tr("Password for which to estimate the entropy."), "[password]");
154+
QCommandLineOption advancedOption(QStringList() << "a"
155+
<< "advanced",
156+
QObject::tr("Perform advanced analysis on the password."));
157+
parser.addOption(advancedOption);
158+
parser.process(arguments);
159+
160+
const QStringList args = parser.positionalArguments();
161+
if (args.size() > 1) {
162+
outputTextStream << parser.helpText().replace("keepassxc-cli", "keepassxc-cli estimate");
163+
return EXIT_FAILURE;
164+
}
165+
166+
QString password;
167+
if (args.size() == 1) {
168+
password = args.at(0);
169+
} else {
170+
password = inputTextStream.readLine();
171+
}
172+
173+
calculate(password.toLatin1(), parser.isSet(advancedOption));
174+
return EXIT_SUCCESS;
175+
}

src/cli/EntropyMeter.h renamed to src/cli/Estimate.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@
1515
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1616
*/
1717

18-
#ifndef KEEPASSXC_ENTROPYMETER_H
19-
#define KEEPASSXC_ENTROPYMETER_H
18+
#ifndef KEEPASSXC_ESTIMATE_H
19+
#define KEEPASSXC_ESTIMATE_H
2020

2121
#include "Command.h"
2222

23-
class EntropyMeter : public Command
23+
class Estimate : public Command
2424
{
2525
public:
26-
EntropyMeter();
27-
~EntropyMeter();
26+
Estimate();
27+
~Estimate();
2828
int execute(QStringList arguments);
2929
};
3030

31-
#endif // KEEPASSXC_ENTROPYMETER_H
31+
#endif // KEEPASSXC_ESTIMATE_H

src/cli/Extract.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ int Extract::execute(QStringList arguments)
8585
compositeKey.addKey(fileKey);
8686
}
8787

88-
8988
QString databaseFilename = args.at(0);
9089
QFile dbFile(databaseFilename);
9190
if (!dbFile.exists()) {

0 commit comments

Comments
 (0)