Important alert: (current site time 7/16/2013 2:25:23 AM EDT)
 

winzip icon

Grab file MD5 quickly

Email
Submitted on: 1/3/2008 11:39:52 AM
By: Malwarebytes  
Level: Intermediate
User Rating: By 20 Users
Compatibility: VB 6.0
Views: 16536
(About the author)
 
     This code will allow you to read a file extremely fast and grab its MD5. On a Pentium D 3.6 ghz it can read Explorer.exe and hash it 50 times in under a second. Please try it out and submit any comments or questions. Update (1/1/08) - Cut down the speed by over 100% by not converting the buffer into a string value. Cut down the size of the code by removing unnecessary functions.
 

Windows API/Global Declarations:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'**************************************
'Windows API/Global Declarations for :Grab file MD5 quickly
'**************************************
Crypto APIs as well as ReadFile()
winzip iconDownload code

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
  3. Scan the source code with Minnow's Project Scanner

If you don't have a virus scanner, you can get one at many places on the net including:McAfee.com

 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.

Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

8/4/2007 1:27:23 AMAlfred

Thank you; very well done!! Compact, clean code; can't see any optimizaion potential.
(If this comment was disrespectful, please report it.)

 
8/4/2007 1:52:53 AMAlfred

Really good: 50 rounds in 0.86 sec on a P4 3.4 GHz.
Next week I'll receive my new 3GHz Dual Core machine; hope for double speed.

Ehm - two things: 1. For performance measurements i propose to use the
QueryPerformanceCounter / QueryPerformanceFrequency API. 2. For maximum perf. avoid to use use local variables; they will be "built" on every procedure call on the stack.
Instaed use Module level vars.

(If this comment was disrespectful, please report it.)

 
8/4/2007 6:01:41 PMMohammed Sayed

Nice , but how we can make it work for very large files ?
(If this comment was disrespectful, please report it.)

 
8/5/2007 4:04:35 AMEd Wilk

Is there a file size limit the code can handle? I got an "Out of memory error" at InputFile = StrConv(uBuffer, GetvbUnicode), with a 237 Meg file.
(If this comment was disrespectful, please report it.)

 
8/5/2007 3:40:28 PMFrank Maier

Nice code, but it would be great, if the program could read larger files (>Ram).
(If this comment was disrespectful, please report it.)

 
8/6/2007 4:49:06 AMMalwarebytes

Thanks for the feedback. I generally use it to hash files under 5mb, however I do a lot of hashing with them so this code comes in handy.

I would say that after about 200mb, you would run out of string space to utilize and get an out of string space or out of memory error.

I can't really find a solution for this because eventually the full string needs to be passed to the MD5 grabbing function.

Any ideas on this would be great.

(If this comment was disrespectful, please report it.)

 
8/6/2007 7:59:13 AMjasonh

Im lost, what exactly does this do, and why? Thank you.
(If this comment was disrespectful, please report it.)

 
8/6/2007 8:09:30 AMJames Dougherty

To see a sample of how to do it look at code ID 26955
(If this comment was disrespectful, please report it.)

 
8/6/2007 10:57:00 PMEd Wilk

Even with a file size limit this code is really nice and its fast.

I mainly use MD5 for checking anti-viri definition downloads which aren't very big, I've tried all the free MD5 codes, DLLs and Classes I could find for VB6 and they all returned a wrong hash number sooner or later, so far this is working, will post back otherwise. ;)

Thanks for sharing! *****
(If this comment was disrespectful, please report it.)

 
8/9/2007 10:38:56 AMSteve Pickering

Don't blame the coder.
VB6 is shaky as heck when it comes too files over 200MB and if you think thats bad, try it on a file thats 500MB+ and watch the sparks fly lol

(If this comment was disrespectful, please report it.)

 
8/9/2007 6:28:15 PMMohammed Sayed

@ James Dougherty : How can that submission help ?!

@ Steve Pickering : We don't he made an amazing project.
(If this comment was disrespectful, please report it.)

 
8/30/2007 8:03:15 AMTerriTop

Very nice....should come in handy!!

To resove the "out of memory" issue, you might try splitting the string into chuncks and processing these in sequence. Just a thought... TerriTop
(If this comment was disrespectful, please report it.)

 
10/7/2007 4:05:39 AMHossein Moradi

i have optimized your function now it`s 2.5 time faster
just change the declaration of the "CryptHashData" function and add then function
now you can get the MD5 directly from the function by passing the file name

(If this comment was disrespectful, please report it.)

 
10/7/2007 4:07:28 AMHossein Moradi

Dim hFile As Long, uBuffer() As Byte, lFileSize As Long, lBytesRead As Long, uMD5(BUFFER_SIZE) As Byte, lMD5Len As Long, i As Long, hCrypt As Long, hHash As Long

'Get a handle to the file
hFile = CreateFile(sFileName, GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0, OPEN_EXISTING, 0, 0)

'Can't open file
If hFile = -1 Then Exit Function
lFileSize = GetFileSize(hFile, 0)

If lFileSize < 1 Then
CloseHandle hFile
Exit Function
End If

'Prepare the buffer
ReDim uBuffer(lFileSize - 1)

'Read the file
If ReadFile(hFile, uBuffer(0), lFileSize, lBytesRead, ByVal 0) <> 0 Then If lBytesRead <> lFileSize Then ReDim Preserve uBuffer(lBytesRead)

'Close the handle to the file
CloseHandle hFile
(If this comment was disrespectful, please report it.)

 
10/7/2007 4:09:45 AMHossein Moradi

'Acquire the MD5 hash generator
If CryptAcquireContext(hCrypt, vbNullString, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT) <> 0 Then
If CryptCreateHash(hCrypt, HASH_TYPE, 0, 0, hHash) <> 0 Then
If CryptHashData(hHash, uBuffer(0), lFileSize, 0) <> 0 Then
If CryptGetHashParam(hHash, HP_HASHSIZE, uMD5(0), UBound(uMD5) + 1, 0) <> 0 Then
lMD5Len = uMD5(0)

If CryptGetHashParam(hHash, HP_HASHVAL, uMD5(0), UBound(uMD5) + 1, 0) <> 0 Then
For i = 0 To lMD5Len - 1
GetMD5 = GetMD5 & (Right$("0" & Hex$(uMD5(i)), 2))
Next i
End If

End If
End If
End If
End If

'Destroy the MD5 hash generator
CryptDestroyHash hHash
CryptReleaseContext hCrypt, 0
End Function
(If this comment was disrespectful, please report it.)

 
11/9/2007 2:53:29 AMJack

Well, that doesn't seem very fast considering my code is less than a 1/3 the size and hashed explorer.exe 10000 - (YES, Ten Thousand) times in 0.6 (YES, point six) seconds.
(If this comment was disrespectful, please report it.)

 
11/9/2007 2:33:53 PMMalwarebytes

Can you post a link to your code, I am very interested in taking a look at it.
(If this comment was disrespectful, please report it.)

 
7/10/2009 4:51:02 AMNick Bullar

Very nice code. Great find.
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.