Fabiotk Fabiotk - 4 months ago 28
C++ Question

Any c++ library to work with Md5 using class string?

Is there any c++ standard library to work with Md5 hashing (and other hashes, possibly) by using the c++

std::string
instead of
signed\unsigned char*
?. I found no implementation of this in
openssl/md5.h
since its function MD5 has this header:
unsigned char *MD5(const unsigned char *d, unsigned long n, unsigned char *md);


I'm asking this because I'm trying to set a
QLabel lbResult
's text to the result of the digest of the input of a string (coming from QString& arg1).


MD5((const unsigned char *) arg1.toStdString().c_str(),32,cryptResult);
ui->lbResult->setText(getStringFromUnsignedChar(cryptResult));


I had to build this
getStringFromUnsignedChar
which returns a
QString
object from the result but all these casting and this function could be avoided if I had something line
string MD5(string s);
. Thanks in advance.

Answer

You can use QCryptographicHash. For example:

QByteArray hash = QCryptographicHash::hash(arg1.toUtf8(), QCryptographicHash::Md5);
ui->lbResult->setText(hash.toHex());