Delphi 检测密码强度 规则(仿 google)

一、密码长度:

  • 5 分: 小于等于 4 个字符
  • 10 分: 5 到 7 字符
  • 25 分: 大于等于 8 个字符

二、字母:

  • 0 分: 没有字母
  • 10 分: 全都是小(大)写字母
  • 20 分: 大小写混合字母

三、数字:

  • 0 分: 没有数字
  • 10 分: 1 个数字
  • 20 分: 大于等于 3 个数字

四、符号:

  • 0 分: 没有符号
  • 10 分: 1 个符号
  • 25 分: 大于 1 个符号

五、奖励:

  • 2 分: 字母和数字
  • 3 分: 字母、数字和符号
  • 5 分: 大小写字母、数字和符号

最后的评分标准:

  • = 90: 非常安全
  • >= 80: 安全(Secure)
  • >= 70: 非常强
  • >= 60: 强(Strong)
  • >= 50: 一般(Average)
  • >= 25: 弱(Weak)
  • >= 0: 非常弱
function CheckPassword(password: string): Integer;  //检测密码强度  规则(仿 google)
var
  i, countLowercase, countUppercase, countDigit, countSymbol: Integer;
begin
  Result := 0;
  // 计算密码长度得分
  if Length(password) <= 4 then
    Result := 5
  else if (Length(password) >= 5) and (Length(password) <= 7) then
    Result := 10
  else if Length(password) >= 8 then
    Result := 25;
  // 计算字母得分
  countLowercase := 0;
  countUppercase := 0;
  for i := 1 to Length(password) do
  begin
    if password[i] in ['a'..'z'] then
      Inc(countLowercase)
    else if password[i] in ['A'..'Z'] then
      Inc(countUppercase);
  end;
  if (countLowercase = 0) and (countUppercase = 0) then
    Result := Result + 0
  else if (countLowercase > 0) and (countUppercase = 0) or (countLowercase = 0) and (countUppercase > 0) then
    Result := Result + 10
  else if (countLowercase > 0) and (countUppercase > 0) then
    Result := Result + 20;
  // 计算数字得分
  countDigit := 0;
  for i := 1 to Length(password) do
  begin
   if password[i] in ['0'..'9'] then
      Inc(countDigit);
  end;
  if countDigit = 0 then
    Result := Result + 0
  else if countDigit = 1 then
    Result := Result + 10
  else if countDigit >= 3 then
    Result := Result + 20;
  // 计算符号得分
  countSymbol := 0;
  for i := 1 to Length(password) do
  begin
    if not (password[i] in ['a'..'z', 'A'..'Z', '0'..'9']) then
      Inc(countSymbol);
  end;
  if countSymbol = 0 then
    Result := Result + 0
  else if countSymbol = 1 then
    Result := Result + 10
  else if countSymbol > 1 then
    Result := Result + 25;
  // 计算奖励得分
  if (countLowercase > 0) and (countUppercase > 0) and (countDigit > 0) and (countSymbol = 0) then
    Result := Result + 2
  else if (countLowercase > 0) and (countUppercase > 0) and (countDigit > 0) and (countSymbol > 0) then
    Result := Result + 3
  else if (countLowercase > 0) and (countUppercase > 0) and (countDigit > 0) and (countSymbol > 0) then
    Result := Result + 5;
end;

function CheckPasswordw(score: Integer):String;
begin
    // 计算最终得分
  if score >= 90 then
    Result := '非常安全'
  else if score >= 80 then
    Result := '安全(Secure)'
  else if score >= 70 then
    Result := '非常强'
  else if score >= 60 then
    Result := '强(Strong)'
  else if score >= 50 then
    Result := '一般(Average)'
  else if score >= 25 then
    Result := '弱(Weak)'
  else
    Result := '非常弱'; // 密码非常弱
end;

procedure TForm1.Button1Click(Sender: TObject);  //检测
begin
   Edit2.Text:=IntToStr(CheckPassword(Edit1.Text));
   Edit3.Text:=CheckPasswordw(StrToInt(Edit2.Text));
end;

 

posted @ 2023-05-11 11:14  williamlv  阅读(247)  评论(0编辑  收藏  举报