Skip to content

Instantly share code, notes, and snippets.

@Supm4n
Created July 10, 2013 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Supm4n/5969377 to your computer and use it in GitHub Desktop.
Save Supm4n/5969377 to your computer and use it in GitHub Desktop.
HMAC-SHA1 w/ QT
QString hmacsha1(const QString & key, const QString & data){
QByteArray ipad;
QByteArray opad;
QByteArray ctx;
QByteArray sha1;
QByteArray k;
k = key.toAscii();
int keyLen = key.size();
if(keyLen > 64){
QByteArray tempKey;
tempKey.append(key);
k = QCryptographicHash::hash(tempKey, QCryptographicHash::Sha1);
keyLen = 20;
}
ipad.fill( 0, 64);
opad.fill(0, 64);
ipad.replace(0, keyLen, k);
opad.replace(0, keyLen, k);
for (int i=0; i<64; i++)
{
ipad[i] = ipad[i] ^ 0x36;
opad[i] = opad[i] ^ 0x5c;
}
ctx.append(ipad,64);
ctx.append(data);
sha1 = QCryptographicHash::hash(ctx, QCryptographicHash::Sha1);
ctx.clear();
ctx.append(opad,64);
ctx.append(sha1);
sha1.clear();
sha1 = QCryptographicHash::hash(ctx, QCryptographicHash::Sha1);
return sha1.toBase64();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment