Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. CRC32 computation and error detection

CRC32 computation and error detection

Scheduled Pinned Locked Moved Visual Basic
cryptographyhelp
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Gagan 20
    wrote on last edited by
    #1

    Hi all... I want to create a simple program which takes a file and compute its hash value, also it will check whether the file is corrupt or damaged or not. I googled a lot but I did not get any code for that error detection. If you have any piece of code then suggest me.. Thanks. Gagan

    D L 2 Replies Last reply
    0
    • G Gagan 20

      Hi all... I want to create a simple program which takes a file and compute its hash value, also it will check whether the file is corrupt or damaged or not. I googled a lot but I did not get any code for that error detection. If you have any piece of code then suggest me.. Thanks. Gagan

      D Offline
      D Offline
      DaveAuld
      wrote on last edited by
      #2

      If you generate a hash value when the file is generated. and then re-check the hash at a later date, you will know that the file is still the same and not-corrupted if the hash value doesn't change. Do a search for MD5, SHA1, SHA256 etc.

      Dave GoogleWave: dave.m.auld[at]googlewave.com Who am I?: Web|Facebook|Twitter|LinkedIn|Bebo

      1 Reply Last reply
      0
      • G Gagan 20

        Hi all... I want to create a simple program which takes a file and compute its hash value, also it will check whether the file is corrupt or damaged or not. I googled a lot but I did not get any code for that error detection. If you have any piece of code then suggest me.. Thanks. Gagan

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        Hi, I have some high-performance C# code that reads any file and calculates a CRC32 for it; it is table-driven to get maximum speed. There is an infinite number of definitions for checksums, hashes and the like, however my code returns the same value that is reported by ZIP utilities such as WinZip. If you're inexperienced in bit fiddling, you may have some trouble in translating it to another language, especially VB/VB.NET; not sure the automatic translators such as this one[^] would do a good job here. Interested? :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
        [The QA section does it automatically now, I hope we soon get it on regular forums as well]


        G 1 Reply Last reply
        0
        • L Luc Pattyn

          Hi, I have some high-performance C# code that reads any file and calculates a CRC32 for it; it is table-driven to get maximum speed. There is an infinite number of definitions for checksums, hashes and the like, however my code returns the same value that is reported by ZIP utilities such as WinZip. If you're inexperienced in bit fiddling, you may have some trouble in translating it to another language, especially VB/VB.NET; not sure the automatic translators such as this one[^] would do a good job here. Interested? :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
          [The QA section does it automatically now, I hope we soon get it on regular forums as well]


          G Offline
          G Offline
          Gagan 20
          wrote on last edited by
          #4

          Thanks for your reply,,, Could you gimmie some piece of code in C#. Is it possible to judge whether the file is damaged or not by computed hash value?? Thanks. Gagan

          L 1 Reply Last reply
          0
          • G Gagan 20

            Thanks for your reply,,, Could you gimmie some piece of code in C#. Is it possible to judge whether the file is damaged or not by computed hash value?? Thanks. Gagan

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            to check for "damage" you need to: 1. have a file 2. calculate some kind of checksum 3. wait 4. calculate the checksum again in an identical way 5. if the results of (2) and (4) don't match, you can be sure the file was modified during (3); if they match, that most likely means the file was not modified. FWIW: If, between (2) and (4) the file gets transfered to another machine, so (4) is not running where (2) was running, then you also need to transfer the checksum somehow; someone intending to fiddle with your file could also do so to whatever you choose for transferring the checksum The code I have only calculates the checksum, it does not check anything, nor does it have a GUI. It has two methods, the first is simply reading all the data reusing the same buffer over and over (pseudocode shown), the second is "adding" the data to the ongoing CRC.

            private static uint[] CRCtable={
            0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
            0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
            0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
            0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c};

                        // this method is pseudo-code only!
            	public static uint CalcCRC(string fileName) {
            		uint CRC=0;
                                open the file
            		byte\[\] buf=new byte\[0x10000\];
            		for(;;) {
            			int count=readBinaryDataFromFileReturningCount(file, buf);
            			if (count<=0) break;
            			CRC=updateCRC(CRC,buf,count);
            		}
            		close the file
            		return CRC;
            	}
            
                        // this is the tricky part:
            	private static uint updateCRC(uint CRC, byte\[\] buf, int count) {
            		CRC=~CRC;
            		for (int i=0; i< count; i++) {
            			byte chr=buf\[i\];
            			uint index=(CRC^chr)&0x0F;
            			CRC=(CRC>>4)^CRCtable\[index\];
            			chr>>=4;
            			index=(CRC^chr)&0x0F;
            			CRC=(CRC>>4)^CRCtable\[index\];
            		}
            		return ~CRC;
            	}
            

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


            I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
            [The QA section does it automatically now, I hope we soon get it on regular forums as well]


            modified on Friday, January 22, 2010 12:44 PM

            G 1 Reply Last reply
            0
            • L Luc Pattyn

              to check for "damage" you need to: 1. have a file 2. calculate some kind of checksum 3. wait 4. calculate the checksum again in an identical way 5. if the results of (2) and (4) don't match, you can be sure the file was modified during (3); if they match, that most likely means the file was not modified. FWIW: If, between (2) and (4) the file gets transfered to another machine, so (4) is not running where (2) was running, then you also need to transfer the checksum somehow; someone intending to fiddle with your file could also do so to whatever you choose for transferring the checksum The code I have only calculates the checksum, it does not check anything, nor does it have a GUI. It has two methods, the first is simply reading all the data reusing the same buffer over and over (pseudocode shown), the second is "adding" the data to the ongoing CRC.

              private static uint[] CRCtable={
              0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
              0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
              0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
              0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c};

                          // this method is pseudo-code only!
              	public static uint CalcCRC(string fileName) {
              		uint CRC=0;
                                  open the file
              		byte\[\] buf=new byte\[0x10000\];
              		for(;;) {
              			int count=readBinaryDataFromFileReturningCount(file, buf);
              			if (count<=0) break;
              			CRC=updateCRC(CRC,buf,count);
              		}
              		close the file
              		return CRC;
              	}
              
                          // this is the tricky part:
              	private static uint updateCRC(uint CRC, byte\[\] buf, int count) {
              		CRC=~CRC;
              		for (int i=0; i< count; i++) {
              			byte chr=buf\[i\];
              			uint index=(CRC^chr)&0x0F;
              			CRC=(CRC>>4)^CRCtable\[index\];
              			chr>>=4;
              			index=(CRC^chr)&0x0F;
              			CRC=(CRC>>4)^CRCtable\[index\];
              		}
              		return ~CRC;
              	}
              

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


              I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
              [The QA section does it automatically now, I hope we soon get it on regular forums as well]


              modified on Friday, January 22, 2010 12:44 PM

              G Offline
              G Offline
              Gagan 20
              wrote on last edited by
              #6

              Are you sure a file will have two different hash values after sometime if it is damaged or corrupted??

              L 1 Reply Last reply
              0
              • G Gagan 20

                Are you sure a file will have two different hash values after sometime if it is damaged or corrupted??

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                No. I said "if the results of (2) and (4) don't match, you can be sure the file was modified during (3); if they match, that most likely means the file was not modified." and nothing else. There isn't a checksum, hash or anything in the world that will assure this. The larger the checksum range (i.e. the more bits it uses), the lower the probability of a false match. If you need to be absolutely sure, you need a check that has at least the same number of bits as the [ADDED] optimal loss-less compressed version of [/ADDED] the actual file itself. :)

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
                [The QA section does it automatically now, I hope we soon get it on regular forums as well]


                modified on Friday, January 22, 2010 12:56 PM

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups