unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Math;
type
TForm1 =
class(TForm)
Label1: TLabel;
Button1: TButton;
Edit1: TEdit;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var KlarText: AnsiString;
a0,b0,c0,d0: LongWord;
K:
Array[0..63]
of LongWord;
Form1: TForm1;
const s:
Array[0..63]
of LongWord = (7,12,17,22, 7,12,17,22, 7,12,17,22, 7,12,17,22,
5,9,14,20, 5,9,14,20, 5,9,14,20, 5,9,14,20,
4,11,16,23, 4,11,16,23, 4,11,16,23, 4,11,16,23,
6,10,15,21, 6,10,15,21, 6,10,15,21, 6,10,15,21);
implementation
{$R *.dfm}
{-----------------------------------------------------------------------------}
function F1(x,y,z:LongWord):LongWord;
begin
Result:= (x
and y)
or ((
not x)
and z);
end;
function F2(x,y,z:LongWord):LongWord;
begin
Result:= (x
and z)
or (y
and (
not z));
end;
function F3(x,y,z:LongWord):LongWord;
begin
Result:= x
xor y
xor z;
end;
function F4(x,y,z:LongWord):LongWord;
begin
Result:= y
xor (x
or (
not z));
end;
procedure PrepareText;
var l,j: Int64;
var i: integer;
begin
l:= Length(KlarText)*8;
KlarText:= Klartext+ Chr(128);
repeat
KlarText:= KlarText+Chr(0);
until Length(KlarText)
mod 64 = 56;
for i:= 0
to 7
do begin
j:= l;
//ShowMessage(IntToStr(j));
j:= j
shl (i*8);
//ShowMessage(IntToStr(j));
j:= j
shr 56;
KlarText:= KlarText+Chr(j);
end;
{for i:=1 to Length(Klartext) do begin
ShowMessage('Buchstabe an Stelle ' + IntToStr(i) + ':' + Klartext[i]);
end;}
end;
procedure HashMD5;
var A,B,C,D,temp,RotWert,FWert: LongWord;
var M:
array[0..15]
of LongWord;
var TempText,Hash: AnsiString;
var parts,i,j,g: integer;
//Anzahl der 512 bit Stücke im KlarText
begin
//Variablen auf Standard setzen:
a0:= StrToInt('
$67452301');
b0:= StrToInt('
$EFCDAB89');
c0:= StrToInt('
$98BADCFE');
d0:= StrToInt('
$10325476');
PrepareText;
parts:= Length(KlarText)
div 64;
for i:=1
to parts
do begin
TempText:= Copy(KlarText, ((i-1)*64+1), 64);
for j:= 0
to 15
do begin
M[j]:= ( ((Ord(TempText[1]))
shl 24)
or ((Ord(TempText[2]))
shl 16)
or
((Ord(TempText[3]))
shl 8)
or (Ord(TempText[4])) );
Delete(TempText, 1, 4);
end;
A:=a0;
B:=b0;
C:=c0;
D:=d0;
for j:=0
to 63
do begin
Case j
of
0..15:
begin
g:= i;
FWert:= F1(B,C,D);
temp:= D;
D:= C;
C:= B;
RotWert:= (((A+FWert+K[i]+M[g])
shl s[i])
or ((A+FWert+K[i]+M[g])
shr (32-s[i])));
B:= B+RotWert;
A:= temp;
end;
16..31:
begin
g:= (5*i +1)
mod 16;
FWert:= F2(B,C,D);
temp:= D;
D:= C;
C:= B;
RotWert:= (((A+FWert+K[i]+M[g])
shl s[i])
or ((A+FWert+K[i]+M[g])
shr (32-s[i])));
B:= B+RotWert;
A:= temp;
end;
32..47:
begin
g:= (3*i +5)
mod 16;
FWert:= F3(B,C,D);
temp:= D;
D:= C;
C:= B;
RotWert:= (((A+FWert+K[i]+M[g])
shl s[i])
or ((A+FWert+K[i]+M[g])
shr (32-s[i])));
B:= B+RotWert;
A:= temp;
end;
48..63:
begin
g:= (7*i)
mod 16;
FWert:= F4(B,C,D);
temp:= D;
D:= C;
C:= B;
RotWert:= (((A+FWert+K[i]+M[g])
shl s[i])
or ((A+FWert+K[i]+M[g])
shr (32-s[i])));
B:= B+RotWert;
A:= temp;
end;
end;
end;
a0:= a0+A;
b0:= b0+B;
c0:= c0+C;
d0:= d0+D;
end;
Hash:= IntToHex(a0,8)+ IntToHex(b0,8)+IntToHex(c0,8)+IntToHex(d0,8);
Form1.Label1.Caption:= Hash;
end;
{-----------------------------------------------------------------------------}
procedure TForm1.Button1Click(Sender: TObject);
begin
KlarText:= Edit1.Text;
HashMD5;
end;
procedure TForm1.FormCreate(Sender: TObject);
var i:integer;
begin
for i:=0
to 63
do begin
K[i]:= floor(abs(sin(i+1))* Power(2,32));
end;
end;
end.