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

C++ Class Implementation of HMAC-SHA

, 2 May 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

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

Comments and Discussions

 
GeneralMy vote of 5 Pinmembernicklin026-Mar-13 4:57 
QuestionCode Segfaults with messages longer than buffersize minus digestsize PinmemberMember 932791917-Sep-12 7:16 
QuestionIs the digest same as the one computed by C# code ? PinmemberBeckDai12-Apr-12 18: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 = hmacSha.ComputeHash(bPayload);
 
string signature = BitConverter.ToString(bytes).Replace("-", string.Empty);
Console.WriteLine(signature);
 
# I get 10BF095402AAC48CE0BF410BCBFF730F1CE52887 where with your code I get 10DF6D7068F4320F86ECE0002D52A071042CB4F5, this is how I use it, thanks, sorry for my pool English.
 
BYTE Key[20] ;
BYTE digest[20] ;
BYTE input[20];
 
printf("%d",sizeof(Key));
 
memset(Key, 0, 20);
memset(input,0,20);
 
int count = 0;
char *test = "4321";
for (char *s = test; strlen(s)>0; s+=2)
{
    if(*s>='0' && *s<='9')
        input[count]=*s-'0';
    else if(*s>='a'&& *s<='f')
        input[count]=*s-'a'+10;
    if(strlen(s)>1)
    {
        input[count]*=16;
        if(*(s+1)>='0' && *(s+1)<='9') input[count]+=(*(s+1)-'0');
        if(*(s+1)>='a'&& *(s+1)<='f') input[count]+=(*(s+1)-'a'+10);
    }
    count++;
}
 
count = 0;
char * strkey = "1234";
for (char *s1 = strkey; strlen(s1)>0; s1+=2)
{
    if(*s1>='0' && *s1<='9')
        Key[count]=*s1-'0';
    else if(*s1>='a'&& *s1<='f')
        Key[count]=*s1-'a'+10;
    if(strlen(s1)>1)
    {
            Key[count]*=16;
            if(*(s1+1)>='0' && *(s1+1)<='9') Key[count]+=(*(s1+1)-'0');
            if(*(s1+1)>='a'&& *(s1+1)<='f') Key[count]+=(*(s1+1)-'a'+10);
    }
    count++;
}
 
CHMAC_SHA1 HMAC_SHA1 ;
HMAC_SHA1.HMAC_SHA1(input, 2, Key, 2, digest) ;
 
char temp[41];
 
memset(temp,0,41);
 
int i = 0;
for (i = 0; i<20;i++)
{
    sprintf(temp+i*2,"%02X",digest[i]);
}
printf ("%s",temp);

AnswerRe: Is the digest same as the one computed by C# code ? PinmemberChien-Chung, Chung7-May-12 23:49 
Questionthe result is not right Pinmemberhaolifengwang1-Jan-12 19:49 
AnswerRe: the result is not right PinmemberChien-Chung, Chung7-May-12 23:47 
Generalhelp me Pinmemberkalyan41927-Aug-09 19:41 
QuestionError message PinmemberGodwin Ansa5-Aug-08 4:05 
AnswerRe: Error message PinmemberChien-Chung, Chung2-May-10 17:38 
Generalmemset used wrongly PinmemberDante Shamest21-Jul-08 22:06 
GeneralRe: memset used wrongly Pinmemberhetcorp1-Apr-09 5:41 
GeneralRe: memset used wrongly PinmemberJim Chung2-May-10 17:10 
Questionreceiver processing for HMAC-SHA? PinmemberAdam Harding3-Jun-08 13:49 
GeneralBugfix for long key's [modified] Pinmemberbruno19996-Mar-08 4:31 
GeneralThanks Pinmemberbruno19995-Mar-08 10:37 
GeneralRe: Thanks PinmemberGodwin Ansa27-Aug-08 4:01 

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.

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