Author: Mike Shkolnik
How can I generate the random password in own program?
Answer:
Solve 1:
In last holidays I wrote a small dialog for random password generation. It's a
simple but results is very useful:))
Try it:
1 2 function TfrmPWGenerate.btnGenerateClick(Sender: TObject): string;
3 4 {max length of generated password}5 const6 intMAX_PW_LEN = 10;
7 var8 i: Byte;
9 s: string;
10 begin11 {if you want to use the 'A..Z' characters}12 if cbAZ.Checked then13 s := 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
14 else15 s := '';
16 17 {if you want to use the 'a..z' characters}18 if cbAZSmall.Checked then19 s := s + 'abcdefghijklmnopqrstuvwxyz';
20 21 {if you want to use the '0..9' characters}22 if cb09.Checked then23 s := s + '0123456789';
24 if s = '' then25 exit;
26 27 Result := '';
28 for i := 0 to intMAX_PW_LEN - 1 do29 Result := Result + s[Random(Length(s) - 1) + 1];
30 end;
31 32 initialization33 Randomize;
The sample results:
IBbfA1mVK2
tmuXIuQJV5
oNEY1cF6xB
flIUhfdIui
mxaK71dJaq
B0YTqxdaLh
...
I think that it's no bad:)) Of course, you can add the some additional crypt
methods and check of unique.
Solve 2:
f
34 unction GenPassWord(): string;
35 var36 nCounter: integer;
37 cString: string;
38 cNumber: integer;
39 begin40 Randomize;
41 cString := '';
42 // nCounter = password length43 for nCounter := 0 to 8 do44 begin45 repeat46 cNumber := Random(122);
47 // for capitol chrs extend the range48 until (nNumber >= 97) and (nNumber <= 122);
49 cString := cString + Chr(nNumber);
50 end;
51 Result := cString;
52 end;