PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : md5 in C



Giuly
18-09-2005, 22:31
Hi, kann mir mal jemand erklären, wie man die md5.c benutzt?

SeeksTheMoon
19-09-2005, 07:40
schau in die mddriver.c

Giuly
19-09-2005, 11:17
k, danke. Trotzdem funktioniert das nicht ganz. in einer test.c gehts, aber in meiner ajccc.cpp gehts nicht. Code:


std::string getMD5(std::string pass)
{
MD5_CTX mdCntxt;
MD5Init(&mdCntxt);
const char* toDigest = pass.c_str();
char digest[32];
MD5Update(&mdCntxt, toDigest, sizeof(toDigest));
MD5Final(&mdCntxt);
int i;
for (i = 0; i < 16; i++)
sprintf(digest, "%s%02x", digest, mdCntxt.digest[i]);

return std::string(digest);
}

Fehler:

giuly@Campi64 ~/ajccc/ajccc $ make
gcc -c -Wall -O3 md5.c -o md5.o
g++ -c -Wall -O3 tinyxml.cpp -o tinyxml.o
g++ -c -Wall -O3 tinyxmlparser.cpp -o tinyxmlparser.o
g++ -c -Wall -O3 tinyxmlerror.cpp -o tinyxmlerror.o
g++ -c -Wall -O3 tinystr.cpp -o tinystr.o
g++ -c -Wall -O3 ajccc.cpp -o ajccc.o
ajccc.cpp: In function `std::string getMD5(std::string)':
md5.h:51: error: too many arguments to function `void MD5Init()'
ajccc.cpp:13: error: at this point in file
md5.h:52: error: too many arguments to function `void MD5Update()'
ajccc.cpp:16: error: at this point in file
md5.h:53: error: too many arguments to function `void MD5Final()'
ajccc.cpp:17: error: at this point in file
make: *** [ajccc.o] Error 1

anda_skoa
19-09-2005, 12:25
Nichts zu deinem eigentlichen Problem, aber trotzdem wichtig :)


sizeof(toDigest);

Ist je nach Plattform entweder 4 oder 8, je nachdem wie lange ein Pointer ist.
Ich gehe davon aus, daß die Funktion aber eher die Länge des Inputs wissen will, also vielleicht besser pass.size() benutzen.

Ciao,
_

Giuly
19-09-2005, 12:36
std::string getMD5(std::string pass)
{
MD5_CTX mdCntxt;
MD5Init(&mdCntxt);
char digest[32];
MD5Update(&mdCntxt, pass.c_str(), pass.size());
MD5Final(&mdCntxt);
for (int i = 0; i < 16; i++)
sprintf(digest, "%s%02x", digest, mdCntxt.digest[i]);

return std::string(digest);
}
:)
--
das wird wohl irgendein Problem mit dem Header sein, weil da einfach

void MD5Final ();
steht, und das versteht g++ wohl nicht.
ich werd mal versuchen den Header zu korrigieren. :)

Edit: Das wars ;) Aber jetzt kommt was anderes :/

giuly@Campi64 ~/ajccc/ajccc $ make
g++ -c -Wall -O3 ajccc.cpp -o ajccc.o
ajccc.cpp: In function `std::string getMD5(std::string)':
ajccc.cpp:15: error: invalid conversion from `const char*' to `unsigned char*'
ajccc.cpp:15: error: initializing argument 2 of `void MD5Update(MD5_CTX*, unsigned char*, unsigned int)'
make: *** [ajccc.o] Error 1

Hab den scheiß gecasted, aber beim linken wills nicht :/

giuly@Campi64 ~/ajccc/ajccc $ g++ -o ajccc md5.o tinyxml.o tinyxmlparser.o tinyxmlerror.o tinystr.o ajccc.o
ajccc.o(.text+0x2ea): In function `getMD5(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
: undefined reference to `MD5Init(MD5_CTX*)'
ajccc.o(.text+0x2ff): In function `getMD5(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
: undefined reference to `MD5Update(MD5_CTX*, unsigned char*, unsigned int)'
ajccc.o(.text+0x309): In function `getMD5(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
: undefined reference to `MD5Final(MD5_CTX*)'
collect2: ld returned 1 exit status
giuly@Campi64 ~/ajccc/ajccc $ g++ -o ajccc tinyxml.o tinyxmlparser.o tinyxmlerror.o tinystr.o ajccc.o
ajccc.o(.text+0x2ea): In function `getMD5(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
: undefined reference to `MD5Init(MD5_CTX*)'
ajccc.o(.text+0x2ff): In function `getMD5(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
: undefined reference to `MD5Update(MD5_CTX*, unsigned char*, unsigned int)'
ajccc.o(.text+0x309): In function `getMD5(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
: undefined reference to `MD5Final(MD5_CTX*)'
collect2: ld returned 1 exit status

Sind die .os von gcc mit g++ inkompatibel oder was?

Giuly
19-09-2005, 15:15
external "C" { #include "md5.h" }; ist toll :)