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

This is a discussion on Hash function - Object ID/IP address to 2D co-ordinates within the C Programming forums, part of the General Programming Boards category; Hi, I'm trying to reverse engineer and extend code that I have inherited from another postgrad student (now gone). Basically ...

  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.
    Attached Files Attached Files

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 02: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, 01:28 PM
  5. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 04:24 PM

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