HMAC-SHA256 with Qt 4.8/Symbian / Convert QString to unsigned char[]
-
Hello everyone,
for a Symbian app I'm required to create a HMAC-SHA256 hash from a QString. As Symbian doesn't support anything after Qt 4.8, I can't used the available Crypto libraries with SHA256 support that were added in Qt 5.1. So I found the following method (str is a QString):
@QByteArray array = str.toUtf8();
unsigned char iv[array.size()+1];
for (int i=0; i<array.size(); i++) iv[i] = (unsigned char)array.at(i);unsigned char key[] = "testkey";
unsigned char out[EVP_MAX_MD_SIZE];
unsigned int outlen;ERR_clear_error();
if (!HMAC(EVP_sha256(),key,sizeof(key)-1,iv,sizeof(iv)-1,out,&outlen)) return 0;
QByteArray ret((char*)out);
ret = ret.toBase64();
return ret;@The hash creation (basics from here: http://www.opensource.apple.com/source/OpenSSL098/OpenSSL098-27/src/fips/fips_test_suite.c) works, when I'm passing an unsigned char[] as 'iv' directly (see the link). But when I'm creating an unsigned char[] from a QString it works to 90%. An example hash created with the method with QString to char[] ends with "zswEVA=". I've used various online tools to check how the hash should look like and it's "zs=". The whole hash up to this point is exactly the same at both versions, so I have to have an error when creating the char[] I think. Anyone knows what's wrong here?
Best Regards,
Sebastian -
Please have a look at the "Symbian Cryptography APIs":http://developer.nokia.com/community/wiki/Archived:Symbian_Cryptography_APIs. May be you can find an implementation of the algorithm there. This an old Symbian C++ library for devices that nowadays we call ancient. You can combine the library and Symbian C++ code with Qt 4.8 or Qt 4.7 inside you application.