Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1059129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:07:35+00:00 2026-05-16T18:07:35+00:00

I’m trying to implement HMAC-SHA1 algorithm in my C++/Qt application. I have a method

  • 0

I’m trying to implement HMAC-SHA1 algorithm in my C++/Qt application.
I have a method for Sha1 algorithm available, I just need to understand the HMAC part of it.

This pseudocode is from wikipedia:

 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

What is the blocksize? What does the zeroes-function do on line 8? How do you express lines 12-13 in C++?

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T18:07:35+00:00Added an answer on May 16, 2026 at 6:07 pm

    This post has a working implementation:

    /**
     * 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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have thousands of HTML files to process using Groovy/Java and I need to
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.