Click here to Skip to main content
15,891,646 members
Articles / Programming Languages / C++
Article

CSHADigest: A fast and single class implementation of SHA1 Digest algorithm

Rate me:
Please Sign up or sign in to vote.
2.60/5 (4 votes)
13 Mar 20052 min read 41.6K   1.1K   18   5
A single class implementation of SHA1 Digest in Unix/Win - simple and fast.

CSHADigest1.jpg (20013 octets)

CSHADigest2.jpg (44367 octets)

Contents

  1. Introduction
  2. Overview
  3. Using CSHADigest Class
  4. Using SHADigest Program

I. Introduction

The CSHADigest class has been designed to be fast and to be easily integrated in applications on independent platforms. This class respects the SHA1 digest 160 bits definitions.

Note: The demo project works on UNIX and Win platforms.

II. Overview

class CSHADigest
{

public:

    /*******************************************/
    /* Constructor and Destructor definitions */
    /*******************************************/
    CSHADigest();
    virtual ~CSHADigest();


    /*******************************************/
    /* Use for compute SHA from a file */
    /*******************************************/
    int ComputeInputFile(const char filePathName[]);


    /*******************************************/
    /* Use for compute SHA from a buffer */
    /*******************************************/
    void Start();
    int ComputeInputBuffer(const uByte* buffer, uInt32 bufferSize);
    void Stop();


    /*******************************************/
    /* Return the last result */
    /*******************************************/
    //Note : Valid only after call Stop()
    uByte* GetByteResult();
    void GetByteResult(uByte* byteResult);
    
    char* GetHexResult();
    void GetHexResult(char* hexResult);


    /*******************************************/
    /* Static functions (perhaps usefull) */
    /*******************************************/
    static uInt32 GetFileSize(const char filePathName[]);
    static void ConvertByteResultToHexResult(const uByte* byteResult, 
                                                    char* hexResult);

protected:

    /******************************************************/
    /* Context struct for each call of ComputeInputBuffer */
    /******************************************************/
    struct SSHAContext
    {
    uInt32 intermediateHash[5];
    uInt32 lengthLow;
    uInt32 lengthHigh;

    uInt16 messageBlockIndex;
    uByte messageBlock[64];
    };

    SSHAContext SHAContext;


    /******************************************************/
    /* Internal functions for computation */
    /******************************************************/
    void PadMessage();
    void ProcessMessageBlock();
    

    /******************************************************/
    /* this buffer include the last SHA Digest result */
    /******************************************************/
    uByte byteResult[20];
    char hexResult[41];
};

III. Using CSHADigest Class

How to digest a string

void Start();
int ComputeInputBuffer(const uByte* buffer, uInt32 bufferSize);
void Stop();

Step 1: Create an instance of CSHADigest class.

CSHADigest SHADigest;

Step 2: Use Start() to initialise the digest processes.

Step 3: Call ComputeInputBuffer() each times it's necessary. Return 0 if an error has been detected, else 1.

Step 4: When you have finished and you want to compute the results, call Stop().

The get digest result call:

char* GetHexResult()

(The SHA Digest is coded on 40 chars in Hex format.)

or

uByte* GetByteResult()

(The SHA Digest is coded on 20 bytes in Binary format.)

View in the SHADigest.cpp file the main function, to have a good example.

Warning: The SHA Digest result returned by GetByteResult() (or GetHexResult()) is valid only during the existence of the SHADigest instance. Preferably use GetByteResult(uByte* byteResult) (or GetHexResult(char* hexResult)) in the other cases.

How to digest a file

int  ComputeInputFile(const char filePathName[]);

Step 1: Create an instance of CSHADigest class.

CSHADigest SHADigest;

Step 2: Just call ComputeInputFile() with the valid filepath as argument. Return 0 if an error has been detected, else 1.

The get digest result call:

char* GetHexResult()

(The digest is coded on 40 chars in Hex format.)

or

uByte* GetByteResult()

(The digest is coded on 20 bytes in Binary format.)

View the SHADigest.cpp file to have a good example.

Warning: The SHA Digest result returned by GetByteResult() (or GetHexResult()) is valid only during the existence of the SHADigest instance. Preferebly use GetByteResult(uByte* byteResult) (or GetHexResult(char* hexResult)) in the other cases.

Using SHADigest Program

On Windows platform

CSHADigest1.jpg (20013 octets)

Compile and copy the SHADigest.exe in [root drive]\windows\System32\ folder.

Now, starts a new instance of the command interpreter (cmd.exe):

Execute SHADigest with the following parameters :

To compute the SHADigest of a file:

SHADigest -f FilePathName (or just SHADigest FilePathName)

To compute the SHADigest of a string:

SHADigest -s string

To compute the SHADigest of some strings:

SHADigest -s string1 string2 [...] stringN

On Unix platform

CSHADigest2.jpg (44367 octets)

To compile the program type make. To copy the SHADigest in /usr/bin/ folder, just type make install. (You must be logged in as root.)

Now, start a shell. Execute SHADigest with the following parameters:

To compute the SHADigest of a file:

SHADigest -f FilePathName (or just SHADigest FilePathName)

To compute the SHADigest of a string:

SHADigest -s string

To compute the SHADigest of some strings:

SHADigest -s string1 string2 [...] stringN

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
France France
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralLatest license info Pin
Jim Mello31-Dec-09 9:02
Jim Mello31-Dec-09 9:02 
QuestionAbout licence Pin
blongtq6-Aug-07 17:31
blongtq6-Aug-07 17:31 
GeneralLittle bug detected Pin
PC-Alex9-Aug-05 3:40
PC-Alex9-Aug-05 3:40 
GeneralAlso see CSHA1 project Pin
DrusTheAxe20-Mar-05 8:46
DrusTheAxe20-Mar-05 8:46 
QuestionUseful as a hash value? Pin
Blake Miller15-Mar-05 4:54
Blake Miller15-Mar-05 4:54 

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.