Sunday, July 24, 2005

KIO::http_post and KMD5, md5sum usage learnings

I needed MD5 sum for a given string for the api-signing(irrelevant) of flickr's new Authetication API. Simply guessed KMD5 and searched it and woooo!!! it was there.
Usage of KMD5 is quite well documented in their documentation page, what I used and what worked perfectly well for me :

QString final("this is just an example.");
const char *test=final.ascii();
KMD5 context (test);
kdDebug()<< "Test Hex Digest output: " << context.hexDigest().data() << endl;

Since I was not getting proper response from the server on using the MD5 sum, I enquired and came to know that the command md5sum in Linux takes the terminating \n of the string in to account if not told otherwise. i.e
the output of echo "this is just an example." | md5sum
is the md5sum of this is just an example.\n
to get the correct answer the command should be echo -n "this is just an example." | md5sum
This might look very trivial now, but even when I copy pasted string in to a file and checked the md5sum the \n was creating the trouble. So be careful about the \n thing. Also if you can suggest how to get rid of this \n from a file in vim please let me know, I tried backspace etc but it didn't give correct checksum.
KMD5 works without any problem the QString converted using latin1, utf8 etc anything works well. The trailing \0 is not a problem as against what I initially guessed.

Now KIO::http_post. I basically don't have much idea about its implementation details , but only that it is used for posting html form. I wanted to send a form with a query string, so I suspected that I should use content type of application/x-www-form-urlencoded and encode the query string data in to the second argument somehow, I was wrong, that all is not needed, the first argument is a KUrl and it can handle any complex query string as a value so my final code looked like this. (I am still not sure what kind of data is to be send when I want to send the form data using second argument, I guess it should be the utf8 format.)

QString final_url=url+"/?"+queryStr;
QByteArray t;
KIO::TransferJob* job = KIO::http_post(final_url,t,false);
job->addMetaData("content-type", "Content-Type: application/x-www-form-urlencoded" );
connect(job, SIGNAL(data(KIO::Job*, const QByteArray&)),
SLOT(data(KIO::Job*, const QByteArray&)));
connect(job, SIGNAL(result(KIO::Job *)),
SLOT(slotResult(KIO::Job *)));

This page is just placed so that anyone who faces similar question finds a solution. In case the details are inaccurate please let me know I will apply them with acknowledgement.

No comments:

Post a Comment