Solved

Checksum of a file ...

Posted on 2009-07-13
9
359 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

747 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

13 Experts available now in Live!

Get 1:1 Help Now