做应用程序的过程中,总是会涉及到用户管理,涉及到用户管理,那么总是避免不了需要涉及到用户密码的设置。今天我们看看怎么用Qt来管理用户密码,实现用户密码等级的设定。
首先我们设定一下密码强度的规则:
- 密码只能输入数字和字母(大小写敏感)
- 密码长度在6-8之间,必须包含数字以及字母(大小写不敏感), 此为密码最低等级
- 密码长度在8-16之间,必须包含数字以及字母(大小写其中一种),此为密码中间等级
- 密码长度在8-16,并且满足同时包含数字、小写字母、大写字母时为密码最强等级
先来看下效果,为了能够直观的显示输入的内容,将QLineEdit的显示模式设置为普通模式。
因此,根据上述规则,我们就能制定出跟密码等级相对应的正则表达式:
1、m_regStrong.setPattern(QString("(?=^.{8,16}$)((?=.*\\d)|(?=.*\\W+))(?![.\\n])(?=.*[A-Z])(?=.*[a-z]).*$"));
2、m_regMedium.setPattern(QString("(?!^\\d+$)(?!^[a-zA-Z]+$).{8,}"));
3、m_regWeak.setPattern(QString("(?!^\\d+$)(?!^[a-zA-Z]+$).{6,}"));
我们使用 QProgressBar来显示的展示密码的等级,以其0-100表示,33、66、100为其等级的区分,并且各等级之间显示颜色不同来区分。
设置密码等级的显示为:
void PasswdCheckWidget::setPasswdLevel(PasswordSafeLevel level)
{
switch (level) {
case Weak:
{
ui->progressBar->setValue(33);
ui->lbIntensity->setText("Weak");
ui->progressBar->setProperty("level", "weak");
ui->lbIntensity->setProperty("labeltype", "passwdlevelweak");
break;
}
case Medium:
{
ui->progressBar->setValue(66);
ui->lbIntensity->setText("Medium");
ui->progressBar->setProperty("level", "medium");
ui->lbIntensity->setProperty("labeltype", "passwdlevelmedium");
break;
}
case Strong:
{
ui->progressBar->setValue(100);
ui->lbIntensity->setText("Strong");
ui->progressBar->setProperty("level", "strong");
ui->lbIntensity->setProperty("labeltype", "passwdlevelstrong");
break;
}
default:
break;
}
this->style()->unpolish(ui->progressBar);
this->style()->polish(ui->progressBar);
this->style()->unpolish(ui->lbIntensity);
this->style()->polish(ui->lbIntensity);
if(!isVisible())
{
setVisible(true);
}
}
对于密码等级的检测则为:
bool PasswdCheckWidget::checkPasswdStrength()
{
//m_passWd为实时输入的密码
if(m_regStrong.exactMatch(m_passWd))
{
setPasswdLevel(Strong);
return true;
}
if(m_regMedium.exactMatch(m_passWd))
{
setPasswdLevel(Medium);
return true;
}
if(m_regWeak.exactMatch(m_passWd))
{
setPasswdLevel(Weak);
return true;
}
setVisible(false);
return false;
}
返回false时,表示该密码不满足任何等级。
为了在密码不满足任何等级的时候提醒密码规则,我们增加一个提示信息,提示信息的显示如下:
void PasswdCheckWidget::setPrompt(bool checked)
{
QString prompt = checked ? QString("At least 6 digits.\nMust contain both number and character.")
: QString("The two passwords are not the same.\nPlease try again.");
QString property = checked ? QString("passwdprompt") : QString("passwdpromptwarning");
ui->txtTips->setText(prompt);
ui->txtTips->setProperty("labeltype", property);
this->style()->unpolish(ui->txtTips);
this->style()->polish(ui->txtTips);
if(!isVisible())
{
setVisible(true);
}
}
其中true表示密码不满足各等级,false表示确认密码和输入密码不一样。
比较两者密码是否一致:
bool PasswdCheckWidget::checkPasswd()
{
if(m_passWd != m_cpassWd)
{
return false;
}
return true;
}
使用的过程中,提升两个该类,一个用来显示等级,一个用来显示提醒语句。密码输入框的信号调用:
ui->widgetLevel->setPassWd(value);
ui->widgetPrompt->setPassWd(value);
if(!ui->widgetLevel->checkPasswdStrength())
{
ui->widgetPrompt->setPrompt(true);
return;
}
ui->widgetPrompt->setVisible(false);
if(ui->lnCPasswd->text().isEmpty())
{
return;
}
if(!ui->widgetPrompt->checkPasswd())
{
ui->widgetPrompt->setPrompt(false);
return;
}
ui->widgetPrompt->setVisible(false);
密码确认框的输入信号调用:
ui->widgetLevel->setCPasswd(value);
ui->widgetPrompt->setCPasswd(value);
if(!ui->widgetLevel->checkPasswdStrength())
{
ui->widgetPrompt->setPrompt(true);
return;
}
if(!ui->widgetPrompt->checkPasswd())
{
ui->widgetPrompt->setPrompt(false);
return;
}
ui->widgetPrompt->setVisible(false);
测试代码。