Delphi - Random Password Generator

Delphi - Random Password Generator

Postby DarkByte » 13 Jul 2011, 10:24

Ieri am avut nevoie, la birou, de o functie care sa genereze o parola random - care sa respecte, totusi, anumite restrictii:
  • 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):
  1. 9Dtc=04/O1
  2. {F4btI7G!zhaUB2s8_35
  3. z5jB7*6trR125=_30/49+adlA8s
  4. w}$yV421exdL0-=+9c/8GOn6M3_7h
  5. bE05nW_Q6/&7-92%
  6. oO7z3=-ut2+5q*06?$8c
  7. WAH_yg93=8
  8. xC?%347aw-_c86l+&F=s}!$2
  9. HY78ILs%$={FjnS-/rK&+43*z
  10. f0kS67D3X8odq5wbVhg{=_v2c9CL$
  11. -_B0c48h}KD9X=$!r%M&75q?v
  12. I$=3_J7&8Fn45
  13. {3J?4!z-L9X2nC8NgR=
  14. 0v6={w-27u1O&l+*neiU%
  15. F*5D9z{o4!_%
  16. ?04D7{l-2pV&5=$


Comentariile sunt la tot pasul, nu cred ca are rost sa explic (din nou) cum functioneaza.
  1. function GeneratePassword(aLength: Integer): String;
  2. {
  3.   Description:
  4.     Generates a random password.
  5.  
  6.   Parameters:
  7.     aLength - length of the generated password
  8.  
  9.   Returns:
  10.     Random passsword
  11. }
  12.  
  13. const
  14.   // the characters we can use - arranged in sets
  15.   CharsArray: array [1 .. 4] of string = ('*$-+?_&=!%{}/',
  16.                                           '0123456789',
  17.                                           'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
  18.                                           'abcdefghijklmnopqrstuvwxyz');
  19.  
  20. var
  21.   lTemp: String;
  22.   lChars: array [1 .. 4] of string; // temporary charset - loaded from the constant array above
  23.   lCharSet: String; // the charsets from which we grab chars
  24.   i, lCharPos, lSet, lPos: Integer;
  25.  
  26.   function GetEmptyPos(aTemp: String): Integer;
  27.   // searches for a random space character within the parameter string
  28.   var
  29.     lRand, i: Integer;
  30.   begin
  31.     lRand := Random(200) + 1;
  32.  
  33.     while (lRand > 0) do
  34.       begin
  35.         i := 1;
  36.         while (i <= Length(aTemp)) do
  37.           begin
  38.             if (lTemp[i] = ' ') then
  39.               Dec(lRand);
  40.             if (lRand = 0) then
  41.               Break;
  42.             Inc(i);
  43.           end;
  44.       end;
  45.  
  46.     Result := i;
  47.   end;
  48.  
  49. begin
  50.   Randomize;
  51.  
  52.   lTemp := EmptyStr;
  53.   // initialize the password to ALL spaces
  54.   // these spaces will be later replaced by actual password chars
  55.   for i := 1 to aLength do
  56.     lTemp := lTemp + ' ';
  57.  
  58.   // getting the char sets [specials, digits, letters (upper & lower)]
  59.   for i := 1 to 4 do
  60.     lChars[i] := CharsArray[i];
  61.  
  62.   // this specifies from which charset will the chars be taken
  63.   // note that 1 & 2 are duplicated - because we need 2 chars from each group
  64.   lCharSet := '112234';
  65.  
  66.   for lCharPos := 1 to aLength do
  67.     begin
  68.       // refill the buffers (if needed) - begin
  69.       for i := 1 to 4 do
  70.         if (lChars[i] = EmptyStr) then
  71.           lChars[i] := CharsArray[i];
  72.  
  73.       // the initial charsets are A MUST - however, the next ones are random !
  74.       if (lCharSet = EmptyStr) then
  75.         begin
  76.           lPos := Random(6) + 4;
  77.           for i := 1 to lPos do
  78.             lCharSet := lCharSet + Chr(49 + Random(4));
  79.                                    // chars from '1' to '4' - available charsets
  80.         end;
  81.       // refill the buffers (if needed) - end
  82.  
  83.       // get a random charset
  84.       lPos := Random(Length(lCharSet)) + 1;
  85.       lSet := StrToInt(lCharSet[lPos]);
  86.       System.Delete(lCharSet, lPos, 1); // remove the charset from the list
  87.  
  88.       // from the given charset (lSet), get a random char
  89.       lPos := Random(Length(lChars[lSet])) + 1;
  90.  
  91.       // since we now have the character in lChars[lSet, lPos], we must place it in the password
  92.       // we search for a random space (GetEmptyPos) and place it there
  93.       lTemp[GetEmptyPos(lTemp)] := lChars[lSet, lPos];
  94.  
  95.       System.Delete(lChars[lSet], lPos, 1); // remove the char from the charset
  96.  
  97.       // we remove the charsets and chars from the list to avoid duplicates, as much as possible
  98.       // however, when they're empty, we refill them above (see the comment)
  99.     end;
  100.  
  101.   Result := lTemp;
  102. end;


Bafta !
3p / 1 votes
User avatar
DarkByte
11011011
 
Joined: 29 Dec 2009
Status: 140

Re: Delphi - Random Password Generator

Postby Payne » 13 Jul 2011, 15:54

Ar fi frumos daca din cele 4 seturi sa poti alege cele folosite.

Adica, de exemplu sa iti genereze o parola doar cu numere si litere mari sau o parola doar cu litere mari si mici.

Despre functie nu pot sa iti zic nimic deoare limbajul cel mai asemanator pascal-ului pe care il cunosc e C/AL.
0,0p / 0 votes
Suit up!

Image
User avatar
Payne
Byte
 
Joined: 04 Jan 2010
Location: 0x7C00
Status: 17

Re: Delphi - Random Password Generator

Postby DarkByte » 13 Jul 2011, 16:10

Payne wrote:Ar fi frumos daca din cele 4 seturi sa poti alege cele folosite.

Adica, de exemplu sa iti genereze o parola doar cu numere si litere mari sau o parola doar cu litere mari si mici.

Da, stiu ce spui, dar parolele care trebuia sa le generez au exact cerintele astea :D

O sa revin zilele astea cu o varianta imbunatatita, merci de sugestie ;)

Bafta
0,0p / 0 votes
User avatar
DarkByte
11011011
 
Joined: 29 Dec 2009
Status: 140


Return to Snippets

Who is online

Users browsing this forum: No registered users and 0 guests

cron