- 9Dtc=04/O1
- {F4btI7G!zhaUB2s8_35
- z5jB7*6trR125=_30/49+adlA8s
- w}$yV421exdL0-=+9c/8GOn6M3_7h
- bE05nW_Q6/&7-92%
- oO7z3=-ut2+5q*06?$8c
- WAH_yg93=8
- xC?%347aw-_c86l+&F=s}!$2
- HY78ILs%$={FjnS-/rK&+43*z
- f0kS67D3X8odq5wbVhg{=_v2c9CL$
- -_B0c48h}KD9X=$!r%M&75q?v
- I$=3_J7&8Fn45
- {3J?4!z-L9X2nC8NgR=
- 0v6={w-27u1O&l+*neiU%
- F*5D9z{o4!_%
- ?04D7{l-2pV&5=$
- lungimea parolei sa poata fi setata
- sa foloseasca litere mari si mici, cifre si caractere speciale
- la o parola de 6 caractere (or longer) trebuie sa existe o litera mare, o litera mica, doua cifre si doua caractere speciale - aranjate aleator in parola (nu neaparat la inceput !)
That being said, functia de mai jos a corespuns cerintelor. Cateva exemple de parole generate cu ea (intre 10 si 30 de caractere lungime):
Comentariile sunt la tot pasul, nu cred ca are rost sa explic (din nou) cum functioneaza.
- function GeneratePassword(aLength: Integer): String;
- {
- Description:
- Generates a random password.
- Parameters:
- aLength - length of the generated password
- Returns:
- Random passsword
- }
- const
- // the characters we can use - arranged in sets
- CharsArray: array [1 .. 4] of string = ('*$-+?_&=!%{}/',
- '0123456789',
- 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
- 'abcdefghijklmnopqrstuvwxyz');
- var
- lTemp: String;
- lChars: array [1 .. 4] of string; // temporary charset - loaded from the constant array above
- lCharSet: String; // the charsets from which we grab chars
- i, lCharPos, lSet, lPos: Integer;
- function GetEmptyPos(aTemp: String): Integer;
- // searches for a random space character within the parameter string
- var
- lRand, i: Integer;
- begin
- lRand := Random(200) + 1;
- while (lRand > 0) do
- begin
- i := 1;
- while (i <= Length(aTemp)) do
- begin
- if (lTemp[i] = ' ') then
- Dec(lRand);
- if (lRand = 0) then
- Break;
- Inc(i);
- end;
- end;
- Result := i;
- end;
- begin
- Randomize;
- lTemp := EmptyStr;
- // initialize the password to ALL spaces
- // these spaces will be later replaced by actual password chars
- for i := 1 to aLength do
- lTemp := lTemp + ' ';
- // getting the char sets [specials, digits, letters (upper & lower)]
- for i := 1 to 4 do
- lChars[i] := CharsArray[i];
- // this specifies from which charset will the chars be taken
- // note that 1 & 2 are duplicated - because we need 2 chars from each group
- lCharSet := '112234';
- for lCharPos := 1 to aLength do
- begin
- // refill the buffers (if needed) - begin
- for i := 1 to 4 do
- if (lChars[i] = EmptyStr) then
- lChars[i] := CharsArray[i];
- // the initial charsets are A MUST - however, the next ones are random !
- if (lCharSet = EmptyStr) then
- begin
- lPos := Random(6) + 4;
- for i := 1 to lPos do
- lCharSet := lCharSet + Chr(49 + Random(4));
- // chars from '1' to '4' - available charsets
- end;
- // refill the buffers (if needed) - end
- // get a random charset
- lPos := Random(Length(lCharSet)) + 1;
- lSet := StrToInt(lCharSet[lPos]);
- System.Delete(lCharSet, lPos, 1); // remove the charset from the list
- // from the given charset (lSet), get a random char
- lPos := Random(Length(lChars[lSet])) + 1;
- // since we now have the character in lChars[lSet, lPos], we must place it in the password
- // we search for a random space (GetEmptyPos) and place it there
- lTemp[GetEmptyPos(lTemp)] := lChars[lSet, lPos];
- System.Delete(lChars[lSet], lPos, 1); // remove the char from the charset
- // we remove the charsets and chars from the list to avoid duplicates, as much as possible
- // however, when they're empty, we refill them above (see the comment)
- end;
- Result := lTemp;
- end;
Bafta !
Welcome to BitCell. Click here to register !


