Then you can use the functions inside to realize MD5 encryption of Md5string and Md5File.
// tabs = 2
// -
//
//MD5 message digest for Delphi 4
//
// Delphi 4 unit implementation
//MD5 message digest algorithm of RSA data security company
//
//Implementation of Ronald L. Rivest RFC 132 1
//
//Copyright? 1997- 1999 Medienagentur Fichtner & amp; Meyer
//Author Matthias Fichtner
//
// -
See RFC 132 1 for the copyright and license statement of RSA Data Security!
// -
//
/14-jun-97mf implements MD5 according to RFC1321RFC1321
// 16-Jun-97 mf compilation unit (no source code) RFC 132 1
// 28-Feb-99 mf added the MD5Match function RFC 132 1
// 13-Sep-99 mf Rework the whole unit RFC 132 1
// 17-Sep-99 mf rework "test driver" project RFC 132 1
///The source code of KLOC-0/9-SEP-99MF MD5 unit and "Test Driver" project is issued RFC 132 1
//
// -
//The latest version of md5.pas can be obtained from the following website.
//The distribution site is located at:
// -
//This code is provided "as is" without express or
//Any kind of implied warranty. Use at your own risk.
// -
Unit md5
// -
connect
// -
use
Windows
type
MD5Count = array[0..DWORD's1];
DWORD of MD5State = array[0..3]
Md5block = array [0 .. of dword15];
MD5CBits = array[0..7] bytes;
MD5Digest = array[0.. 15] bytes;
Md5buffer = byte of array [0 .. 63];
MD5Context = record
Status: MD5State
Count: MD5Count
Buffer: MD5Buffer
End;
Process md5init (varcontext: md5context);
Process md5context (varcontext: md5context; Input: pChar length: long word);
Process md5final (varcontext: md5context; var Digest:MD 5 Digest);
Function md5string (m: string): md5digest;
Function md5file (n: string): md5digest;
Function md5print (d: md5digest): string;
Function MD5Match(D 1, d2: md5digest): boolean;
// -
fulfil
// -
defined variable
PADDING: MD5Buffer =(
$80, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $00, $00, $00
);
Function F(x, y, z: dword): dword;
begin
Results: = (x and y) or ((non-x and z));
End;
Function G(x, y, z: dword): dword;
begin
Results: = (x and z) or (y and (not z));
End;
Function H(x, y, z: dword): dword;
begin
Results: = x xor y xor z;;
End;
Function I(x, y, z: dword): dword;
begin
Results: = y xor (x or (not z)););
End;
Process rot (var x: dword; N: bytes);
begin
X := (x shl n) or (xshr (32-n));
End;
Process FF (variable a: dword; b,c,d,x:DWORD; S: bytes; AC:DWORD);
begin
inc(a,F(b,c,d)+x+AC);
rot(a,s);
Inc (a and b);
End;
Process GG (variable a: dword; b,c,d,x:DWORD; S: bytes; AC:DWORD);
begin
inc(a,G(b,c,d)+x+AC);
rot(a,s);
Inc (a and b);
End;
Procedure HH (variable a: dword; b,c,d,x:DWORD; S: bytes; AC:DWORD);
begin
inc(a,H(b,c,d)+x+AC);
rot(a,s);
Inc (a and b);
End;
Process II (variable a: dword; b,c,d,x:DWORD; S: bytes; AC:DWORD);
begin
inc(a,I(b,c,d)+x+AC);
rot(a,s);
Inc (a and b);
End;
// -
//Encode the count bytes of the source location into (Count/4) double words of the target location.
Process coding (source, target: pointer; Count: long word);
defined variable
s:PByte;
t:PD word;
I:long word;
begin
s:= Source;
T := target;
For I := 1, the count div 4 starts.
t^:= s^;
Company;
t^:= t^ or(s^ SHL 8);
Company;
T^ := T^ or (s SHL16);
Company;
t^:= t^ or(s^ SHL 24);
Company;
Inc(T);
End;
End;
//Decodes the count doubleword at the source into (Count * 4) bytes at the target.
Process decoding (source, target: pointer; Count: long word);
defined variable
s:PD word;
t:PByte;
I:long word;
begin
s:= Source;
T := target;
For I := 1
T^ := S^ and $ ff
Inc(T);
T^ := (S^ shr 8) and $ ff
Inc(T);
T: = (s SHR16) and $ ff
Inc(T);
T^ := (S^ shr 24) and $ ff
Inc(T);
Company;
End;
End;
//According to the first 64 bytes of the buffer, switch the state.
Process conversion (buffer: pointer; Var status: md5state);
defined variable
a、b、c、d:DWORD;
block:MD 5 block;
begin
Encode(Buffer,@Block,64);
a:= State[0];
b:= State[ 1];
c:= State[2];
d:= State[3];
FF (a,b,c,d,Block[ 0],7,$ d 76 aa 478);
FF (d,a,b,c,Block[ 1], 12,$ E8 c7b 756);
FF (c,d,a,b,Block[ 2], 17,$ 242070 db);
FF (b,c,d,a,Block[ 3],22,$ c 1 BDC eee);
FF (a,b,c,d,Block[ 4],7,$ f 57 c 0 faf);
FF (d,a,b,c,Block[ 5], 12,$ 4787 c62a);
FF (c,d,a,b,Block[ 6], 17,$ a 83046 13);
FF (b,c,d,a,Block[ 7],22,$ FD 46950 1);
FF (a,b,c,d,Block[ 8],7,$ 698098 D8);
FF (d,a,b,c,Block[ 9], 12,$8b 44 f 7 af);
FF (c,d,a,b,Block[ 10], 17,$ ffff 5b b 1);
FF (b,c,d,a,Block[ 1 1],22,$ 895 CD 7 be);
FF (a,b,c,d,Block[ 12],7,$ 6b 90 1 122);
FF (d,a,b,c,Block[ 13], 12,$ FD 987 193);
FF (c,d,a,b,Block[ 14], 17,$ a 679438 e);
FF (b,c,d,a,Block[ 15],22,$ 49b 4082 1);
GG (a,b,c,d,Block[ 1],5,$ f 6 1e 2562);
GG (d,a,b,c,Block[ 6],9,$ c 040 b 340);
GG (c,d,a,b,Block[ 1 1], 14,$ 265 e5a 5 1);
GG (b,c,d,a,Block[ 0],20,$ e 9 b 6 c 7 aa);
GG (a,b,c,d,Block[ 5],5,$ d62f 105d);
GG (d,a,b,c,Block[ 10],9,$ 244 1453);
GG (c,d,a,b,Block[ 15], 14,$ d8a 1e 68 1);
GG (b,c,d,a,Block[ 4],20,$ e 7d 3 FBC 8);
GG (a,b,c,d,Block[ 9],5,$ 2 1e 1 CDE 6);
GG (d,a,b,c,Block[ 14],9,$ c 33707d 6);
GG (c,d,a,b,Block[ 3], 14,$ f 4d 50d 87);
GG (b,c,d,a,Block[ 8],20,$ 455 a 14ed);
GG (a,b,c,d,Block[ 13],5,$ a9e 3 e 905);
GG (d,a,b,c,Block[ 2],9,$ fcefa 3 f 8);
GG (c,d,a,b,Block[ 7], 14,$ 676 f02d 9);
GG (b,c,d,a,Block[ 12],20,$ 8d 2 a4 c 8 a);
HH (a,b,c,d,Block[ 5],4,$ fffa 3942);
HH (d,a,b,c,Block[ 8], 1 1,$ 877 1f 68 1);
HH (c,d,a,b,Block[ 1 1], 16,$ 6d9d 6 122);
HH (b,c,d,a,Block[ 14],23,$ FDE 5380 c);
HH (a,b,c,d,Block[ 1],4,$ a4 beea 44);
HH (d,a,b,c,Block[ 4], 1 1,$ 4 bdecfa 9);
HH (c,d,a,b,Block[ 7], 16,$ f 6 bb 4b 60);
HH (b,c,d,a,Block[ 10],23,$ befbc 70);
HH (a,b,c,d,Block[ 13],4,$ 289 b7ec 6);
HH (d,a,b,c,Block[ 0], 1 1,$ EAA 127 fa);
HH (c,d,a,b,Block[ 3], 16,$ d4ef 3085);
HH (b,c,d,a,Block[ 6],23,$ 488 1d 05);
HH (a,b,c,d,Block[ 9],4,$ d9d 4d 039);
HH (d,a,b,c,Block[ 12], 1 1,$ e 6 db 99 e 5);
HH (c,d,a,b,Block[ 15], 16,$ 1fa 27 cf 8);
HH (b,c,d,a,Block[ 2],23,$ c4ac 5665);
II (a,b,c,d,Block[ 0],6,$ f 4292244);
II (d,a,b,c,Block[ 7], 10,$ 432 aff 97);
2 (c, d, a, b, Block[ 14], 15, $ ab9423a7);
II (b,c,d,a,Block[ 5],2 1,$ fc93a 039);
II (a,b,c,d,Block[ 12],6,$ 655 b 59 c 3);
II (d,a,b,c,Block[ 3], 10,$8f 0 CCC 92);
II (c,d,a,b,Block[ 10], 15,$ ffeff 47d);
II (b,c,d,a,Block[ 1],2 1,$ 85845 DD 1);
II (a,b,c,d,Block[ 8],6,$ 6fa 87 e 4 f);
II (d,a,b,c,Block[ 15], 10,$ Fe 2 ce 6 e 0);
2 (c, d, a, b, Block[ 6], 15, $ a3014314);
II (b,c,d,a,Block[ 13],2 1,$4e 08 1 1a 1);
II (a,b,c,d,Block[ 4],6,$ f 7537 e 82);
II (d,a,b,c,Block[ 1 1], 10,$ bd3af 235);
II (c,d,a,b,Block[ 2], 15,$2 ad 7d 2 bb);
II (b,c,d,a,Block[ 9],2 1,$ eb86d 39 1);
inc(State[0],a);
inc(State[ 1],b);
inc(State[2],c);
inc(State[3],d);
End;
// -
//Initializes the given context
Process md5init (varcontext: md5context);
begin
Start from the context
state[0]:= $ 6745230 1;
state[ 1]:= $ EFC dab 89;
state[2]:= $ 98 bad cfe;
Status [3]: = $10325476;
count[0]:= 0;
count[ 1]:= 0;
ZeroMemory(@Buffer,SizeOf(MD 5 Buffer));
End;
End;
//Update the given context to include the entered length bytes.
Process md5context (varcontext: md5context; Input: pChar length: long word);
defined variable
Index: longword
PartLen:long word;
I:long word;
begin
Start from the context
Index := (Count[0] shr 3) and $ 3f.
Inc (count [0], length SHL 3);
If count [0] <; (length shl 3) and then inc (count [1]);
Inc(Count[ 1], length SHR29);
End;
PartLen:= 64-Index;
If the length & gt= PartLen and then start.
CopyMemory(@Context。 Buffer[Index],Input,PartLen);
Transform (@Context. Buffer, context. Status);
I:= part len;
And I+63 <; Length start
Transform(@Input[I], context. Status);
inc(I,64);
End;
Index: = 0;
end else I:= 0;
CopyMemory(@Context。 Buffer[Index],@Input[I],Length-I);
End;
//Complete the given context, create a summary and zero the context.
Process md5final (varcontext: md5context; var Digest:MD 5 Digest);
defined variable
Bit: MD5CBits
Index: longword
pad len:long word;
begin
Decode (@Context. Count,@Bits,2);
Index: = (Context. Count[0] shr 3) and $ 3f
If Index & lt56 then padlen: = 56-index else padlen: =120-index;
MD5Update(Context,@PADDING,PadLen);
MD5Update (context, @Bits, 8);
Decode (@Context. State, @Digest, 4);
ZeroMemory(@Context,SizeOf(MD 5 Context));
End;
// -
//Create a digest of the given message.
Function md5string (m: string): md5digest;
defined variable
Context: MD5Context
begin
MD5Init (context);
MD5Update(Context,pChar(M),length(M));
MD5Final (context, result);
End;
//Create a file summary with the given name.
Function md5file (n: string): md5digest;
defined variable
file handle:THandle;
map handle:THandle;
ViewPointer: pointer;
Context: MD5Context
begin
MD5Init (context);
FileHandle := CreateFile(pChar(N), GENERIC_READ, FILE_SHARE_READ or FILE_SHARE_WRITE,
Nile, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL or FILE_FLAG_SEQUENTIAL_SCAN, 0);
Invalid handle value for iffilehandle < >, and then try.
map handle:= create file mapping(file handle,nil,PAGE_READONLY,0,0,nil);
If MapHandle & lt& gt0 then tries.
view pointer:= MapViewOfFile(MAP handle,FILE_MAP_READ,0,0,0);
If ViewPointer & lt& gt doesn't, try it.
MD5Update(Context,ViewPointer,GetFileSize(FileHandle,nil));
finally
UnmapViewOfFile(view pointer);
End;
finally
close handle(map handle);
End;
finally
close handle(file handle);
End;
MD5Final (context, result);
End;
//Creates a hexadecimal representation of the given summary.
Function md5print (d: md5digest): string;
defined variable
I: bytes;
constant
Number: array [0.. 15] of char =
(' 0 ',' 1 ',' 2 ',' 3 ',' 4 ',' 5 ',' 6 ',' 7 ',' 8 ',' 9 ',' a ',' b ',' c ',' d ',' e ',' f ');
begin
Result: = "";
For I := 0 to15 do result: = result+digits [(d [i] shr4) and $ 0f]+digits [d [i] and $ 0f];
End;
// -
//Compare two abstracts
Function MD5Match(D 1, d2: md5digest): boolean;
defined variable
I: bytes;
begin
I:= 0;
Result: = true;
While the results and (I< Let's get started.
Results: = d1[i] = d2 [i];
Inc (1);
End;
End;
End.