?
Solved

How to use MD5 hasing function ?

Posted on 2003-03-26
25
Medium Priority
?
275 Views
Last Modified: 2010-04-01
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
Comment
Question by:CBasheer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 14
  • 11
25 Comments
 
LVL 8

Expert Comment

by:akshayxx
ID: 8215019
Hi CBasheer,
whats the problem

Cheers!
0
 

Author Comment

by:CBasheer
ID: 8215096
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
 
LVL 8

Expert Comment

by:akshayxx
ID: 8216381
so u need a library . that have a function which can give u MD5 checksum,  will that do for you?
0
Independent Software Vendors: 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!

 
LVL 8

Expert Comment

by:akshayxx
ID: 8216388
i meant , do u already have the functionality for hashtable, and need only MD5 hash calculation function?
0
 

Author Comment

by:CBasheer
ID: 8216604
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
 

Author Comment

by:CBasheer
ID: 8216607
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
 

Author Comment

by:CBasheer
ID: 8216612
Sorry the message was posted twice
0
 

Author Comment

by:CBasheer
ID: 8216624
Sorry the message was posted twice
0
 

Author Comment

by:CBasheer
ID: 8216657
Sorry the message was posted twice
0
 

Author Comment

by:CBasheer
ID: 8216856
Sorry the message was posted twice
0
 

Author Comment

by:CBasheer
ID: 8217187
Sorry the message was posted twice
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8217440
its ok .. now its my sleeping time, i'll have a look at it tomorrow.
0
 

Author Comment

by:CBasheer
ID: 8222515
Hi Akshay,

    Are you back to work?....
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8223465
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
 

Author Comment

by:CBasheer
ID: 8233031
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
 
LVL 8

Expert Comment

by:akshayxx
ID: 8233569
ok i'll get back to u on monday .(dont have my computer with me). i hope its not that urgent..
0
 

Author Comment

by:CBasheer
ID: 8244163
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
 
LVL 8

Expert Comment

by:akshayxx
ID: 8250259
u didnt tell me what OS u r working on.... i can give u appropriate solution only then
0
 

Author Comment

by:CBasheer
ID: 8251466
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
 
LVL 8

Expert Comment

by:akshayxx
ID: 8252578
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
 
LVL 8

Expert Comment

by:akshayxx
ID: 8252605
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
 

Author Comment

by:CBasheer
ID: 8253365
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
 
LVL 8

Accepted Solution

by:
akshayxx earned 200 total points
ID: 8255065
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
 

Author Comment

by:CBasheer
ID: 8269651
Thank Akshay for your timely help!!.

Regards
Basheer
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8275409
glad to know that you got it working..
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question