hash function - int to char string + vice versa

This is a discussion on hash function - int to char string + vice versa within the C Programming forums, part of the General Programming Boards category; Hi everyone, So I am using a C model that I inherited from someone else (no documentation unfortunately). There's a ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    29

    hash function - int to char string + vice versa

    Hi everyone,

    So I am using a C model that I inherited from someone else (no documentation unfortunately). There's a C function (well its developed using the OPNET simulator so it's a sort of a C/OPNET hybrid) that takes a value convert its to a hash value (the dht_get_node_id function) and then that hash can be converted to an int value with the sha_get_int_id function.

    Code:
    // A bit more semantically-friendly name for a SHA-1 hash.
    typedef unsigned char* DhtT_Key_Bytes
    
    DhtT_Key_Bytes 
    	dht_get_node_id(int val)
    
    {
          ShaT_Ctx 			context;
          DhtT_Key_Bytes 	key = OPC_NIL;
    
         FIN (dht_get_node_id(IpT_Address ip_addr_ptr));
    			
    
                sha_init(&context);
     
                // allocate room for the SHA-1 Hash
    
                key = (DhtT_Key_Bytes) op_prg_mem_alloc(20);
            
                // update() keeps a "running total" of the SHA-1, but since we're only hashing 32 bits, we only use it once
    
                sha_update(&context, (unsigned char*)&val, sizeof(int));
    
                 // store the final SHA-1 as the DHT ID
                sha_final(key, &context);
     
    			printf ("\nThe value of my node ID is: ");
    			sha_print_char(key);
    Code:
    int sha_get_int_id(unsigned char digest[ShaC_Bytes])
    {
    	int k, runningTotal;
    	char	temp;
    	
    	runningTotal = 0;
    	temp = '2';
    		
    	for (k = 0; k < ShaC_Bytes; k++)
    	{
    		if (digest[k] == '\a')
    		{
    			runningTotal += 7;
    			//printf("%c", temp);			
    		}
    		else if(digest[k] != '\a')
    		{
    			runningTotal += digest[k];
    			//printf("%c", digest[k]);	
    		}
    		
    		if (!((k + 1) % 4))
    			printf(" ");
    	}
    	
    	return runningTotal;	
    }
    So for example, from debugging, if I pass the value 5 into the function I get a hash ID of 221 'Y' and if I pass this to sha_get_int_id it gives me an integer ID of 3144.

    My problem is that I need a way of converting an integer back into the appropriate hash ID e.g. converting 3144 back to 221 'Y'.

    Can anyone give me any advice as to how I can go about this? I suppose what's confusing me is the whole hash/digest thing which I'm not sure I fully follow and whether it's even possible to convert back (because from my limited knowledge maybe that's the whole point that you can't convert back because this is used for encryption).

    Many thanks in advance.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by kerrymaid View Post
    Can anyone give me any advice as to how I can go about this? I suppose what's confusing me is the whole hash/digest thing which I'm not sure I fully follow and whether it's even possible to convert back (because from my limited knowledge maybe that's the whole point that you can't convert back because this is used for encryption).
    No, you can't. However, this is not "because this is used for encryption" -- hashes can also be used to structure and sort data. The reason you cannot convert them back is because they are reductive (usually drastically so), meaning completely different messages could have the same hash. Eg, this produces a hash key from 0-12 for any char string:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int hash (char *str) {
    	int r = 0, i, len = strlen(str);
    	for (i=0;i<len;i++) r += str[i];
    	return r%12;
    }
    
    int main(int argc, char *argv[]) {
    	char eg[2][8] = { "word", "static" };
    	printf("%s = %d\n%s = %d\n",eg[0],hash(eg[0]),eg[1],hash(eg[1]));
    	return 0;
    }
    "word" and "static" have the same key.

    The SHA1 hash is more complicated (to yield 2^64 possible keys, I think, as opposed to just 12). It's purpose is to ensure that a transmitted message has not been corrupted -- it IS NOT an encryption since the message itself cannot be produced only from the hash. In fact, it widely used for this purpose with unencytpred messages (eg, software packages). If the hash of the received message does not match the SHA1 it came with, the message/package was corrupted somewhere. If the corruption was intentional (but the corrupter could not rely on altering the hash associated with the package), you could attempt to make it match, however, this adds a significant complication (in fact, an impossible one unless you understand how the hash was done, and I would guess in the case of the SHA1 nearly impossible anyway, but I dunno the details of it).

    It's used in cryptography for the same purpose (to help ensure the decoded message was not corrupted).
    Last edited by MK27; 05-29-2010 at 07:37 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    29
    Hi MK27,

    Thanks for the reply. I suspected as much (but wasn't sure exactly why) so thanks for the further explanation. I'll have to come up with a work around for what I need to do.

    Cheers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  4. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 11:36 AM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21