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
  • 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?
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB6 Compile Compatibility Issue 4 102
How to set the sa password in a vb6 code for sql connection 9 54
Spell Check in VB6 13 113
vbModal 12 52
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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 Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

777 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