Jump to content

Looking for a VBS script to generate MD5 of a file


Recommended Posts

Does anyone know about any script which can generate MD5 of a file? I've searched in the Internet but haven't been able to find anything working. I did find this:

http://scriptbox.toll.at/index.php?showcontent=Calculate%20MD5%20Hash.vbs

and everything seems to work fine except for the fact that the hash generated by it is wrong :ph34r: (doesn't match the real MD5).

Basically speaking, I'm looking for a solution to calculate MD5 in Windows environment without relying on any 3rd party tools.

Edited by tomasz86
Link to comment
Share on other sites


I've been using FCIV and will probably continue to use it if I can't find any other solution. I'd like to use a VBS script instead of it since such a script can be created on the fly in Windows while FCIV has to be downloaded separately.

Link to comment
Share on other sites

Hum, this code might be a good start.

I've found that one in many different places. The problem is that it only works for text strings, not files... and I'm a complete zero when it comes to VBS so I've got no idea how to change / utilise it :(

Link to comment
Share on other sites

I've been using FCIV and will probably continue to use it if I can't find any other solution. I'd like to use a VBS script instead of it since such a script can be created on the fly in Windows while FCIV has to be downloaded separately.

Well, I don't see any issue in using a FREE (freely redistributable) MD5 calculator and "embed" it in a .script (i.e. creating it on the fly or more simply including it in the "main" download .zip or whatever). :unsure:

Point might be to find a no-frill one (tiny).

This one is a good candidate:

http://www.pc-tools.net/win32/md5sums/

The DSFOK dsfo.exe is MUCH smaller but unfortunately it is not redistributable "by itself".

Another point of interest would be WHY choosing MD5. (if the idea is a check for consistency MD5 might not be needed and a simpler hash like CRC32 might do)

jaclaz

Edited by jaclaz
Link to comment
Share on other sites

Well, I don't see any issue in using a FREE (freely redistributable) MD5 calculator and "embed" it in a .script (i.e. creating it on the fly or more simply including it in the "main" download .zip or whatever). :unsure:

Point might be to find a no-frill one (tiny).

Since I've already done MD5, SHA-1, and CRC-32 (and could rig more through the Windows API), would it be fruitful to cook up a quick exec for this (command-line and free to distribute of course)? Or at least post the proper API calls (i.e. source example) to get the MD5 out of them (assuming those can be done in VBScript - i.e. the source can be adapted)? If so, how should the MD5 be reported?

Link to comment
Share on other sites

Well, I don't see any issue in using a FREE (freely redistributable) MD5 calculator and "embed" it in a .script (i.e. creating it on the fly or more simply including it in the "main" download .zip or whatever). :unsure:

How could such an utility be created on the fly?

Another point of interest would be WHY choosing MD5. (if the idea is a check for consistency MD5 might not be needed and a simpler hash like CRC32 might do)

Unfortunately MD5 is a must since I need it mainly (but not only) for creating update.ver files for M$ hotfix installer which requires MD5 hash to verify files.

Link to comment
Share on other sites

I've found that one in many different places. The problem is that it only works for text strings, not files... and I'm a complete zero when it comes to VBS so I've got no idea how to change / utilise it :(

Me too, but I will tell you from experience in implementing it in other things (and reading that VBScript) that it will work as long as you present the entire data at once to the function. The problem is you really can't do that, so what you have to do is write multiple functions to process a limited buffer based on current state data that you save outside of the function and then finishes up the MD5 once you hit the end of the data. It would be a change of moderate complexity to someone who knows VBScript (I'm sure), but the problem for me is while I have done this in another language, my knowledge of VBScript is zero, so I couldn't say I could write the change there.

Link to comment
Share on other sites

I've tested the script which I mentioned in #1 a little bit more. It's very strange... There seems to be a bug in it somewhere because it does work for some files and doesn't work for others.

For example,

1. Empty file 1.txt has MD5 of d41d8cd98f00b204e9800998ecf8427e. The script is able to generate it correctly.

2. Now I add one line "abc" to the file. The calculated MD5 is now wrong.

3. I add a single space after abc ("abc "). Now the calculated MD5 is correct again... :blink:

Link to comment
Share on other sites

I know it's another external file, though it is small (only 49,152 bytes), but have you tried using md5sum.exe (Freeware) available here and here, and probably other places, too? [Open up a command window and type md5sum --help for command syntax info]

I also have another very small version of this, only about 2kb, but I can't find a link for it right now. I'd be glad to share it if you like, but I think I remember that it has a problem running on Win2K so didn't think it would be your first choice. Let me know if you want to check it out.

Cheers and Regards

Edited by bphlpt
Link to comment
Share on other sites

I know it's another external file, though it is small (only 49,152 bytes),

You must be joking, I pointed to a 28 Kb file which I thought being "huge" :ph34r: .

The mentioned dsfo.exe is around 6 Kb.

The 2 Kb sized one seems like the "right" thing, it would be nice if you could find it, maybe there is a version suitable fro Windows 2000.

jaclaz

Link to comment
Share on other sites

1. Empty file 1.txt has MD5 of d41d8cd98f00b204e9800998ecf8427e. The script is able to generate it correctly.

2. Now I add one line "abc" to the file. The calculated MD5 is now wrong.

3. I add a single space after abc ("abc "). Now the calculated MD5 is correct again... :blink:

This is how testing hash functions usually goes. You have to pick out a number of cases of files of different sizes and then test test and retest (and eliminate all potential of garbage, too). From looking at the script in #1 it seems like the way it's written it only works right if files are presented to it that are in multiples of 4 bytes. The MD5Decode function sticks out in this regard.

Link to comment
Share on other sites

The 2 Kb sized one seems like the "right" thing, it would be nice if you could find it, maybe there is a version suitable fro Windows 2000.

Probably ASM that calls the Windows API functions that will obtain a MD5 hash. But with storage and memory what it is, why quibble over a handful of bytes? Speaking of which, if you can do those in VBScript that's probably the best course.

Link to comment
Share on other sites

FWIW this is what the API calls look like when you use Delphi. Hope they can be adapted to VBScript.


function MD5_File(infile_name: string): string;
// Create digest of file with given Name
const
bufsize = 65536;
var
infile: THandle;
inbuffer: pointer;
amount_read: longint;
cbHashDataLen: Integer;
Digest: TMD5Digest;
hProv: TCryptProv;
hHash: TCryptHash;
begin
GetMem(inbuffer, bufsize);
infile := CreateFile(PChar(infile_name), GENERIC_READ, FILE_SHARE_READ, nil,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL or FILE_FLAG_SEQUENTIAL_SCAN, 0);
hHash := nil;
hProv := 0;
CryptAcquireContext(hProv, nil, nil, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
CryptCreateHash(hProv, CALG_MD5, nil, 0, hHash);
try
ReadFile(infile, inbuffer^, bufsize, amount_read, nil);
repeat
CryptHashData(hHash, inbuffer, amount_read, 0);
ReadFile(infile, inbuffer^, bufsize, amount_read, nil);
until amount_read = 0;
cbHashDataLen := 16;
CryptGetHashParam(hHash, HP_HASHVAL, @Digest, cbHashDataLen, 0);
finally
CryptDestroyHash(hhash);
CryptReleaseContext(hprov, 0);
end;
CloseHandle(infile);
Result := write_hash(@Digest, cbHashDataLen);
FreeMem(inbuffer);
end;

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...