· 6 years ago · Mar 12, 2019, 12:02 PM
1int blockSize = 64;
2
3 QByteArray innerPadding(blockSize, char(0x36));
4 QByteArray outerPadding(blockSize, char(0x5c));
5
6 unsigned char msg[blockSize];
7 const char* result;
8
9 //If the key is longer, hash to make it blockSize
10 if (secretKey.length() > blockSize) {
11 sha256(reinterpret_cast<unsigned char*>(secretKey.data()), secretKey.length(), msg); result = reinterpret_cast<const char*>(msg);
12 secretKey = QByteArray::fromRawData(result, strlen(result));
13 }
14
15 //If the key is shorter, fill with 0x00 until blockSize
16 if (secretKey.length() < blockSize) {
17 int amount = blockSize - secretKey.length();
18 secretKey += QByteArray(amount, char(0x00));
19 }
20
21 for (int i = 0; i < secretKey.length(); i++) {
22 innerPadding[i] = innerPadding[i] ^ secretKey.at(i);
23 outerPadding[i] = outerPadding[i] ^ secretKey.at(i);
24 }
25
26 QByteArray total = outerPadding;
27 QByteArray part = innerPadding;
28
29 part.append(stringToSign);
30 sha256(reinterpret_cast<const unsigned char*>(part.data()), part.length(), msg);
31
32 result = reinterpret_cast<const char*>(msg);
33 total.append(QByteArray::fromRawData(result, strlen(result)));
34
35 sha256(reinterpret_cast<const unsigned char*>(total.data()), total.length(), msg);
36 result = reinterpret_cast<const char*>(msg);
37
38 QByteArray hashed = QByteArray::fromRawData(result, strlen(result));
39 return hashed.toBase64();