Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 279
  • Last Modified:

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

0
CBasheer
Asked:
CBasheer
  • 14
  • 11
1 Solution
 
akshayxxCommented:
Hi CBasheer,
whats the problem

Cheers!
0
 
CBasheerAuthor Commented:
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  
0
 
akshayxxCommented:
so u need a library . that have a function which can give u MD5 checksum,  will that do for you?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
akshayxxCommented:
i meant , do u already have the functionality for hashtable, and need only MD5 hash calculation function?
0
 
CBasheerAuthor Commented:
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;
}



0
 
CBasheerAuthor Commented:
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;
}



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

    Are you back to work?....
0
 
akshayxxCommented:
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).
0
 
CBasheerAuthor Commented:
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




0
 
akshayxxCommented:
ok i'll get back to u on monday .(dont have my computer with me). i hope its not that urgent..
0
 
CBasheerAuthor Commented:
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
0
 
akshayxxCommented:
u didnt tell me what OS u r working on.... i can give u appropriate solution only then
0
 
CBasheerAuthor Commented:
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
0
 
akshayxxCommented:
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
0
 
akshayxxCommented:
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
0
 
CBasheerAuthor Commented:
Thanks Akshy,

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

My supervisor suggested me to use MD5.

Cheers
Basheer

0
 
akshayxxCommented:
the example that i gave u , is simple C (esp the md5 code.).. rest is some gui stuff .. that u dont need at all,
all you need is md5.c and md5.h,
still need more clean and more close to c++ implementation ..
here is another link i found for you
http://userpages.umbc.edu/~mabzug1/cs/md5/md5.html
on this download this file
http://userpages.umbc.edu/~mabzug1/cs/md5/md5-cc-102.tar.gz

its completely in c++ and doesnt contain any MFC stuff, and they have given simple usage (drver.cc) and readme file..
hope this helps u
0
 
CBasheerAuthor Commented:
Thank Akshay for your timely help!!.

Regards
Basheer
0
 
akshayxxCommented:
glad to know that you got it working..
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 14
  • 11
Tackle projects and never again get stuck behind a technical roadblock.
Join Now