Hello,
      I have written some code on FreeBSD simply to understand the CryptoPP
library. I know, as I am a newbie, that one of the best ways to get going is
by looking at other peoples code so I thought I'd contribute. This code
takes a user input string and creates a HASH of that string. It then, and
this is IMPORTANT, hashes the HASH to get the HMAC. So in other words the
HMAC is of the hex hash and not the text input. The key is static in that is
coded into the application, this can of course be easily modified so that it
can be user supplied. Anyhow it works (validated as creating correct hashes)
on FreeBSD. Also for people like me who get stuck on compiling issues here
is the compile command:

#  g++ userinput.cpp -lcryptopp -L/usr/local/lib -o user.test

Some notes. There is a block at the end which I commented out... Ignore it.
The using directives are pretty useless as I don't take advantage of them,
but left them in anyhow. Also I included the key in the code as a string in
case you want to verify the code in some other program. Oh yeah and contrary
to the disclaimer if you get stuck or have problems feel free to contact me
:).


////////////////////////////////////////////////////////////////////////////
//////////////
//DISCLAIMER:
//
//       This PROGRAM/CD/ITEM/SERVER/SITE/DOCUMENT/CONTENT WAS/IS/WILL BE
//
//       WRITTEN/CREATED/ADMINISTERED/MAINTAINED by cHeCksUm. The
//
//       AUTHOR/ADMIN/CREATOR/FOUNDER/MAINTAINER holds no responsibility for
the        //
//       PROGRAM/CD/ITEM/SERVER/SITE/DOCUMENT/CONTENT or any other
COMPONENT(S)/PART(S) //
//       used BY/WITH the PROGRAM/CD/ITEM/SERVER/SITE/DOCUMENT/CONTENT. The
//
//       AUTHOR/ADMIN/CREATOR/FOUNDER/MAINTAINER takes no responsibility for
any        //
//       DAMAGE/LOSS/INJURY/DEATH OF/BY/TO DATA/PERSON/ITEM/COMPUTER/INCOME
or other    //
//       MISFORTUNE/EVENT/CATASTROPHY CAUSED/SUSTAINED/INFLICTED TO/BY/OF
the afore     //
//       mentioned PROGRAM/CD/ITEM/SERVER/SITE/DOCUMENT/CONTENT whether
direct or       //
//       indirect. The AUTHOR/ADMIN/CREATOR/FOUNDER/MAINTAINER holds no
responsibility  //
//       for MAINTAINING/UPDATING/DEVELOPING/IMPROVING/FINISHING the
//
//       PROGRAM/CD/ITEM/SERVER/SITE/DOCUMENT/CONTENT in question. In other
words: USE  //
//       AT YOUR OWN RISK and DON'T come to ME with PROBLEMS, I DON'T CARE,
unless      //
//       stated otherwise in the
PROGRAM/CD/ITEM/SERVER/SITE/DOCUMENT/CONTENT.          //
//       This PROGRAM/CD/ITEM/SERVER/SITE/DOCUMENT/CONTENT can be
//
//
USED/DISTRIBUTED/SOLD/RENTED/GIVEN/ABUSED/RIPPED/DECOMPILED/DISASSEMBLED/
//
//       MODIFIED/HACKED/CRACKED freely, unless stated otherwise in the
//
//       PROGRAM/CD/ITEM/SERVER/SITE/DOCUMENT/CONTENT; however
//
//       COPYRIGHTED/TRADEMARKED/PATENTED/PROTECTED
//
//       PROGRAM(S)/CD(S)/ITEM(S)/SERVER(S)/SITE(S)/DOCUMENT(S)/CONTENT are
the         //
//       property of their respective
OWNER(S)/PROPRIETER(S)/HOLDER(S)/CREATORS(S) as   //
//       stated in any relevant LICENSE/MANUAL/DOCUMENTATION/COPYRIGHT and
MUST be      //
//       unconditionally ACCEPTED/RESPECTED by you. However there is no
garuantee that  //
//       there is any accompanying LICENSE/MANUAL/DOCUMENTATION/COPYRIGHT
whether       //
//       INTENTIONALLY/UNINTENTIONALLY left out on THEIR/MY part. This does
NOT mean the//
//       mentioned PROGRAM/CD/ITEM/SERVER/SITE/DOCUMENT/CONTENT is
unrestricted, please //
//       contact the respective
OWNER/PROPRIETER/HOLDER/COMPANY/PERSON/CREATORS if      //
//       in doubt about your rights. The only exception to this rule is if
the          //
//       OWNER/PROPRIETER/HOLDER/COMPANY/PERSON/CREATORS is me in which case
I DON'T    //
//       CARE.
//
//**************************************************************************
************//
//                              Item/Program/Cd/Server/Computer/Site:
gsm_control/userinput.cpp                                               //
//              OS:                                             Any
//
//              Language:                                       ANSI C++
//
//                              Requires:
CryptoPP 5.2.1
//
//              Version:                                        0.3
//
//              Date:                                           08112005
(DD-MM-YYYY)                                                            //
//              Author:                                         cHeCksUm
//
//              Comment:                                        Simple
program to test user input      //
//
of encryption data.
//
////////////////////////////////////////////////////////////////////////////
//////////////

/***************************************************************************
**************
****************************************************************************
**************
** EXTERNAL INCLUDES
**
****************************************************************************
**************
****************************************************************************
*************/
#include "/usr/local/include/cryptopp/modes.h"
#include "/usr/local/include/cryptopp/hex.h"
#include "/usr/local/include/cryptopp/filters.h"
#include "/usr/local/include/cryptopp/osrng.h"
#include "/usr/local/include/cryptopp/sha.h"
#include "/usr/local/include/cryptopp/hmac.h"

/***************************************************************************
**************
****************************************************************************
**************
** STANDARD INCLUDES
**
****************************************************************************
**************
****************************************************************************
*************/
#include <string>
#include <iostream>

/***************************************************************************
**************
****************************************************************************
**************
** USING DIRECTIVES
**
****************************************************************************
**************
****************************************************************************
*************/
using namespace CryptoPP;
using namespace std;

/***************************************************************************
**************
****************************************************************************
**************
** MAIN
**
****************************************************************************
**************
****************************************************************************
*************/

int main( int argc, char** argv )
{
        /*****************************
        ** VARIABLES
        *****************************/
        int debug = 0;
        int counter = 0;
        std::string plainText;
        std::string::size_type stringSize;
        std::string::const_iterator strBegin;
        std::string::const_iterator strEnd;
        
        // Initialize crypto stuff
                                unsigned int keyLength = 256;
        byte hashOutputBuffer[ CryptoPP::SHA::DIGESTSIZE ];
        byte hmacOutputBuffer[ CryptoPP::SHA::DIGESTSIZE ];
        memset( hashOutputBuffer, 0x00, CryptoPP::SHA::DIGESTSIZE );
        memset( hmacOutputBuffer, 0x00, CryptoPP::SHA::DIGESTSIZE );
        /*key:
cbb4af87981aa2c079b85e9d9248c63fc7e125cb1fabc4295ff89fb5e0317f6d3735a7c10b0d
2b7ecd663c976e3da39549b9a6956d6a9c38ed4f5bc8e46c1336783cc11659b987d701255977
73971c2a81839d0966b20f4f416fbd25f4cf51d0aded77589cf658bc0e50c93134b447d9488b
bf9ef93a3f07647e648f801da64c5978c680589aa802533ab21a3c832b4e21fa7df9e6eb1505
f8bee2c8a09459bb971fb17a9096d31be86188c719f80ae991ab1299bf242ac446f5067a86c8
eb6cb1720c438abae2fed57f5aa5348e97903f1381f78b9c19e757c8611e6bb076b22e486834
0af4089ce932012240f14fa741054d688fcea954795d05ac1c607428 */
        const byte secretKey[] =
                        {
        0xcb, 0xb4, 0xaf, 0x87, 0x98, 0x1a, 0xa2, 0xc0, 
        0x79, 0xb8, 0x5e, 0x9d, 0x92, 0x48, 0xc6, 0x3f, 
        0xc7, 0xe1, 0x25, 0xcb, 0x1f, 0xab, 0xc4, 0x29, 
        0x5f, 0xf8, 0x9f, 0xb5, 0xe0, 0x31, 0x7f, 0x6d, 
        0x37, 0x35, 0xa7, 0xc1, 0x0b, 0x0d, 0x2b, 0x7e, 
        0xcd, 0x66, 0x3c, 0x97, 0x6e, 0x3d, 0xa3, 0x95, 
        0x49, 0xb9, 0xa6, 0x95, 0x6d, 0x6a, 0x9c, 0x38, 
        0xed, 0x4f, 0x5b, 0xc8, 0xe4, 0x6c, 0x13, 0x36, 
        0x78, 0x3c, 0xc1, 0x16, 0x59, 0xb9, 0x87, 0xd7, 
        0x01, 0x25, 0x59, 0x77, 0x73, 0x97, 0x1c, 0x2a, 
        0x81, 0x83, 0x9d, 0x09, 0x66, 0xb2, 0x0f, 0x4f, 
        0x41, 0x6f, 0xbd, 0x25, 0xf4, 0xcf, 0x51, 0xd0, 
        0xad, 0xed, 0x77, 0x58, 0x9c, 0xf6, 0x58, 0xbc, 
        0x0e, 0x50, 0xc9, 0x31, 0x34, 0xb4, 0x47, 0xd9, 
        0x48, 0x8b, 0xbf, 0x9e, 0xf9, 0x3a, 0x3f, 0x07, 
        0x64, 0x7e, 0x64, 0x8f, 0x80, 0x1d, 0xa6, 0x4c, 
        0x59, 0x78, 0xc6, 0x80, 0x58, 0x9a, 0xa8, 0x02, 
        0x53, 0x3a, 0xb2, 0x1a, 0x3c, 0x83, 0x2b, 0x4e, 
        0x21, 0xfa, 0x7d, 0xf9, 0xe6, 0xeb, 0x15, 0x05, 
        0xf8, 0xbe, 0xe2, 0xc8, 0xa0, 0x94, 0x59, 0xbb, 
        0x97, 0x1f, 0xb1, 0x7a, 0x90, 0x96, 0xd3, 0x1b, 
        0xe8, 0x61, 0x88, 0xc7, 0x19, 0xf8, 0x0a, 0xe9, 
        0x91, 0xab, 0x12, 0x99, 0xbf, 0x24, 0x2a, 0xc4, 
        0x46, 0xf5, 0x06, 0x7a, 0x86, 0xc8, 0xeb, 0x6c, 
        0xb1, 0x72, 0x0c, 0x43, 0x8a, 0xba, 0xe2, 0xfe, 
        0xd5, 0x7f, 0x5a, 0xa5, 0x34, 0x8e, 0x97, 0x90, 
        0x3f, 0x13, 0x81, 0xf7, 0x8b, 0x9c, 0x19, 0xe7, 
        0x57, 0xc8, 0x61, 0x1e, 0x6b, 0xb0, 0x76, 0xb2, 
        0x2e, 0x48, 0x68, 0x34, 0x0a, 0xf4, 0x08, 0x9c, 
        0xe9, 0x32, 0x01, 0x22, 0x40, 0xf1, 0x4f, 0xa7, 
        0x41, 0x05, 0x4d, 0x68, 0x8f, 0xce, 0xa9, 0x54, 
        0x79, 0x5d, 0x05, 0xac, 0x1c, 0x60, 0x74, 0x28
                        };
  
                        /*****************************
        ** BEGIN PROGRAM CODE
        *****************************/
        std::cout << "Please enter the string to SHA hash: ";
        std::getline( cin, plainText );

        // Find the size of the string
        stringSize = plainText.size();
                                
                                // Initialize the input buffer
                                byte inputData[ stringSize ];
                                                                
                                // If bebugging print string size
                                if ( debug == 1)
                                {
                std::cout << "The size of the string is: " << stringSize <<
std::endl;
        }

        // Setup interators for the beginning and end of the string
        strBegin = plainText.begin();
        strEnd = plainText.end();

                                // Loop through the string assigning each
character to the buffer
        while ( strBegin != strEnd )
        {
                                        if ( debug == 1 )
                {
                        std::cout << "Line: " << counter << " Value: " <<
*strBegin << std::endl;
                }
                inputData[ counter++ ] = *strBegin++;
        }
                                
                                // Run the HASH calculation
                                CryptoPP::SHA().CalculateDigest(
hashOutputBuffer, inputData, stringSize );
                        
                        // Run the HMAC calculation
                        CryptoPP::HMAC<SHA >( secretKey, keyLength
).CalculateDigest( hmacOutputBuffer, hashOutputBuffer,
CryptoPP::SHA::DIGESTSIZE );
                        
                        // Print the HASH...
                        std::printf( "SHA: " );
                        for( int i = 0; i < CryptoPP::SHA::DIGESTSIZE; i++ )

                        {
                std::printf("%2.2x", hashOutputBuffer[ i ] );
                        }
                        std::cout << std::endl;
                        
                        // ... and the HMAC     
                        std::printf( "HMAC: " );
                        for( int i = 0; i < CryptoPP::SHA::DIGESTSIZE; i++ )

                        {
                std::printf("%2.2x", hmacOutputBuffer[ i ] );
                        }
                        std::cout << std::endl;

        return 0;
                
        /*
        #include "hex.h" // from crypto++ library
                        HexEncoder hexEncoder;
                        hexEncoder.Put(pData,dwLen);
                        hexEncoder.MessageEnd();
                        hexEncoder.Get(pData2,dwLen*2);
                        */
                        
                        /*
                        HexDecoder hexDecoder;
                        hexDecoder.Put(pData,dwLen);
                        hexDecoder.MessageEnd();
                        hexDecoder.Get(pData2,dwLen/2);
                        */
}

Reply via email to