Friday, 3 July 2015

Complexity of Password Delphi Coding

Password can be more secure depend on complexity of password which include numbers, symbols and both upper or lowercase alphabet characters.

The requirement of password composition here, include at least 1 number, 1 special character, 1 upper case and 1 lowercase alphabet character:

var
  StatusMsg: string;
  tmp : string;
 aOldPsw, aNewPsw, aVerNewPsw: string;
  i, LowerC, UpperC, NumC, SymbolC : integer;
begin
  try
    ModalResult:= mrNone;

    // get vals
     aOldPsw:= Trim(OldPassword.Text);
    aNewPsw:= Trim(NewPassword.Text);
    aVerNewPsw:= Trim(VerifyNewPassword.Text);

    LowerC:= 0;
    UpperC:= 0;
    NumC:= 0;
    SymbolC:= 0;
  
     //----- complexity : at least 1 lowercase, 1 uppercase, 1 symbol, 1 digit----
     for i := 1 to length(aNewPsw) do
     begin
        tmp:= copy(aNewPsw, i, 1);

       if (tmp[1] in ['a'..'z']) then LowerC:= LowerC + 1;
       if (tmp[1] in ['A'..'Z']) then UpperC:= UpperC + 1;
       if (tmp[1] in ['0'..'9']) then NumC:= NumC + 1;
       if not (tmp[1] in ['0'..'9', 'A'..'Z', 'a'..'z']) then
          SymbolC:= SymbolC + 1;
     end; // end for

     if ((LowerC = 0) or (UpperC = 0) or (NumC = 0) or (SymbolC = 0)) then
     begin
       MessageDlg('Password has at least 1 lowercase, 1 uppercase, 1 symbol, 1 digit', mtError, [mbOk], 0);
       exit;
     end;

   // showmessage('LowerC: ' + inttostr(LowerC) + '    UpperC: ' + inttostr(UpperC) +
  //         '    NumC: '+ inttostr(NumC) + '    SymbolC: '+ inttostr(SymbolC));

  except
    on E: Exception do
      MessageDlg(E.Message, mtError, [mbOK],0);
  end;
end;

No comments:

Post a Comment