Click here to Skip to main content
Licence CPOL
First Posted 14 Dec 2007
Views 29,460
Downloads 727
Bookmarked 16 times

C++ Class Implementation of HMAC-SHA

By | 2 May 2010 | Article
C++ class of HMAC-SHA1

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

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Layout  Per page   
  Refresh
QuestionIs the digest same as the one computed by C# code ? PinmemberBeckDai17:55 12 Apr '12  
AnswerRe: Is the digest same as the one computed by C# code ? PinmemberChien-Chung, Chung22:49 7 May '12  
Questionthe result is not right Pinmemberhaolifengwang18:49 1 Jan '12  
AnswerRe: the result is not right PinmemberChien-Chung, Chung22:47 7 May '12  
Generalhelp me Pinmemberkalyan41918:41 27 Aug '09  
QuestionError message PinmemberGodwin Ansa3:05 5 Aug '08  
AnswerRe: Error message PinmemberChien-Chung, Chung16:38 2 May '10  
Generalmemset used wrongly PinmemberDante Shamest21:06 21 Jul '08  
GeneralRe: memset used wrongly Pinmemberhetcorp4:41 1 Apr '09  
GeneralRe: memset used wrongly PinmemberJim Chung16:10 2 May '10  
Questionreceiver processing for HMAC-SHA? PinmemberAdam Harding12:49 3 Jun '08  
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 the packet is discarded etc. Is there any example open source code out there that also implements that functionality?
 
Many thanks,
GeneralBugfix for long key's [modified] Pinmemberbruno19993:31 6 Mar '08  
GeneralThanks Pinmemberbruno19999:37 5 Mar '08  
GeneralRe: Thanks PinmemberGodwin Ansa3:01 27 Aug '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web01 | 2.5.120529.1 | Last Updated 2 May 2010
Article Copyright 2007 by Chien-Chung, Chung
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid