Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
14 Mar 2005 1  
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