HMAC
//密钥散列消息认证码
QString HMAC(QByteArray baseString, QByteArray key, QCryptographicHash::Algorithm type = QCryptographicHash::Algorithm::Sha256);
QString HMAC(QByteArray baseString, QByteArray key, QCryptographicHash::Algorithm type)
{
//HMAC-SHA-1 block size
int blockSize = 64;
//if key is longer than block size (64), reduce key length with SHA-1 compression
if (key.length() > blockSize)
{
key = QCryptographicHash::hash(key, type);
}
//initialize inner padding with char "6"
QByteArray innerPadding(blockSize, char(0x36));
//initialize outer padding with char "/"
QByteArray outerPadding(blockSize, char(0x5c));
//ascii characters 0x36 ("6") and 0x5c ("/") are selected because they have large
//Hamming distance (http://en.wikipedia.org/wiki/Hamming_distance)
for (int i = 0; i < key.length(); i++)
{
//XOR operation between every byte in key and innerpadding, of key length
innerPadding[i] = innerPadding[i] ^ key.at(i);
//XOR operation between every byte in key and outerpadding, of key length
outerPadding[i] = outerPadding[i] ^ key.at(i);
}
QByteArray total = outerPadding;
QByteArray part = innerPadding;
part.append(baseString);
total.append(QCryptographicHash::hash(part, type));
QByteArray hashed = QCryptographicHash::hash(total, type);
return QByteArray::fromHex(hashed.toHex()).toHex();
}
转载请注明出处并保持作品的完整性,谢谢