Created
July 10, 2013 19:25
-
-
Save Supm4n/5969377 to your computer and use it in GitHub Desktop.
HMAC-SHA1 w/ QT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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