From 56d9acef43a60ecc6a29354fb421641224977604 Mon Sep 17 00:00:00 2001 From: sniperbeamer Date: Mon, 22 Dec 2008 15:14:43 +0000 Subject: [PATCH] Fixed #2230887: No password generated using list of very special characters git-svn-id: https://svn.code.sf.net/p/keepassx/code/trunk@243 b624d157-de02-0410-bad0-e51aec6abb33 --- src/dialogs/PasswordGenDlg.cpp | 124 +++++++++++++++++---------------- src/dialogs/PasswordGenDlg.h | 16 +++-- 2 files changed, 77 insertions(+), 63 deletions(-) diff --git a/src/dialogs/PasswordGenDlg.cpp b/src/dialogs/PasswordGenDlg.cpp index 9aa5706..9d475c9 100644 --- a/src/dialogs/PasswordGenDlg.cpp +++ b/src/dialogs/PasswordGenDlg.cpp @@ -53,8 +53,8 @@ CGenPwDialog::CGenPwDialog(QWidget* parent, bool StandAloneMode,Qt::WFlags fl) connect(checkBoxPN, SIGNAL(toggled(bool)), SLOT(estimateQuality())); connect(checkBoxPS, SIGNAL(toggled(bool)), SLOT(estimateQuality())); connect(Spin_Num, SIGNAL(valueChanged(int)), SLOT(estimateQuality())); + connect(Check_ExcludeLookAlike, SIGNAL(toggled(bool)), SLOT(estimateQuality())); connect(Check_CollectEntropy, SIGNAL(stateChanged(int)), SLOT(OnCollectEntropyChanged(int))); - connect(Edit_chars, SIGNAL(textEdited(const QString&)), SLOT(OnCharsChanged(const QString&))); connect(ButtonChangeEchoMode, SIGNAL(clicked()), SLOT(SwapEchoMode())); connect(tabCategory, SIGNAL(currentChanged(int)), SLOT(setGenerateEnabled())); connect(Radio_1, SIGNAL(toggled(bool)), SLOT(setGenerateEnabled())); @@ -82,7 +82,9 @@ CGenPwDialog::CGenPwDialog(QWidget* parent, bool StandAloneMode,Qt::WFlags fl) DialogButtons->addButton(QDialogButtonBox::Close); AcceptButton=NULL; } - + + Edit_chars->setValidator(new PassCharValidator(this)); + tabCategory->setCurrentIndex(config->pwGenCategory()); QBitArray pwGenOptions=config->pwGenOptions(); Radio_1->setChecked(pwGenOptions.at(0)); @@ -184,7 +186,7 @@ void CGenPwDialog::OnRadio2StateChanged(bool state){ void CGenPwDialog::OnGeneratePw() { int length = Spin_Num->value(); - char* buffer = new char[length+1]; + QString password; if (tabCategory->currentIndex()==1) { @@ -198,35 +200,49 @@ void CGenPwDialog::OnGeneratePw() if (checkBoxPS->isChecked()) mode |= S_SS; + char* buffer = new char[length+1]; char* hyphenated_word = new char[length*18+1]; gen_pron_pass(buffer, hyphenated_word, length, length, mode); + password = buffer; delete[] hyphenated_word; + delete[] buffer; } else{ - generatePasswordInternal(buffer, length); + password = generatePasswordInternal(length); } - Edit_dest->setText(buffer); - delete[] buffer; + Edit_dest->setText(password); } void CGenPwDialog::estimateQuality(){ int num=0; if (tabCategory->currentIndex()==0){ if(Radio_1->isChecked()){ - if(checkBox1->isChecked()) + if(checkBox1->isChecked()) { num+=26; - if(checkBox2->isChecked()) + if (Check_ExcludeLookAlike->isChecked()) + num -= 2; + } + if(checkBox2->isChecked()) { num+=26; - if(checkBox3->isChecked()) + if (Check_ExcludeLookAlike->isChecked()) + num -= 1; + } + if(checkBox3->isChecked()) { num+=10; - if(checkBox4->isChecked()) + if (Check_ExcludeLookAlike->isChecked()) + num -= 2; + } + if(checkBox4->isChecked()) { num+=32; + if (Check_ExcludeLookAlike->isChecked()) + num -= 1; + } if(checkBox5->isChecked()) num++; - if(checkBox6->isChecked() && !checkBox4->isChecked()) + if(checkBox6->isChecked()) num++; - if(checkBox7->isChecked() && !checkBox4->isChecked()) + if(checkBox7->isChecked()) num++; } else @@ -243,43 +259,15 @@ void CGenPwDialog::estimateQuality(){ num+=32; } - float bits=0; - if(num) - bits=log((float)num)/log(2.0f); - bits=bits*((float)Spin_Num->value()); - Progress_Quali->setFormat(tr("%1 Bits").arg((int)bits)); + float bits = 0; + if (num) + bits = log((float)num) / log(2.0f); + bits = bits * ((float)Spin_Num->value()); + int bitsNum = (int) (bits+0.5); + + Progress_Quali->setFormat(tr("%1 Bits").arg(bitsNum)); Progress_Quali->update(); - if(bits>128)bits=128; - Progress_Quali->setValue((int)bits); -} - -void CGenPwDialog::OnCharsChanged(const QString& str){ - bool multiple=false; - for(int i=0;isetText(newstr); - + Progress_Quali->setValue((bitsNum > 128) ? 128 : bitsNum); } void CGenPwDialog::OnAccept() @@ -311,7 +299,7 @@ void CGenPwDialog::SwapEchoMode(){ } } -void CGenPwDialog::AddToAssoctable(char* table,int start,int end,int& pos){ +void CGenPwDialog::AddToAssoctable(QList& table,int start,int end,int& pos){ for (int i=start;i<=end;i++){ if (Check_ExcludeLookAlike->isChecked()){ switch (i){ @@ -324,12 +312,12 @@ void CGenPwDialog::AddToAssoctable(char* table,int start,int end,int& pos){ continue; } } - table[pos]=i; + table.append(QChar(i)); pos++; } } -CGenPwDialog::PwGroup CGenPwDialog::AddToAssoctableGroup(char* table,int start,int end,int& pos){ +CGenPwDialog::PwGroup CGenPwDialog::AddToAssoctableGroup(QList& table,int start,int end,int& pos){ PwGroup group; group.start = pos; AddToAssoctable(table,start,end,pos); @@ -337,7 +325,7 @@ CGenPwDialog::PwGroup CGenPwDialog::AddToAssoctableGroup(char* table,int start,i return group; } -void CGenPwDialog::generatePasswordInternal(char* buffer, int length){ +QString CGenPwDialog::generatePasswordInternal(int length){ /*------------------------------------------------------- ASCII ------------------------------------------------------- @@ -351,7 +339,7 @@ void CGenPwDialog::generatePasswordInternal(char* buffer, int length){ */ int num=0; - char assoctable[255]; + QList assoctable; int groups=0; bool ensureEveryGroup = false; QList groupTable; @@ -412,13 +400,11 @@ void CGenPwDialog::generatePasswordInternal(char* buffer, int length){ else{ QString str=Edit_chars->text(); for(int i=0;iisChecked()){ if((Check_CollectOncePerSession->isChecked() && !EntropyCollected) || !Check_CollectOncePerSession->isChecked()){ CollectEntropyDlg dlg(this); @@ -427,6 +413,8 @@ void CGenPwDialog::generatePasswordInternal(char* buffer, int length){ } } + QString password(length, '\0'); + if (ensureEveryGroup){ QList charPos; for (int i=0; isetEnabled(!str.isEmpty()); } + +PassCharValidator::PassCharValidator(QObject* parent) : QValidator(parent) { +} + +QValidator::State PassCharValidator::validate(QString& input, int& pos) const { + QSet chars; + + for (int i=0; i& table,int start,int end,int& pos); + PwGroup AddToAssoctableGroup(QList& table,int start,int end,int& pos); void paintEvent(QPaintEvent* event); - void generatePasswordInternal(char* buffer, int length); + QString generatePasswordInternal(int length); QPixmap BannerPixmap; static bool EntropyCollected; QPushButton* AcceptButton; @@ -52,11 +52,19 @@ class CGenPwDialog : public QDialog, public Ui_GenPwDlg void OnAccept(); void estimateQuality(); void OnCollectEntropyChanged(int); - void OnCharsChanged(const QString& str); void SwapEchoMode(); void setGenerateEnabled(); void setAcceptEnabled(const QString& str); }; +class PassCharValidator : public QValidator +{ + Q_OBJECT + + public: + PassCharValidator(QObject* parent); + State validate(QString& input, int& pos) const; +}; + #endif