Thread: Hash function - Object ID/IP address to 2D co-ordinates

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

    Hash function - Object ID/IP address to 2D co-ordinates

    Hi,

    I'm trying to reverse engineer and extend code that I have inherited from another postgrad student (now gone).

    Basically he extended a SHA-1 library (see attached) so that an IP address/object id could be converted into a hashed 40 digit hex value (DhtT_Key_Bytes) (and provided a function to easily convert that to an int value). The functions in the library are called as per the below:

    Code:
    ShaT_Ctx 			context;
    
     my_addr = ip_support_address_from_node_id_get(my_node_objid);
    ip_address_print (str, my_addr);			
    printf ("\nThe value of my IP address is : %s", str);
    						
    InetT_my_addr = inet_address_from_ipv4_address_create (ip_addr_ptr);
    	  
    val = ip_addr_ptr;
    
    // initialize the SHA-1 Hash context
    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);
     
    sha_print_char(key);
    			
    	intnodeID = sha_get_int_id(key);
    I need to change this to still take an IP address or object ID but instead to hash it into (x,y) co-ordinates (within a given area). Can someone please advise me as to how I'd begin to even go about this?

    Many thanks in advance.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    So you take your 32 bit int, and say
    x_coord = hash % 65536;
    y_coord = hash / 65536;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

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

    Thanks for the reply.

    I don't think I follow though. What is the significance of 65536 and when you say "hash" do you mean I should make use of the same library as I attached?

    Thanks again.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You have a 32 bit int.
    65536 is the limit of 16 bits.
    In other words, it splits your 32 bit int into TWO 16-bit ints.

    No, 'hash' is whatever you assigned your 32 bit result to.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. hash function - int to char string + vice versa
    By kerrymaid in forum C Programming
    Replies: 2
    Last Post: 05-29-2010, 11:01 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 04:24 PM