Solved

Checksum of a file ...

Posted on 2009-07-13
9
360 Views
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

Regards
Raj
0
Comment
Question by:krisrajz
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 13

Expert Comment

by:game-master
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

Else

lCrc32 = lTempCrc32

End If

Next

'// Put Crc32 checksum value in the holding array

Crc32Table(iBytes) = lCrc32

Next

'// 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

Next

'// 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))

Next

TapCrc = l&

End Function

Open in new window

0
 
LVL 20

Expert Comment

by:hes
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
0
 
LVL 1

Author Comment

by:krisrajz
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?
0
 
LVL 13

Expert Comment

by:game-master
ID: 24856245


HAVE U TRIED THE ONE I HAVE POSTED...



game-master
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:krisrajz
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!

Raj
0
 
LVL 45

Accepted Solution

by:
aikimark earned 20 total points
ID: 24857599
@Raj

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.
0
 
LVL 45

Expert Comment

by:aikimark
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.
0
 
LVL 1

Author Closing Comment

by:krisrajz
ID: 31602745
Great
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

863 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now