How to implement HMAC-SHA1 algorithm in Qt
1. What is the blocksize ?
Usually, hash algorithm process data by cutting it into chunks of fixed size data (aka. "blocks"). For SHA1, I the usual block size is 64 bytes.
2. What does the zeros function do on line 8 ?
It (as the comment states) adds "zeroes" to the end of key so that its length matches the "block" size.
3. How do you express lines 12-13 in C++ ?
I think you're looking for the XOR operator: ^
.
Example:
o_key_pad = (0x5c * blocksize) ^ key; // Actually, it should be 0x5c5c5c... repeated enough so that it matches key size.
Just a quick note: this has nothing special to do with Qt
and you will probably want to do it in "raw" C++
so that you can eventually reuse it in a non-Qt
project. Qt
is great imho, but you clearly don't require it to implement this.
This post has a working implementation:
/**
* Hashes the given string using the HMAC-SHA1 algorithm.
*
* \param key The string to be hashed
* \param secret The string that contains secret word
* \return The hashed string
*/
static QString hmac_sha1(const QString &key, const QString &secret) {
// Length of the text to be hashed
int text_length;
// For secret word
QByteArray K;
// Length of secret word
int K_length;
K_length = secret.size();
text_length = key.size();
// Need to do for XOR operation. Transforms QString to
// unsigned char
K = secret.toAscii();
// Inner padding
QByteArray ipad;
// Outer padding
QByteArray opad;
// If secret key > 64 bytes use this to obtain sha1 key
if (K_length > 64) {
QByteArray tempSecret;
tempSecret.append(secret);
K = QCryptographicHash::hash(tempSecret, QCryptographicHash::Sha1);
K_length = 20;
}
// Fills ipad and opad with zeros
ipad.fill(0, 64);
opad.fill(0, 64);
// Copies Secret to ipad and opad
ipad.replace(0, K_length, K);
opad.replace(0, K_length, K);
// XOR operation for inner and outer pad
for (int i = 0; i < 64; i++) {
ipad[i] = ipad[i] ^ 0x36;
opad[i] = opad[i] ^ 0x5c;
}
// Stores hashed content
QByteArray context;
// Appends XOR:ed ipad to context
context.append(ipad, 64);
// Appends key to context
context.append(key);
//Hashes inner pad
QByteArray Sha1 = QCryptographicHash::hash(context, QCryptographicHash::Sha1);
context.clear();
//Appends opad to context
context.append(opad, 64);
//Appends hashed inner pad to context
context.append(Sha1);
Sha1.clear();
// Hashes outerpad
Sha1 = QCryptographicHash::hash(context, QCryptographicHash::Sha1);
// String to return hashed stuff in Base64 format
QByteArray str(Sha1.toBase64());
return str;
}
Since Qt 5.1 there is the QMessageAuthenticationCode that will generate HMAC with the QCryptographicHash::Algorithm of your choice.