Funcao - gerar senha randomicamente

Top  Previous  Next

// funcao para gerar senhas randomicamente

 

function GerarSenhaRandom(const Tamanho: Integer; Numeros: Boolean = True; Maiusculas: Boolean = True; Minusculas: Boolean = True): string;

var

  Num, Mai, Min: Integer;

begin

  Result := '';

  Randomize;

  if (not Numeros) and (not Maiusculas) and (not Minusculas) then Exit;  // para não travar

  while Length(Result) < Tamanho do

  begin

    Num := Trunc(Random(10));      // Número randomico

    Mai := Trunc(Random(26)) + 65// 65 -  90

    Min := Trunc(Random(26)) + 97// 97 - 122

    case Trunc(Random(3)+1of

      1if Numeros    then Result := Result + IntToStr(Num) else Continue;

      2if Minusculas then Result := Result + Chr(Min)      else Continue;

      3if Maiusculas then Result := Result + Chr(Mai)      else Continue;

    end;

  end;

end;