SRL Archive - Other Scripts

Toot MD5 Checksum-er


Tootoot222

2009-02-17 08:16:35

This will check the MD5 checksum of whatever file(s) you put into Path (an array), it can also check the checksum against what you put into CheckAgainst (also an array)

this is quite useful for checking if your HTTP or .torrent downloads have been successfully downloaded (if the MD5 is provided)

program TootMD5ChecksumChecker;

var
X, I, L: Integer;
S, O: String;
Path, Sum, CheckAgainst: TStringArray;
Same: array of Boolean;

begin
Path:= ['C:\Windows\Explorer.exe', //to demonstrate a file being checked against a checksum
'C:\Windows\Explorer.exe', //to demonstrate a file not being check agains anything (a blank checksum) (it will just give the checksum out)
'C:\Windows\Explorer.exe', //to demonstrate a checksum that is not the same
'C:\Windows\Explorer.exe']; //to demonstrate a 4th value (while CheckAgainst only contains 3)
CheckAgainst:= ['4f554999d7d5f05daaebba7b5ba1089d', //Windows Vista explorer.exe =p (change this to whatever your checksum is if it's different on another OS to see how it works with good checksums)
'', //to demonstrate a blank checksum
'BLAHBLAHTHISISNOTAREALCHECKSUMHA'];
L:= Length(Path); //DO NOT CHANGE THIS LINE! get the length of the files to be read
SetLength(CheckAgainst, L); //DO NOT CHANGE THIS LINE! set all the lengths of the arrays to the lengths of the files (if theres more checksum's then files, it will cut off the extra bits, if there's less it'll fill in the missing parts with '' (an empty string))
SetLength(Same, L); //DO NOT CHANGE THIS LINE! see above
SetLength(Sum, L); //DO NOT CHANGE THIS LINE! see above
ClearDebug;
Writeln('Full report:');
for I:= 0 to L - 1 do
begin
Path[I]:= Trim(Path[I]); //takes extra spaces off the end of strings
CheckAgainst[I]:= Trim(CheckAgainst[I]);
Same[I]:= False; //this is because i'm paranoied that it will not start as False (never happened to me before)
try //start of try block
X:= OpenFile(Path[I], False); //open up the file for reading
if(X >= 0) then //if the file was good (it opened right and it actually exists)
begin
Writeln(' File ' + IntToStr(I + 1) + ': ' + Path[I]);
Writeln(' Reading file contents of file...');
ReadFileString(X, S, FileSize(X)); //read all the data from the file
Writeln(' Getting MD5 checksum...');
Sum[I]:= MD5(S); //get the MD5 checksum
if(not(CheckAgainst[I] = '')) then //if it wasn't left blank
begin
Same[I]:= (Sum[I] = CheckAgainst[I]); //compare the sums
if(Same[I]) then
Writeln(' ' + Sum[I] + ' is good;') //checksum was the same
else
Writeln(' ' + Sum[I] + ' is NOT good;') //checksum was not the same
end else
Writeln(' The checksum is: ' + Sum[I] + ';'); //checksum was blank
end else
Writeln(' Unable to open file ' + IntToStr(I + 1) + ': ' + Path[I]); //unable to open the file / it doesn't exist
except
Writeln('Error ' + ExceptionToString(ExceptionType, ExceptionParam) + ' has occured'); //ExceptionToString FTW (returns the last occured error)
finally
if(X >= 0) then //can't close an un-opened file =p
CloseFile(X);
end;
Writeln(' End file ' + IntToStr(I + 1) + ';');
end;
Writeln('End full report;');
Writeln('');
Writeln('Quick report:'); //give a quick report of all files "scanned"
for I:= 0 to High(Path) do
begin
O:= ' File ' + IntToStr(I + 1) + ': ' + Path[I] + ' - ' + Sum[I] + ' - ';
if(not(Same[I])) then //checksum's are not the same
if(not(CheckAgainst[I] = '')) then //if it wasn't blank
O:= O + 'Bad: Checked against ' + CheckAgainst[I] + ';'
else
O:= O + 'Nothing to check;' //it was blank
else
O:= O + 'Good;'; //they were the same
Writeln(O);
end;
Writeln('End quick report;');
end.

Da 0wner

2009-02-17 23:41:10

Pretty easy ;p. But nice.