Thread: wich hash type?

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    161

    wich hash type?

    Hi to all. I have to control in a loop some files, I have to see if they has been modifyed.

    I think the faster way is to :

    1) read the first file, create an hash
    2) while I've done it for all the files, recreate the hash of the first file and confronte it with the previous hash.
    3) do the same for all the other files.

    I have found a nice md5 help here but md5 is not so fast (crc32 is faster for example)

    I have 2 questions :

    Which hash should I use ? wich one is the faster? (I don't care about security, I know that more faster it is, more faster can I bruteforce it, but it is not a pass validation)

    There are some function in C for crc32 and so on ? Or I have to write them by myself?
    This forum is the best one I've ever seen. Great ppl, great coders

  2. #2
    Registered User
    Join Date
    Apr 2004
    Posts
    210
    The common approach would be to simply compare file modification times. Anyways...

    I don't know if there is a standard function available in Windows but one simple way to do this on your own would be to xor-sum up the file contents as in this pseudocode:

    Code:
      int hash= 0;
     
     read complete or partial file to buffer.
    
     int *buffer= your buffer;
     int *loc= buffer;
     int *end= buffer + size of your buffer in integers;
    
     for (; loc < end; loc++)
       hash^= *loc;
     
     handle the up to three remaining bytes if buffersize % sizeof(int) != 0
     if buffer did not contain all of the file, read next part to buffer, restart...
     
    You could optimize it by using register variables for loc, end and hash and maybe unrolling the loop a bit. That way it should become pretty fast.
    In any case, try to use a standard function if there is one available.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    Quote Originally Posted by Nyda
    The common approach would be to simply compare file modification times.
    Oh great, that's really better and faster than an hash comparison.
    I'll do something like :


    Code:
    #include<stdio.h>
    #include<sys/stat.h>
    
    extern int blat(char *);
    
    int main()
    {
    	int booty;
    	char blah[] = {"c:/yourfilename.txt"};
    
    	booty = blat(blah);
    
    return 0;
    }
    
    extern int blat(char *blat)
    {
    	struct stat buf;
    	stat(blat,&buf);
    	printf("Last modify: %d\n", buf.st_mtime );
    	return (buf.st_mtime );
    
    }
    This forum is the best one I've ever seen. Great ppl, great coders

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  2. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  3. Little Array Difficulty
    By G4B3 in forum C Programming
    Replies: 16
    Last Post: 03-19-2008, 12:59 AM
  4. Not sure on hash table memory allocations
    By Thumper333 in forum C Programming
    Replies: 3
    Last Post: 09-27-2004, 09:00 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM