Click here to Skip to main content
Click here to Skip to main content

C++ Class Implementation of HMAC-SHA

By , 2 May 2010
 

Introduction

This is my first article on CodeProject. Sorry for my poor English.

The reason I think it might be helpful that I share this HMAC-SHA1 class is because I found no related source I could refer to. This is a simple C++ class of HMAC-SHA1 with only single byte character support. You could add double bytes character support if needed. You will find this class contains only a function HMAC_SHA1 that accept test input and hash key, then generates a digest.

Background

Thanks to Dominik Reichl, the SHA1 class I wrapped is from his amazing class. I simply implemented the HMAC algorithm on it. For MD5, you could refer to RFC. There is a detailed programming flow of it.

Using the Code

The usage of this class is extremely simple. Declare CHMAC_SHA1, call its HMAC_SHA1 function. That's it!

You may use HMAC-SHA1 in RFC 2202 test case to verify your implementation.

Following is test case 1 in RFC 2202.

#include "HMAC_SHA1.h"
 
BYTE Key[20] ;
BYTE digest[20] ; 

unsigned char *test = "Hi There" ; 
memset(Key, 0x0b, 20) ;
CHMAC_SHA1 HMAC_SHA1 ;
HMAC_SHA1.HMAC_SHA1(test, strlen(test), Key, sizeof(Key), digest) ;
 
// Check with digest equal to 0xb617318655057264e28bc0b6fb378c8ef146be00
// or not

History

  • 2007/12/17: Updated sample code with RFC 2202 test case 1

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Chien-Chung, Chung
Software Developer (Senior)
Taiwan Taiwan
Member
Life is nothing but choices.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Layout  Per page   
GeneralMy vote of 5membernicklin026 Mar '13 - 3:57 
Work as intend
QuestionCode Segfaults with messages longer than buffersize minus digestsizememberMember 932791917 Sep '12 - 6:16 
The code in method HMAC_SHA1 is copying the ipad and the input text into a buffer of fixed size, without checking the length of the input. This will segfault and crash with input text larger than 4076 bytes (I am trying to sign files and executable which are larger than the buffer size).  ...
QuestionIs the digest same as the one computed by C# code ?memberBeckDai12 Apr '12 - 17:55 
Here is my source code in C#. string apiKey = "1234"; string payload = "4321";   byte[] bKey = Encoding.Default.GetBytes(apiKey); byte[] bPayload = Encoding.Default.GetBytes(payload);   HMACSHA1 hmacSha = new HMACSHA1(bKey, true); hmacSha.Initialize(); byte[] bytes =...
AnswerRe: Is the digest same as the one computed by C# code ?memberChien-Chung, Chung7 May '12 - 22:49 
Do you try to use "Hi There" string and check if the digest is the same as "0xb617318655057264e28bc0b6fb378c8ef146be00" ???
Questionthe result is not rightmemberhaolifengwang1 Jan '12 - 18:49 
the code is : #include <fstream> #include <string> #include "HMAC_SHA1.h" using namespace std; int main() {      fstream fout("D:\\t1.txt",ios::out);      fout << "begin" <<endl;        //CHMAC_SHA1...
AnswerRe: the result is not rightmemberChien-Chung, Chung7 May '12 - 22:47 
You are sending a Binary data in ASCII mode. The binary Digest data is converted to text. You have to set the fout as a binary mode.
Generalhelp mememberkalyan41927 Aug '09 - 18:41 
Iam using folwing code but end up with many errors with conversions   please help me int main() { cout << "begin" ; //CHMAC_SHA1 HMAC_SHA1; BYTE Key[20] ; BYTE digest[20] ;   unsigned char *test = "Hi There" ;   //int unsigned char*y = (char)...
QuestionError messagememberGodwin Ansa5 Aug '08 - 3:05 
Hi there,   I was trying to run the C++ implemention of HMAC-SHA. I have done the neccesary linkage of the the external library ( using Crypto++. ).   I added the following test code to the end of HMAC.cpp file:   int main() { BYTE Key[20] ; BYTE...
AnswerRe: Error messagememberChien-Chung, Chung2 May '10 - 16:38 
Hi   Have you add the CSHA files to your project ??? What kind of platform you are using for this proejct ???   You will get the errors because the compiler checks syntax for the pointer to a string as a const pointer. And parameter 1 for CHMAC_SHA1 is a pointer to unsigned char....
Generalmemset used wronglymemberDante Shamest21 Jul '08 - 21:06 
In the article: memset(Key, 20, 0x0b) ; I think it should be: memset(Key, 0x0b, 20) ;   "Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why. Programmers combine theory and practice: Nothing works and they don't know why."
GeneralRe: memset used wronglymemberhetcorp1 Apr '09 - 4:41 
You are correct. It should be memset(Key, 0x0b, 20) ;
GeneralRe: memset used wronglymemberJim Chung2 May '10 - 16:10 
Thanks folks.   It's a typo, I will try to fix it.
Questionreceiver processing for HMAC-SHA?memberAdam Harding3 Jun '08 - 12:49 
Hi there,   Excellent, this is the sort of code example I have been looking for.   I also want to see example code for the receiver processing that occurs where the HMAC digest values are compared with one another and if the sending digest does not match the receiving digest then...
GeneralBugfix for long key's [modified]memberbruno19996 Mar '08 - 3:31 
There is a small bug in the HMAC_SHA1.cpp code when using long key's.   Key's must be hashed when the size is longer than the block size but the code checks the digest length instead.   This is the fix:   /* STEP 1 */ //if (key_len > SHA1_DIGEST_LENGTH) if (key_len...
GeneralThanksmemberbruno19995 Mar '08 - 9:37 
Thanks for the HMAC implementation, works fine on Mac OSX 10.5! Bruno
GeneralRe: ThanksmemberGodwin Ansa27 Aug '08 - 3:01 
hi Bruno1999,   Can u offer some advice here on how to use the HMAC implementation. I tried using it by got some error messages which I posted for help.   I used the following test data:   int main() { BYTE Key[20] ; BYTE digest[20] ; char *test =...

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 2 May 2010
Article Copyright 2007 by Chien-Chung, Chung
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid