Home    | Products    | Downloads    | Purchase    | Support   

 Products

 Paint Express

 HashCalc

 FSUM

 QuickHash Library

    Download

    Purchase

    Samples

    License Agreement

    Related Links

    F.A.Q.

    Overview

 FastCRC Library

 Company

 About Us

 Contact Us

 Miscellaneous

 Affiliate Program

 Site Map

SlavaSoft QuickHash Library Samples
Sample #1 (C++ Interface)  

All

Previous Next


The following sample demonstrates how to use the CSHA1 class to calculate the SHA1 message digest for a string.

#include <iostream>
#include <QuickHash.h>

using namespace std;
using namespace QuickHash;

void ConvertToHex( char* dest, const unsigned char* src, unsigned int count );

int main()
{
    char buff[ 256 ];

    unsigned char digest[ CSHA1::DIGESTSIZE ];
    char          digesthex[ CSHA1::HEXDIGESTSIZE ]; //0 terminated

    do
    {
        //Get the string from the user

        cout << "\nEnter a string: ";
        cin.getline( buff, 256, '\n' );

        cout << "\nDigest for \"" << buff <<"\":";

        //Calculate the digest using CalculateHex

        cout << "\nCalculated using CalculateHex:        ";
       
        CSHA1::CalculateHex( digesthex, (const unsigned char*)buff, strlen( buff ) );

        cout << digesthex;

        //Calculate the digest using Calculate

        cout << "\nCalculated using Calculate:           ";

        CSHA1::Calculate( digest, (const unsigned char*)buff, strlen( buff ) );

        ConvertToHex( digesthex, digest, CSHA1::DIGESTSIZE );

        cout << digesthex;

        //Instantiate a CSHA1 object

        CSHA1 hash;

        //Calculate the digest using Update and FinalHex

        cout << "\nCalculated using Update and FinalHex: ";

        hash.Update( (const unsigned char*)buff, strlen( buff ) );
        hash.FinalHex( digesthex ); // FinalHex reinitializes the hash object for the next use

        cout << digesthex;

        //Calculate the digest using Update and Final

        cout << "\nCalculated using Update and Final:    ";

        hash.Update( (const unsigned char*)buff, strlen( buff ) );
        hash.Final( digest ); // Final reinitializes the hash object for the next use

        ConvertToHex( digesthex, digest, CSHA1::DIGESTSIZE );

        cout << digesthex;

        //Continue?

        cout << "\nContinue (Y/N)?";
        cin.getline( buff, 256 );

    } while ( *buff == 'y' || *buff == 'Y' );

    return 0;
}

void ConvertToHex( char* dest, const unsigned char* src, unsigned int count )
{
    static char hex[ 16 ] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

    for( unsigned int i = 0; i < count; ++i )
    {
        *dest++ = hex[ *src / 16 ];
        *dest++ = hex[ *src++ % 16 ];
    }

    *dest = '\0';
}
 
  Copyright SlavaSoft Inc. All rights reserved.