Question : Delphi Read huge files into an ansistring chunks

Hi - I  use 4 MD5 routines in a program that work perfectly:
#1 - hashes a file - standard
#2 - hashes a (binary copy of pad file + target file)
#3 - hashes an ansistring (pad + target file read into an ansistring) -
       which is extremely slow* for large files. - I read 32K block buffers
       but then have to convert to an ansistring one char at a time.
       See my code.
#4 - Hash ( Memo.text ) string.

* extremely slow means - takes at least 10 times longer than MD5 standard.
  (cause I don't want to admit it takes a 100 times as long).

If provided the same, in total, resulting strings, #2, #3 & #4 all produce the exact same hash, which is paramount - i.e. extremely impotant. To use my MD5 functions, they must be passed either a file name or an AnsiString.  

In the end, my question is, can I quickly read huge files into directly into ansistring chunks?

TIA - Ed



       
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:
// StrBuf, StrBuf2, StrPad : ansistring
       // bufin2 -> array[1..32768] of AnsiChar
       // StrPad is assigned one of several, user selected,
       //        anti-collision protocols  
          
       repeat

          BlockRead(InFile, Bufin2, SizeOf(Bufin2), NumRead);
          BytsRem := BytsRem - NumRead;
          NBlks   := NBlks + 1;
          StrBuf  := '';          
          if NumRead > 0 then    
             for k := 1 to NumRead do    //** extremely slow for large files           
                 StrBuf := StrBuf+AnsiString(Bufin2[k]);

          if NBlks  = 1 then
             StrBuf2  := AnsiString(StrPad+StrBuf)
           else
             StrBuf2  := AnsiString(StrBuf2+StrBuf);

          MD5Long := MD5Hex(MD5Str(StrBuf2));

       until (NumRead = 0) or (BytsRem = 0); // or (NBlks = 2);

       Closefile(InFile);

       // Completed hash is contained in MD5Long
       // Most importantly, MD5Long agrees exactly with the
       //      binary "copied files" hash - see my #2.

Answer : Delphi Read huge files into an ansistring chunks

function  MD5Str(M: RawByteString): MD5Digest;

You shouldn't have to convert to AnsiString when using this. Do you get a different hash when you don't? You shouldn't.
Random Solutions  
 
programming4us programming4us