Hii friends
Well i am working on hex values of a file.Now to test it i delete single byte and then try to write the file using the remaining bytes.Well the file gets corrupted but still the file is constructed.
So is there any code with which i can test if a file is corrupted or not???
I am only talking about resource files.....

Recommended Answers

All 9 Replies

How you define file is corrupt ?

when the binary contents is altered...!!

Only way I would assume to determine if a file is corrupt, is to compare the contents to it's original file content. Just opinion.

Only way I would assume to determine if a file is corrupt, is to compare the contents to it's original file content. Just opinion.

I was thinking about CRC-32 or SHA algorithm........is it possible??

There might be a way to check length of each binary and if one is corrupt, to identify it.
If you still need help with this, post a few lines of original binary content and a few lines of binary contents altered.

I suggest comparing checksums of the original file and the possibly corrupted file. Resulting hash values are easy to store and you can validate corrupted file even without the original file.

.NET has a few suitable hash functions like SHA1 and SHA256 (see System.Security.Cryptography namespace). CRC32 is not provided but you can find code for that from the net with a little googling.


HTH

Can you help me explain more about hash values...???

I Just want to validate file without its original file.....Only make sure that the file is corrupted...!!!

Hash values are calculated with hash functions like SHA1, SHA256, MD5 etc. See hash functions explained.

For example, I have two sets of hex values and calculate SHA1 from them:

00000000h: 44 61 6E 69 77 65 62 20 72 75 6C 65 73 ; Daniweb rules
SHA1: cc014a3fc006c99971f12d21dcc420564479f994

and

00000000h: 44 61 6E 69 77 65 62 20 72 6F 6C 65 73 ; Daniweb roles
SHA1: 84eef0d3c95fd9898d964592024132501e8a4f09

As you can see, 10th hex value is the only difference, but SHA1-values are totally different.

Here's some code for you, taken from one of my many unfinished projects :)

Imports System.Security.Cryptography
Imports System.IO

Private Sub CalculateSHA1(ByVal BytesIn() As Byte, ByRef ResultSHA1() As Byte)
  '
  ' Calculate SHA1 for bytes
  '
  Dim ProvSHA1 As SHA1
  'Const PROCNAME As String = "CalculateSHA1"

  Try
    ProvSHA1 = SHA1CryptoServiceProvider.Create()
    ProvSHA1.Initialize()
    ResultSHA1 = ProvSHA1.ComputeHash(BytesIn)
    ProvSHA1.Clear()
  Catch ex As Exception
    'g_Log.LogError(g_APPNAME, m_MODULE, PROCNAME, ex.Message)
  End Try

End Sub

Private Sub CalculateSHA1(ByVal StreamIn As Stream, ByRef ResultSHA1() As Byte)
  '
  ' Calculate SHA1 for stream
  '
  Dim ProvSHA1 As SHA1
  'Const PROCNAME As String = "CalculateSHA1"

  Try
    ProvSHA1 = SHA1CryptoServiceProvider.Create()
    ProvSHA1.Initialize()
    ResultSHA1 = ProvSHA1.ComputeHash(StreamIn)
    ProvSHA1.Clear()
  Catch ex As Exception
    'g_Log.LogError(g_APPNAME, m_MODULE, PROCNAME, ex.Message)
  End Try

End Sub

Private Function SHA1ToString(ByVal BytesIn() As Byte) As String
  '
  ' Return SHA1 as a hex string
  '
  Dim UBits As Integer
  Dim LBits As Integer
  Dim TempStr As String
  Dim i As Integer
  'Const PROCNAME As String = "SHA1ToString"

  Try
    TempStr = ""
    For i = 0 To BytesIn.GetUpperBound(0)
      UBits = (BytesIn(i) And 240) >> 4
      LBits = (BytesIn(i) And 15)
      TempStr = TempStr & String.Format("{0:x}", UBits)
      TempStr = TempStr & String.Format("{0:x}", LBits)
    Next i
    Return TempStr
  Catch ex As Exception
    'g_Log.LogError(g_APPNAME, m_MODULE, PROCNAME, ex.Message)
    Return ""
  End Try

End Function

Overloaded CalculateSHA1-procedures do the actual calculation and return hash-value as a byte array. SHA1ToString-function simply takes in a byte array and returns a readable hex-string.

HTH

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.