【问题标题】:How to implement HMAC-SHA1 algorithm in Qt如何在 Qt 中实现 HMAC-SHA1 算法
【发布时间】:2011-03-21 10:46:00
【问题描述】:

我正在尝试在我的 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

什么是块大小? zeroes 函数在第 8 行有什么作用?如何用 C++ 表达第 12-13 行?

【问题讨论】:

    标签: c++ qt hash sha1 hmac


    【解决方案1】:

    1. 块大小是多少?

    通常,哈希算法通过将数据切割成固定大小的数据块(又名“块”)来处理数据。对于 SHA1,我通常的块大小是 64 字节。

    2。 zeros 函数在第 8 行做了什么?

    它(如评论所述)在键的末尾添加“零”,使其长度与“块”大小匹配。

    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++ 中执行此操作,以便您最终可以在非-Qt 项目。 Qt 是伟大的恕我直言,但你显然不需要它来实现这一点。

    【讨论】:

    • 块大小为 64 个八位字节(512 位)。并且“0x5C * blocksize”应该是一个blocksize许多字节的序列,值为0x5C
    • @sellibitze:谢谢。修正了我的答案。
    【解决方案2】:

    看看QCA 库。它已经提供了所有主要加密算法的实现。

    【讨论】:

    • 我知道那个库,但由于我只需要一个函数,我不想将整个库合并到我的软件中。
    【解决方案3】:

    您还应该查看QCryptographicHash,因为它可以帮助您解决问题的 sha1 部分。

    【讨论】:

    • QCryptographicHash 不提供 HMAC-SHA1 算法
    • 不,它没有。但是你当然可以利用它来构建你自己的 hmac-sha1
    【解决方案4】:

    This post 有一个有效的实现:

    /**
     * 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;
    }
    

    【讨论】:

      【解决方案5】:

      从 Qt 5.1 开始,QMessageAuthenticationCode 将使用您选择的 QCryptographicHash::Algorithm 生成 HMAC。

      【讨论】:

      • 这不做 HMAC
      • @LayneBernardo 我将它用于 HMAC 和 Qt Wiki 说它可以 HMAC-SHA1
      猜你喜欢
      • 2012-01-10
      • 2013-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多