LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   md5 checksum of a string (https://www.linuxquestions.org/questions/programming-9/md5-checksum-of-a-string-820151/)

aa.bb.cc 07-16-2010 04:58 AM

md5 checksum of a string
 
hello everyone

can anybody help me to get a code in c++ to find the md5 checksum of a string.
thanks in advance

bigearsbilly 07-16-2010 05:25 AM

Is there a C library on your system?
have you tried man -k md5?

on my BSD I have this:
Code:

    #include <sys/types.h>
    #include <sys/md5.h>

    void
    MD5Init(MD5_CTX *buf);

    void
    MD5Transform(u_int32_t buf[4], const unsigned char block[64]);

also
http://sourceforge.net/projects/libmd5-rfc/

google for RFC1321

aa.bb.cc 07-16-2010 06:07 AM

hey...
thankyou so much. i just want to know one more thing.what is the use of the parameter const unsigned char block[64]. i mean what will i send in the argument.

bigearsbilly 07-16-2010 08:11 AM

this works for me on FreeBSD.

(you need to specify the md5 library to link)
export LDLIBS=-lmd

Code:

#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <md5.h>

int main(int argc, char * argv) {

    const char * p = "hello\n";
    char buffer[512];
    MD5Data((void *) p,  strlen(p), buffer);
    puts(buffer);

    return 0;
}

Code:

$ ./md5         
b1946ac92492d2347c6235b4d2611184
$ echo hello | md5
b1946ac92492d2347c6235b4d2611184

no error checking, no warranty ;)


All times are GMT -5. The time now is 07:58 AM.