6

C++/Qt アプリケーションに HMAC-SHA1 アルゴリズムを実装しようとしています。Sha1 アルゴリズムのメソッドを利用できます。その HMAC 部分を理解する必要があるだけです。

この疑似コードはウィキペディアからのものです。

 1 function hmac (key, message)
 2     if (length(key) > blocksize) then
 3         // keys longer than blocksize are shortened
 4         key = hash(key)
 5     end if
 6     if (length(key) < blocksize) then
 7         // keys shorter than blocksize are zero-padded
 8         key = key ∥ zeroes(blocksize - length(key))
 9     end if
10
11     // Where blocksize is that of the underlying hash function
12     o_key_pad = [0x5c * blocksize] ⊕ key
13     i_key_pad = [0x36 * blocksize] ⊕ key // Where ⊕ is exclusive or (XOR)
14     // Where ∥ is concatenation
15     return hash(o_key_pad ∥ hash(i_key_pad ∥ message))
16 end function

ブロックサイズは?ゼロ関数は 8 行目で何をしますか? C++ で 12 ~ 13 行をどのように表現しますか?

4

5 に答える 5

6

1. ブロックサイズとは?

通常、ハッシュアルゴリズムは、データを固定サイズのデータ​​のチャンク (別名「ブロック」) に分割することによって処理します。SHA1 の場合、通常のブロック サイズは 64 バイトです。

2. 8 行目の zeros 関数は何をしますか?

それは(コメントが述べているように)キーの最後に「ゼロ」を追加して、その長さが「ブロック」サイズと一致するようにします。

3. C++ で 12 行目から 13 行目をどのように表現しますか?

XOR 演算子を探していると思います: ^

例:

o_key_pad = (0x5c * blocksize) ^ key; // Actually, it should be 0x5c5c5c... repeated enough so that it matches key size.

簡単なメモ: これには特別な関係はなく、最終的に非プロジェクトで再利用できるようQtに、おそらく「生」で実行したいと思うでしょう。素晴らしい私見ですが、これを実装する必要がないことは明らかです。C++QtQt

于 2010-07-27T06:44:06.647 に答える
5

この投稿には実用的な実装があります:

/**
 * 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;
}
于 2010-08-29T08:25:10.573 に答える
1

QCryptographicHashも確認する必要があります。これは、問題のsha1部分に役立つ可能性があるためです。

于 2010-07-27T09:05:32.930 に答える
1

QCAライブラリを見てみましょう。すべての主要な暗号化アルゴリズムの実装が既に提供されています。

于 2010-07-27T07:28:59.993 に答える