-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathuRandomGenerator.pas
More file actions
57 lines (47 loc) · 1.02 KB
/
uRandomGenerator.pas
File metadata and controls
57 lines (47 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
unit uRandomGenerator;
interface
uses
Windows, uHash;
type
TRandomGenerator = class
private
MD5: TMD5;
procedure FeedGenerator;
public
constructor Create;
destructor Destroy; override;
procedure RandomBytes(var Buffer; Length: Integer);
end;
implementation
constructor TRandomGenerator.Create;
begin
MD5 := TMD5.Create;
end;
destructor TRandomGenerator.Destroy;
begin
MD5.Free;
end;
procedure TRandomGenerator.RandomBytes(var Buffer; Length: Integer);
type
TBytes = array[0..MaxInt - 1] of Byte;
var
I: Integer;
X: Integer;
D: TDigest;
begin
for I := 0 to Length - 1 do
begin
FeedGenerator;
D := MD5.Digest;
X := D.Bytes[0] xor MD5.Digest[1] xor MD5.Digest[2] xor MD5.Digest[3];
TBytes(Buffer)[I] := X and 255;
end;
end;
procedure TRandomGenerator.FeedGenerator;
begin
Inc(PCardinal(MD5.Digest.Buffer)^, GetTickCount);
MD5.Update(MD5.Digest.Buffer, MD5.Digest.Length);
end;
initialization
Randomize;
end.