{\r
pascal implementation of MD5\r
\r
-written by Bas Steendijk - steendijk@xs4all.nl\r
+written by Bas Steendijk\r
\r
based on RFC1321 - The MD5 Message-Digest Algorithm\r
\r
{$i uint32.inc}\r
\r
type\r
- dvar=array[0..0] of byte;\r
+ dvar=array[0..65535] of byte;\r
Tmd5state=record\r
buf:array[0..63] of byte;\r
H:array[0..3] of uint32;\r
\r
procedure getmd5(const data;len:longint;var result);\r
\r
-function md5tostr(const md5:tmd5):string;\r
+function md5tostr(const md5:tmd5):ansistring;\r
+\r
+function md5selftest:boolean;\r
\r
implementation\r
\r
-function inttohex(val,bits:integer):string;\r
+function inttohex(val,bits:integer):ansistring;\r
const\r
- hexchar:array[0..15] of char='0123456789abcdef';\r
+ hexchar:array[0..15] of ansichar='0123456789abcdef';\r
begin\r
inttohex := hexchar[val shr 4]+hexchar[val and $f];\r
end;\r
b := state.msglen and 63;\r
\r
inc(state.msglen,len);\r
- while (state.msglen > $20000000) do begin\r
+ while (state.msglen >= $20000000) do begin\r
dec(state.msglen,$20000000);\r
inc(state.msglenhi);\r
end;\r
md5finish(t,result);\r
end;\r
\r
-function md5tostr(const md5:tmd5):string;\r
+function md5tostr(const md5:tmd5):ansistring;\r
var\r
a:integer;\r
- s:string;\r
+ s:ansistring;\r
begin\r
s := '';\r
for a := 0 to 15 do s := s + inttohex(md5[a],2);\r
md5tostr := s;\r
end;\r
\r
+function md5selftest;\r
+const\r
+ teststring:ansistring='The quick brown fox jumps over the lazy dog';\r
+ testresult:array[0..15] of byte=($9e,$10,$7d,$9d,$37,$2b,$b6,$82,$6b,$d8,$1d,$35,$42,$a4,$19,$d6);\r
+var\r
+ h:tmd5;\r
+ a:integer;\r
+begin\r
+ getmd5(teststring[1],length(teststring),h);\r
+ result := true;\r
+ for a := 0 to 15 do if h[a] <> ord(testresult[a]) then result := false;\r
+end;\r
+\r
+\r
end.\r