Click here to Skip to main content
13,295,691 members (37,780 online)
Click here to Skip to main content
Add your own
alternative version


20 bookmarked
Posted 14 Dec 2007

C++ Class Implementation of HMAC-SHA

, 2 May 2010
Rate this:
Please Sign up or sign in to vote.
C++ class of HMAC-SHA1


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.


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) ;
HMAC_SHA1.HMAC_SHA1(test, strlen(test), Key, sizeof(Key), digest) ;
// Check with digest equal to 0xb617318655057264e28bc0b6fb378c8ef146be00
// or not


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


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
Life is nothing but choices.

You may also be interested in...

Comments and Discussions

QuestionUnicode or Multi-Byte Matters? Pin
Member 1207894322-Oct-15 18:21
memberMember 1207894322-Oct-15 18:21 
Questionfeedback Pin
Member 120473359-Oct-15 22:48
memberMember 120473359-Oct-15 22:48 
GeneralMy vote of 5 Pin
nicklin026-Mar-13 4:57
membernicklin026-Mar-13 4:57 
QuestionCode Segfaults with messages longer than buffersize minus digestsize Pin
Member 932791917-Sep-12 7:16
memberMember 932791917-Sep-12 7:16 
QuestionIs the digest same as the one computed by C# code ? Pin
BeckDai12-Apr-12 18:55
memberBeckDai12-Apr-12 18:55 
AnswerRe: Is the digest same as the one computed by C# code ? Pin
Chien-Chung, Chung7-May-12 23:49
memberChien-Chung, Chung7-May-12 23:49 
Questionthe result is not right Pin
haolifengwang1-Jan-12 19:49
memberhaolifengwang1-Jan-12 19:49 
AnswerRe: the result is not right Pin
Chien-Chung, Chung7-May-12 23:47
memberChien-Chung, Chung7-May-12 23:47 
Generalhelp me Pin
kalyan41927-Aug-09 19:41
memberkalyan41927-Aug-09 19:41 
QuestionError message Pin
Godwin Ansa5-Aug-08 4:05
memberGodwin Ansa5-Aug-08 4: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 digest[20] ;
74: unsigned char *test = "Hi There" ;
memset(Key, 0x0b, 20 ) ;
77: HMAC_SHA1.HMAC_SHA1(test, strlen(test), Key, sizeof(Key), digest) ;

I am getting this error:

74: error:invalid conversion from 'const char*' to 'unsigned char*'
77: error:invalid conversion from 'unsigned char*' to 'const char*'
77: error:initializing argument 1 of size_t strlen(const char*)

if I comment out ( // ) the main function, i get the following errors:

/*STEP 1*/

line 26: CSHA1::Update ((UINT_8*)key, key_len);
26: error: undefined reference to 'CSHA1::Update(unsigned char*, unsigned long)'

Can I get some help?. I will be glad to hear from you.


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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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