Merge remote-tracking branch 'upstream/develop' into develop

This commit is contained in:
Vladimir Svyatski
2018-06-27 23:40:20 +03:00
34 changed files with 1497 additions and 270 deletions

View File

@@ -21,10 +21,13 @@
#include "crypto/Random.h"
#include <zxcvbn.h>
const char* PasswordGenerator::DefaultExcludedChars = "";
PasswordGenerator::PasswordGenerator()
: m_length(0)
, m_classes(0)
, m_flags(0)
, m_excluded(PasswordGenerator::DefaultExcludedChars)
{
}
@@ -56,6 +59,11 @@ void PasswordGenerator::setFlags(const GeneratorFlags& flags)
m_flags = flags;
}
void PasswordGenerator::setExcludedChars(const QString& chars)
{
m_excluded = chars;
}
QString PasswordGenerator::generatePassword() const
{
Q_ASSERT(isValid());
@@ -130,6 +138,10 @@ bool PasswordGenerator::isValid() const
return false;
}
if (passwordGroups().size() == 0) {
return false;
}
return true;
}
@@ -140,7 +152,8 @@ QVector<PasswordGroup> PasswordGenerator::passwordGroups() const
if (m_classes & LowerLetters) {
PasswordGroup group;
for (int i = 97; i < (97 + 26); i++) {
for (int i = 97; i <= (97 + 25); i++) {
if ((m_flags & ExcludeLookAlike) && (i == 108)) { // "l"
continue;
}
@@ -153,7 +166,8 @@ QVector<PasswordGroup> PasswordGenerator::passwordGroups() const
if (m_classes & UpperLetters) {
PasswordGroup group;
for (int i = 65; i < (65 + 26); i++) {
for (int i = 65; i <= (65 + 25); i++) {
if ((m_flags & ExcludeLookAlike) && (i == 73 || i == 79)) { // "I" and "O"
continue;
}
@@ -176,28 +190,79 @@ QVector<PasswordGroup> PasswordGenerator::passwordGroups() const
passwordGroups.append(group);
}
if (m_classes & SpecialCharacters) {
if (m_classes & Braces) {
PasswordGroup group;
for (int i = 33; i <= 47; i++) {
group.append(i);
// ()[]{}
group.append(40);
group.append(41);
group.append(91);
group.append(93);
group.append(123);
group.append(125);
passwordGroups.append(group);
}
if (m_classes & Punctuation) {
PasswordGroup group;
// .,:;
group.append(44);
group.append(46);
group.append(58);
group.append(59);
passwordGroups.append(group);
}
if (m_classes & Quotes) {
PasswordGroup group;
// "'
group.append(34);
group.append(39);
passwordGroups.append(group);
}
if (m_classes & Dashes) {
PasswordGroup group;
// -/\_|
group.append(45);
group.append(47);
group.append(92);
group.append(95);
if (!(m_flags & ExcludeLookAlike)) {
group.append(124); // "|"
}
for (int i = 58; i <= 64; i++) {
group.append(i);
}
for (int i = 91; i <= 96; i++) {
group.append(i);
}
for (int i = 123; i <= 126; i++) {
if ((m_flags & ExcludeLookAlike) && (i == 124)) { // "|"
continue;
}
passwordGroups.append(group);
}
if (m_classes & Math) {
PasswordGroup group;
// !*+-<=>?
group.append(33);
group.append(42);
group.append(43);
group.append(60);
group.append(61);
group.append(62);
group.append(63);
passwordGroups.append(group);
}
if (m_classes & Logograms) {
PasswordGroup group;
// #$%&
for (int i = 35; i <= 38; i++) {
group.append(i);
}
// @^`~
group.append(64);
group.append(94);
group.append(96);
group.append(126);
passwordGroups.append(group);
}
@@ -220,6 +285,27 @@ QVector<PasswordGroup> PasswordGenerator::passwordGroups() const
passwordGroups.append(group);
}
// Loop over character groups and remove excluded characters from them;
// remove empty groups
int i = 0;
while (i != passwordGroups.size()) {
PasswordGroup group = passwordGroups[i];
for (QChar ch : m_excluded) {
int j = group.indexOf(ch);
while (j != -1) {
group.remove(j);
j = group.indexOf(ch);
}
}
if (group.size() > 0) {
passwordGroups.replace(i, group);
i++;
} else {
passwordGroups.remove(i);
}
}
return passwordGroups;
}
@@ -236,7 +322,22 @@ int PasswordGenerator::numCharClasses() const
if (m_classes & Numbers) {
numClasses++;
}
if (m_classes & SpecialCharacters) {
if (m_classes & Braces) {
numClasses++;
}
if (m_classes & Punctuation) {
numClasses++;
}
if (m_classes & Quotes) {
numClasses++;
}
if (m_classes & Dashes) {
numClasses++;
}
if (m_classes & Math) {
numClasses++;
}
if (m_classes & Logograms) {
numClasses++;
}
if (m_classes & EASCII) {

View File

@@ -30,19 +30,26 @@ class PasswordGenerator
public:
enum CharClass
{
LowerLetters = 0x1,
UpperLetters = 0x2,
Numbers = 0x4,
SpecialCharacters = 0x8,
EASCII = 0x10,
LowerLetters = (1 << 0),
UpperLetters = (1 << 1),
Numbers = (1 << 2),
Braces = (1 << 3),
Punctuation = (1 << 4),
Quotes = (1 << 5),
Dashes = (1 << 6),
Math = (1 << 7),
Logograms = (1 << 8),
SpecialCharacters = Braces | Punctuation | Quotes | Dashes | Math | Logograms,
EASCII = (1 << 9),
DefaultCharset = LowerLetters | UpperLetters | Numbers
};
Q_DECLARE_FLAGS(CharClasses, CharClass)
enum GeneratorFlag
{
ExcludeLookAlike = 0x1,
CharFromEveryGroup = 0x2,
ExcludeLookAlike = (1 << 0),
CharFromEveryGroup = (1 << 1),
AdvancedMode = (1 << 2),
DefaultFlags = ExcludeLookAlike | CharFromEveryGroup
};
Q_DECLARE_FLAGS(GeneratorFlags, GeneratorFlag)
@@ -54,6 +61,7 @@ public:
void setLength(int length);
void setCharClasses(const CharClasses& classes);
void setFlags(const GeneratorFlags& flags);
void setExcludedChars(const QString& chars);
bool isValid() const;
@@ -61,10 +69,18 @@ public:
int getbits() const;
static const int DefaultLength = 16;
static const char* DefaultExcludedChars;
static constexpr bool DefaultLower = (DefaultCharset & LowerLetters) != 0;
static constexpr bool DefaultUpper = (DefaultCharset & UpperLetters) != 0;
static constexpr bool DefaultNumbers = (DefaultCharset & Numbers) != 0;
static constexpr bool DefaultSpecial = (DefaultCharset & SpecialCharacters) != 0;
static constexpr bool DefaultAdvancedMode = (DefaultFlags & AdvancedMode) != 0;
static constexpr bool DefaultBraces = (DefaultCharset & Braces) != 0;
static constexpr bool DefaultPunctuation = (DefaultCharset & Punctuation) != 0;
static constexpr bool DefaultQuotes = (DefaultCharset & Quotes) != 0;
static constexpr bool DefaultDashes = (DefaultCharset & Dashes) != 0;
static constexpr bool DefaultMath = (DefaultCharset & Math) != 0;
static constexpr bool DefaultLogograms = (DefaultCharset & Logograms) != 0;
static constexpr bool DefaultEASCII = (DefaultCharset & EASCII) != 0;
static constexpr bool DefaultLookAlike = (DefaultFlags & ExcludeLookAlike) != 0;
static constexpr bool DefaultFromEveryGroup = (DefaultFlags & CharFromEveryGroup) != 0;
@@ -76,6 +92,7 @@ private:
int m_length;
CharClasses m_classes;
GeneratorFlags m_flags;
QString m_excluded;
Q_DISABLE_COPY(PasswordGenerator)
};

View File

@@ -24,6 +24,7 @@
#include <QImageReader>
#include <QLocale>
#include <QStringList>
#include <cctype>
#include <QElapsedTimer>
@@ -56,8 +57,9 @@
namespace Tools
{
QString humanReadableFileSize(qint64 bytes)
QString humanReadableFileSize(qint64 bytes, quint32 precision)
{
constexpr auto kibibyte = 1024;
double size = bytes;
QStringList units = QStringList() << "B"
@@ -67,12 +69,12 @@ namespace Tools
int i = 0;
int maxI = units.size() - 1;
while ((size >= 1024) && (i < maxI)) {
size /= 1024;
while ((size >= kibibyte) && (i < maxI)) {
size /= kibibyte;
i++;
}
return QString("%1 %2").arg(QLocale().toString(size, 'f', 2), units.at(i));
return QString("%1 %2").arg(QLocale().toString(size, 'f', precision), units.at(i));
}
bool hasChild(const QObject* parent, const QObject* child)
@@ -147,8 +149,8 @@ namespace Tools
bool isHex(const QByteArray& ba)
{
for (char c : ba) {
if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))) {
for (const unsigned char c : ba) {
if (!std::isxdigit(c)) {
return false;
}
}
@@ -158,8 +160,8 @@ namespace Tools
bool isBase64(const QByteArray& ba)
{
QRegExp regexp(
"^(?:[a-z0-9+/]{4})*(?:[a-z0-9+/]{3}=|[a-z0-9+/]{2}==)?$", Qt::CaseInsensitive, QRegExp::RegExp2);
constexpr auto pattern = R"(^(?:[a-z0-9+]{4})*(?:[a-z0-9+]{3}=|[a-z0-9+]{2}==)?$)";
QRegExp regexp(pattern, Qt::CaseInsensitive, QRegExp::RegExp2);
QString base64 = QString::fromLatin1(ba.constData(), ba.size());
@@ -318,14 +320,15 @@ namespace Tools
}
// Set discretionary access control list
bSuccess = ERROR_SUCCESS == SetSecurityInfo(GetCurrentProcess(), // object handle
SE_KERNEL_OBJECT, // type of object
DACL_SECURITY_INFORMATION, // change only the objects DACL
nullptr,
nullptr, // do not change owner or group
pACL, // DACL specified
nullptr // do not change SACL
);
bSuccess = ERROR_SUCCESS
== SetSecurityInfo(GetCurrentProcess(), // object handle
SE_KERNEL_OBJECT, // type of object
DACL_SECURITY_INFORMATION, // change only the objects DACL
nullptr,
nullptr, // do not change owner or group
pACL, // DACL specified
nullptr // do not change SACL
);
Cleanup:

View File

@@ -32,7 +32,7 @@ class QIODevice;
namespace Tools
{
QString humanReadableFileSize(qint64 bytes);
QString humanReadableFileSize(qint64 bytes, quint32 precision = 2);
bool hasChild(const QObject* parent, const QObject* child);
bool readFromDevice(QIODevice* device, QByteArray& data, int size = 16384);
bool readAllFromDevice(QIODevice* device, QByteArray& data);