Link to home
Start Free TrialLog in
Avatar of CBasheer
CBasheer

asked on

How to use MD5 hasing function ?

Hi all,

    Please help me to create a hash table useing MD5 hasing function. I need to pass a input parameter to MD5 hf, and this function will generate and return a index key to the hash table. If two records points to same index key, I have to create a bucket (or another node) which also map to the same index key to store the second value and so on.. to avoid collision.

Expecting your valuable help.

Thanks
Basheer

Avatar of akshayxx
akshayxx
Flag of United States of America image

Hi CBasheer,
whats the problem

Cheers!
Avatar of CBasheer
CBasheer

ASKER

Hi Akshay

    I need a c++ code to create a hash table using MD5. The MD5 function is quite huge and confusing for me, Kindly explain me how it is working and how to use it.

Thanks
Basheer  
so u need a library . that have a function which can give u MD5 checksum,  will that do for you?
i meant , do u already have the functionality for hashtable, and need only MD5 hash calculation function?
I have the following code, which creates,insert, retrieve values in(from) the table. There is a function called hashkey which generate index key. I need this function to replace by MD5. And need to extend this program to hadle buckets.

Thanks
Basheer

#include <iostream.h>
#include <string.h>

//#define MAX_VALUE 10;

class hash
{
public:
     hash(void);
     int hashkey(int);
     void inserthash(int item);
     void retrieveitem(int &item);
    void printhash(void);
private:
     int info [10];
     int length;

};

hash::hash(void)
{
     length = 0;
     for (int i=0; i<10; i++)
          info[i] = 0;
}

int hash::hashkey(int n)
{
     
     return(n % 100);
}

void hash::inserthash(int item)
{
     int location;
     location = hashkey(item);
        info[location] = item;
     length++;
}

void hash::retrieveitem(int & searchitem)
{
     int location,item;
     location = hashkey(searchitem);
     item = info[location];

     if (item == searchitem)
          cout << "item found";
          else
               cout << "item  not found";
}

void hash::printhash()
{
     cout << "stuid" << "\n";
     
     for (int i= 0; i < 10; i++)
     {
          if (info[i] == 0)
               continue;
          else
          cout <<info[i]<<"\n";
     }
}

int main()
{
    hash h; int id;
     
     while(id != 0)
     {
          cout <<"enter stuid, 0 for end" <<"\n";
          cin >> id;
                 
          h.inserthash(id);
     }

     cout << "enter id for search" << "\n";
     cin >> id;

     h.retrieveitem(id);
     cout << "print hash table";
     h.printhash();

    return 0;
}



Hi Akshay,

I have the following code, which creates,insert, retrieve values in(from) the table. There is a function called hashkey which generate index key. I need this function to replace by MD5. And need to extend this program to hadle buckets.

Thanks
Basheer

#include <iostream.h>
#include <string.h>

//#define MAX_VALUE 10;

class hash
{
public:
     hash(void);
     int hashkey(int);
     void inserthash(int item);
     void retrieveitem(int &item);
    void printhash(void);
private:
     int info [10];
     int length;

};

hash::hash(void)
{
     length = 0;
     for (int i=0; i<10; i++)
          info[i] = 0;
}

int hash::hashkey(int n)
{
     
     return(n % 100);
}

void hash::inserthash(int item)
{
     int location;
     location = hashkey(item);
        info[location] = item;
     length++;
}

void hash::retrieveitem(int & searchitem)
{
     int location,item;
     location = hashkey(searchitem);
     item = info[location];

     if (item == searchitem)
          cout << "item found";
          else
               cout << "item  not found";
}

void hash::printhash()
{
     cout << "stuid" << "\n";
     
     for (int i= 0; i < 10; i++)
     {
          if (info[i] == 0)
               continue;
          else
          cout <<info[i]<<"\n";
     }
}

int main()
{
    hash h; int id;
     
     while(id != 0)
     {
          cout <<"enter stuid, 0 for end" <<"\n";
          cin >> id;
                 
          h.inserthash(id);
     }

     cout << "enter id for search" << "\n";
     cin >> id;

     h.retrieveitem(id);
     cout << "print hash table";
     h.printhash();

    return 0;
}



Sorry the message was posted twice
Sorry the message was posted twice
Sorry the message was posted twice
Sorry the message was posted twice
Sorry the message was posted twice
its ok .. now its my sleeping time, i'll have a look at it tomorrow.
Hi Akshay,

    Are you back to work?....
if its not too urgent i'll have a look when my 'office' work is finished ...
till then u can tell me this.. so that i shud attack the specific part only ..
 do u believe ur hashtable is implemented completely and u need only hash-function..
have a look on this free library , they provide  a lot of hashing functions , including MD5, i used this 1 year back .. if u have problem, in getting it working, i can help u out
http://mhash.sourceforge.net/

also r u expecting me to modify / suggest ur existing hashtable function also ..
btw  can u tell me the OS u r working with .. i know couple of good libraries which provide good hashtable implementations . so in ur project u can use them without worrying about collisions and other problems..

if ur primary goal is to develop a hashtable .. then yes u have to work with ur existing code only .. so please list down point by point .. what do u expect to be modified OR helped on. ( if any , in relation to developing hash-table).
Hi Akshay

I have downloaded MD5, and followed the instruction in INSTALL to install. Now how to make use of these files or call md5 hash calculation?!!

Thanks for your valuable comments so for...

Thanks
C.Basheer




ok i'll get back to u on monday .(dont have my computer with me). i hope its not that urgent..
HiAkshay,

    Yes, it is not too urget, but need to submit by this Thursday (eve 6.p.m.) Please give me an example program which call MD5.

Thanks
Basheer
u didnt tell me what OS u r working on.... i can give u appropriate solution only then
Hi Akshay,

 I am working in Unix, If I get the aswer for VC++ 6.0 for windows also o.k. for me.

Thanks
Basheer
Hi CBasheer,
look at this source code + sample .. this is some window GUI + md5.c and .h files which the author have used in
mainwin.c

MainWin_OnMenuExec
 calls
MainWin_ExecFileHash
which calls
CalcFileMD5
the last one is the  important function
u will have to implement only this type of function....in the given example it is only getting filename and  one byte array(unsigned char).. of the size of the hash-digest.
in the example first it called md5_initialize()..
and then is reads the file block by block and calls
md5_update.. which keeps upddating the md5 calculation

in ur case since u need to calculate md5-hash for only one string .. u need to call md5_update only once .. after the md5_initialize
and then u call md5_final..
after that the hash will be filled in the byte array that u passed..
try it out .. its pretty simple .. u just have to use md5.c and .h and ur own main.c .. take the example to use md5 functions from the mainwin.c file
best of luck

http://web.bsn.ch/lasse/md5cksum104.zip

why and who suggested you to use md5-hash for hashtable keys ?.. just out of curiosity
also using the above example .. in ur program u need to calculate the md5_checksum for many strings .. so u need not do all the initialisation steps for each string .. if u look in the md5.h file . there is a function md5_reset()..
which u can call once md5 checksum calculated for one string and u need to calculate for another string..
 good luck with ur project
Thanks Akshy,

    Is there any samples for MD5 which use only c++ instead of MFC.

My supervisor suggested me to use MD5.

Cheers
Basheer

ASKER CERTIFIED SOLUTION
Avatar of akshayxx
akshayxx
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank Akshay for your timely help!!.

Regards
Basheer
glad to know that you got it working..