Checksum of a file ...

Posted on 2009-07-13
Last Modified: 2012-05-07
I need to check whether a file is tampered or not by checking its checksum. If the checksums does not match the file is tampered!

I googled and have found plenty of codes but none of them tells me how to compare the original and tampered checksum! They only returns current checksum of the file.

Any help would be appreciated

Thank you

Question by:krisrajz
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
LVL 13

Expert Comment

ID: 24838359
Here is some checksum code... fer ya

Option Explicit
Option Compare Text
'// Then declare this array variable Crc32Table
Private Crc32Table(255) As Long
'// Getting a checksum
'x'Public Sub Main()
'x' Dim lCrc32Value As Long
'x' On Error Resume Next
'x' lCrc32Value = InitCrc32()
'x' lCrc32Value = AddCrc32("This is the original message!", lCrc32Value)
'x' Debug.Print Hex$(GetCrc32(lCrc32Value))
'x'End Sub
Public Function InitCrc32(Optional ByVal Seed As Long = &HEDB88320, Optional ByVal Precondition As Long = &HFFFFFFFF) As Long
'// Declare counter variable iBytes, counter variable iBits, value variables lCrc32 and lTempCrc32
Dim iBytes As Integer, iBits As Integer, lCrc32 As Long, lTempCrc32 As Long
'// Turn on error trapping
On Error Resume Next
'// Iterate 256 times
For iBytes = 0 To 255
'// Initiate lCrc32 to counter variable
lCrc32 = iBytes
'// Now iterate through each bit in counter byte
For iBits = 0 To 7
'// Right shift unsigned long 1 bit
lTempCrc32 = lCrc32 And &HFFFFFFFE
lTempCrc32 = lTempCrc32 \ &H2
lTempCrc32 = lTempCrc32 And &H7FFFFFFF
'// Now check if temporary is less than zero and then mix Crc32 checksum with Seed value
If (lCrc32 And &H1) <> 0 Then
lCrc32 = lTempCrc32 Xor Seed
lCrc32 = lTempCrc32
End If
'// Put Crc32 checksum value in the holding array
Crc32Table(iBytes) = lCrc32
'// After this is done, set function value to the precondition value
InitCrc32 = Precondition
End Function
Public Function AddCrc32(ByVal Item As String, ByVal Crc32 As Long) As Long
'// Declare following variables
Dim bCharValue As Byte, iCounter As Integer, lIndex As Long
Dim lAccValue As Long, lTableValue As Long
'// Turn on error trapping
On Error Resume Next
'// Iterate through the string that is to be checksum-computed
For iCounter = 1 To Len(Item)
'// Get ASCII value for the current character
bCharValue = Asc(Mid$(Item, iCounter, 1))
'// Right shift an Unsigned Long 8 bits
lAccValue = Crc32 And &HFFFFFF00
lAccValue = lAccValue \ &H100
lAccValue = lAccValue And &HFFFFFF
'// Now select the right adding value from the holding table
lIndex = Crc32 And &HFF
lIndex = lIndex Xor bCharValue
lTableValue = Crc32Table(lIndex)
'// Then mix new Crc32 value with previous accumulated Crc32 value
Crc32 = lAccValue Xor lTableValue
'// Set function value the the new Crc32 checksum
AddCrc32 = Crc32
End Function
Public Function GetCrc32(ByVal Crc32 As Long) As Long
'// Turn on error trapping
On Error Resume Next
'// Set function to the current Crc32 value
GetCrc32 = Crc32 Xor &HFFFFFFFF
End Function
'// And for testing the routines above...
Public Sub Main()
Dim lCrc32Value As Long
On Error Resume Next
lCrc32Value = InitCrc32()
lCrc32Value = AddCrc32("This is the original message!", lCrc32Value)
Debug.Print Hex$(GetCrc32(lCrc32Value))
End Sub
Public Function TapCrc(buf As String) As Long
Dim l&, i
l& = 0
For i = 1 To Len(buf)
l& = l& + Asc(Mid(buf, i, 1))
TapCrc = l&
End Function

Open in new window

LVL 20

Expert Comment

ID: 24842119
You are going to have to save the origional checksum in the program somewhere, then compare the current with that. If they are not the same it has been tampered with

Author Comment

ID: 24856121
Application written in VB need to check the checksum of a file. The checksum is not stored in the file. If the old checksum and the new checksum does not match, the file is tampered. How to get the old and the new checksum?
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 13

Expert Comment

ID: 24856245



Author Comment

ID: 24856257
Hi game-mater,
I already have some piece of code to get the checksum of data and file ... all I need is to get the old and the new checksum!

LVL 45

Accepted Solution

aikimark earned 20 total points
ID: 24857599

1. Save lines of checksum and path\filename pairs in a file.
2. When your program starts, read these lines into a dictionary object with the checksum being the data and the path\filename being the key.
3. As you look at the current state of files, use the path\filename to retrieve the old checksum for comparison.
LVL 45

Expert Comment

ID: 24903334
I object to the question deletion requestion.  "ok" is not a valid reason to request a question be deleted.

Moreover, I think the posted comments have answered the question.

Author Closing Comment

ID: 31602745

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question