changeset 177:81b6f3013738

sha1
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 09 Nov 2012 10:42:00 +0100
parents 81f11fb357f2
children 5739b4d10a4b
files CMakeLists.txt Core/Toolbox.cpp Core/Toolbox.h Resources/sha1/Makefile Resources/sha1/Makefile.nt Resources/sha1/license.txt Resources/sha1/sha.cpp Resources/sha1/sha1.cpp Resources/sha1/sha1.h Resources/sha1/shacmp.cpp Resources/sha1/shatest.cpp UnitTests/main.cpp
diffstat 12 files changed, 1323 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/CMakeLists.txt	Wed Nov 07 17:21:21 2012 +0100
+++ b/CMakeLists.txt	Fri Nov 09 10:42:00 2012 +0100
@@ -65,6 +65,7 @@
 SET(THIRD_PARTY_SOURCES
   ${CMAKE_SOURCE_DIR}/Resources/md5/md5.c
   ${CMAKE_SOURCE_DIR}/Resources/base64/base64.cpp
+  ${CMAKE_SOURCE_DIR}/Resources/sha1/sha1.cpp
   )
 
 include(${CMAKE_SOURCE_DIR}/Resources/CMake/GoogleLogConfiguration.cmake)
--- a/Core/Toolbox.cpp	Wed Nov 07 17:21:21 2012 +0100
+++ b/Core/Toolbox.cpp	Fri Nov 09 10:42:00 2012 +0100
@@ -63,6 +63,7 @@
 
 #include "../Resources/md5/md5.h"
 #include "../Resources/base64/base64.h"
+#include "../Resources/sha1/sha1.h"
 
 
 #if BOOST_HAS_LOCALE == 0
@@ -518,4 +519,36 @@
 
     return result;
   }
+
+  void Toolbox::ComputeSHA1(std::string& result,
+                            const std::string& data)
+  {
+    SHA1 sha1;
+    if (data.size() > 0)
+    {
+      sha1.Input(&data[0], data.size());
+    }
+
+    unsigned digest[5];
+
+    // Sanity check for the memory layout: A SHA-1 digest is 160 bits wide
+    assert(sizeof(unsigned) == 4 && sizeof(digest) == (160 / 8)); 
+    
+    if (sha1.Result(digest))
+    {
+      result.resize(8 * 5 + 4);
+      sprintf(&result[0], "%08x-%08x-%08x-%08x-%08x",
+              digest[0],
+              digest[1],
+              digest[2],
+              digest[3],
+              digest[4]);
+    }
+    else
+    {
+      throw OrthancException(ErrorCode_InternalError);
+    }
+  }
+
+
 }
--- a/Core/Toolbox.h	Wed Nov 07 17:21:21 2012 +0100
+++ b/Core/Toolbox.h	Fri Nov 09 10:42:00 2012 +0100
@@ -73,6 +73,9 @@
     void ComputeMD5(std::string& result,
                     const std::string& data);
 
+    void ComputeSHA1(std::string& result,
+                     const std::string& data);
+
     std::string EncodeBase64(const std::string& data);
 
     std::string GetPathToExecutable();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Resources/sha1/Makefile	Fri Nov 09 10:42:00 2012 +0100
@@ -0,0 +1,41 @@
+#
+#  Makefile
+#  
+#  Copyright (C) 1998, 2009
+#  Paul E. Jones <paulej@packetizer.com>
+#  All Rights Reserved.
+#
+#############################################################################
+#  $Id: Makefile 12 2009-06-22 19:34:25Z paulej $
+#############################################################################
+#
+#  Description:
+#	This is a makefile for UNIX to build the programs sha, shacmp, and
+#	shatest
+#
+#
+
+CC	= g++
+
+CFLAGS	= -c -O2 -Wall -D_FILE_OFFSET_BITS=64
+
+LIBS	=
+
+OBJS	= sha1.o
+
+all: sha shacmp shatest
+
+sha: sha.o $(OBJS)
+	$(CC) -o $@ sha.o $(OBJS) $(LIBS)
+
+shacmp: shacmp.o $(OBJS)
+	$(CC) -o $@ shacmp.o $(OBJS) $(LIBS)
+
+shatest: shatest.o $(OBJS)
+	$(CC) -o $@ shatest.o $(OBJS) $(LIBS)
+
+%.o: %.cpp
+	$(CC) $(CFLAGS) -o $@ $<
+
+clean:
+	$(RM) *.o sha shacmp shatest
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Resources/sha1/Makefile.nt	Fri Nov 09 10:42:00 2012 +0100
@@ -0,0 +1,48 @@
+#
+#  Makefile.nt
+#  
+#  Copyright (C) 1998, 2009
+#  Paul E. Jones <paulej@packetizer.com>
+#  All Rights Reserved.
+#
+#############################################################################
+#  $Id: Makefile.nt 13 2009-06-22 20:20:32Z paulej $
+#############################################################################
+#
+#  Description:
+#	This is a makefile for Win32 to build the programs sha, shacmp, and
+#	shatest
+#
+#  Portability Issues:
+#	Designed to work with Visual C++
+#
+#
+
+.silent:
+
+!include <win32.mak>
+
+RM	= del /q
+
+LIBS	= $(conlibs) setargv.obj
+
+CFLAGS	= -D _CRT_SECURE_NO_WARNINGS /EHsc /O2 /W3
+
+OBJS	= sha1.obj
+
+all: sha.exe shacmp.exe shatest.exe
+
+sha.exe: sha.obj $(OBJS)
+	$(link) $(conflags) -out:$@ sha.obj $(OBJS) $(LIBS)
+
+shacmp.exe: shacmp.obj $(OBJS)
+	$(link) $(conflags) -out:$@ shacmp.obj $(OBJS) $(LIBS)
+
+shatest.exe: shatest.obj $(OBJS)
+	$(link) $(conflags) -out:$@ shatest.obj $(OBJS) $(LIBS)
+
+.cpp.obj:
+	$(cc) $(CFLAGS) $(cflags) $(cvars) $<
+
+clean:
+	$(RM) *.obj sha.exe shacmp.exe shatest.exe
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Resources/sha1/license.txt	Fri Nov 09 10:42:00 2012 +0100
@@ -0,0 +1,14 @@
+Copyright (C) 1998, 2009
+Paul E. Jones <paulej@packetizer.com>
+
+Freeware Public License (FPL)
+
+This software is licensed as "freeware."  Permission to distribute
+this software in source and binary forms, including incorporation 
+into other products, is hereby granted without a fee.  THIS SOFTWARE 
+IS PROVIDED 'AS IS' AND WITHOUT ANY EXPRESSED OR IMPLIED WARRANTIES, 
+INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
+AND FITNESS FOR A PARTICULAR PURPOSE.  THE AUTHOR SHALL NOT BE HELD 
+LIABLE FOR ANY DAMAGES RESULTING FROM THE USE OF THIS SOFTWARE, EITHER 
+DIRECTLY OR INDIRECTLY, INCLUDING, BUT NOT LIMITED TO, LOSS OF DATA 
+OR DATA BEING RENDERED INACCURATE.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Resources/sha1/sha.cpp	Fri Nov 09 10:42:00 2012 +0100
@@ -0,0 +1,176 @@
+/*
+ *  sha.cpp
+ *
+ *  Copyright (C) 1998, 2009
+ *  Paul E. Jones <paulej@packetizer.com>
+ *  All Rights Reserved
+ *
+ *****************************************************************************
+ *  $Id: sha.cpp 13 2009-06-22 20:20:32Z paulej $
+ *****************************************************************************
+ *
+ *  Description:
+ *      This utility will display the message digest (fingerprint) for
+ *      the specified file(s).
+ *
+ *  Portability Issues:
+ *      None.
+ */
+
+#include <stdio.h>
+#include <string.h>
+#ifdef WIN32
+#include <io.h>
+#endif
+#include <fcntl.h>
+#include "sha1.h"
+
+/*
+ *  Function prototype
+ */
+void usage();
+
+
+/*  
+ *  main
+ *
+ *  Description:
+ *      This is the entry point for the program
+ *
+ *  Parameters:
+ *      argc: [in]
+ *          This is the count of arguments in the argv array
+ *      argv: [in]
+ *          This is an array of filenames for which to compute message digests
+ *
+ *  Returns:
+ *      Nothing.
+ *
+ *  Comments:
+ *
+ */
+int main(int argc, char *argv[])
+{
+    SHA1        sha;                        // SHA-1 class
+    FILE        *fp;                        // File pointer for reading files
+    char        c;                          // Character read from file
+    unsigned    message_digest[5];          // Message digest from "sha"
+    int         i;                          // Counter
+    bool        reading_stdin;              // Are we reading standard in?
+    bool        read_stdin = false;         // Have we read stdin?
+
+    /*
+     *  Check the program arguments and print usage information if -?
+     *  or --help is passed as the first argument.
+     */
+    if (argc > 1 && (!strcmp(argv[1],"-?") || !strcmp(argv[1],"--help")))
+    {
+        usage();
+        return 1;
+    }
+
+    /*
+     *  For each filename passed in on the command line, calculate the
+     *  SHA-1 value and display it.
+     */
+    for(i = 0; i < argc; i++)
+    {
+        /*
+         *  We start the counter at 0 to guarantee entry into the for loop.
+         *  So if 'i' is zero, we will increment it now.  If there is no
+         *  argv[1], we will use STDIN below.
+         */
+        if (i == 0)
+        {
+            i++;
+        }
+
+        if (argc == 1 || !strcmp(argv[i],"-"))
+        {
+#ifdef WIN32
+            _setmode(_fileno(stdin), _O_BINARY);
+#endif
+            fp = stdin;
+            reading_stdin = true;
+        }
+        else
+        {
+            if (!(fp = fopen(argv[i],"rb")))
+            {
+                fprintf(stderr, "sha: unable to open file %s\n", argv[i]);
+                return 2;
+            }
+            reading_stdin = false;
+        }
+
+        /*
+         *  We do not want to read STDIN multiple times
+         */
+        if (reading_stdin)
+        {
+            if (read_stdin)
+            {
+                continue;
+            }
+
+            read_stdin = true;
+        }
+
+        /*
+         *  Reset the SHA1 object and process input
+         */
+        sha.Reset();
+
+        c = fgetc(fp);
+        while(!feof(fp))
+        {
+            sha.Input(c);
+            c = fgetc(fp);
+        }
+
+        if (!reading_stdin)
+        {
+            fclose(fp);
+        }
+
+        if (!sha.Result(message_digest))
+        {
+            fprintf(stderr,"sha: could not compute message digest for %s\n",
+                    reading_stdin?"STDIN":argv[i]);
+        }
+        else
+        {
+            printf( "%08X %08X %08X %08X %08X - %s\n",
+                    message_digest[0],
+                    message_digest[1],
+                    message_digest[2],
+                    message_digest[3],
+                    message_digest[4],
+                    reading_stdin?"STDIN":argv[i]);
+        }
+    }
+
+    return 0;
+}
+
+/*  
+ *  usage
+ *
+ *  Description:
+ *      This function will display program usage information to the user.
+ *
+ *  Parameters:
+ *      None.
+ *
+ *  Returns:
+ *      Nothing.
+ *
+ *  Comments:
+ *
+ */
+void usage()
+{
+    printf("usage: sha <file> [<file> ...]\n");
+    printf("\tThis program will display the message digest (fingerprint)\n");
+    printf("\tfor files using the Secure Hashing Algorithm (SHA-1).\n");
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Resources/sha1/sha1.cpp	Fri Nov 09 10:42:00 2012 +0100
@@ -0,0 +1,589 @@
+/*
+ *  sha1.cpp
+ *
+ *  Copyright (C) 1998, 2009
+ *  Paul E. Jones <paulej@packetizer.com>
+ *  All Rights Reserved.
+ *
+ *****************************************************************************
+ *  $Id: sha1.cpp 12 2009-06-22 19:34:25Z paulej $
+ *****************************************************************************
+ *
+ *  Description:
+ *      This class implements the Secure Hashing Standard as defined
+ *      in FIPS PUB 180-1 published April 17, 1995.
+ *
+ *      The Secure Hashing Standard, which uses the Secure Hashing
+ *      Algorithm (SHA), produces a 160-bit message digest for a
+ *      given data stream.  In theory, it is highly improbable that
+ *      two messages will produce the same message digest.  Therefore,
+ *      this algorithm can serve as a means of providing a "fingerprint"
+ *      for a message.
+ *
+ *  Portability Issues:
+ *      SHA-1 is defined in terms of 32-bit "words".  This code was
+ *      written with the expectation that the processor has at least
+ *      a 32-bit machine word size.  If the machine word size is larger,
+ *      the code should still function properly.  One caveat to that
+ *      is that the input functions taking characters and character arrays
+ *      assume that only 8 bits of information are stored in each character.
+ *
+ *  Caveats:
+ *      SHA-1 is designed to work with messages less than 2^64 bits long.
+ *      Although SHA-1 allows a message digest to be generated for
+ *      messages of any number of bits less than 2^64, this implementation
+ *      only works with messages with a length that is a multiple of 8
+ *      bits.
+ *
+ */
+
+
+#include "sha1.h"
+
+/*  
+ *  SHA1
+ *
+ *  Description:
+ *      This is the constructor for the sha1 class.
+ *
+ *  Parameters:
+ *      None.
+ *
+ *  Returns:
+ *      Nothing.
+ *
+ *  Comments:
+ *
+ */
+SHA1::SHA1()
+{
+    Reset();
+}
+
+/*  
+ *  ~SHA1
+ *
+ *  Description:
+ *      This is the destructor for the sha1 class
+ *
+ *  Parameters:
+ *      None.
+ *
+ *  Returns:
+ *      Nothing.
+ *
+ *  Comments:
+ *
+ */
+SHA1::~SHA1()
+{
+    // The destructor does nothing
+}
+
+/*  
+ *  Reset
+ *
+ *  Description:
+ *      This function will initialize the sha1 class member variables
+ *      in preparation for computing a new message digest.
+ *
+ *  Parameters:
+ *      None.
+ *
+ *  Returns:
+ *      Nothing.
+ *
+ *  Comments:
+ *
+ */
+void SHA1::Reset()
+{
+    Length_Low          = 0;
+    Length_High         = 0;
+    Message_Block_Index = 0;
+
+    H[0]        = 0x67452301;
+    H[1]        = 0xEFCDAB89;
+    H[2]        = 0x98BADCFE;
+    H[3]        = 0x10325476;
+    H[4]        = 0xC3D2E1F0;
+
+    Computed    = false;
+    Corrupted   = false;
+}
+
+/*  
+ *  Result
+ *
+ *  Description:
+ *      This function will return the 160-bit message digest into the
+ *      array provided.
+ *
+ *  Parameters:
+ *      message_digest_array: [out]
+ *          This is an array of five unsigned integers which will be filled
+ *          with the message digest that has been computed.
+ *
+ *  Returns:
+ *      True if successful, false if it failed.
+ *
+ *  Comments:
+ *
+ */
+bool SHA1::Result(unsigned *message_digest_array)
+{
+    int i;                                  // Counter
+
+    if (Corrupted)
+    {
+        return false;
+    }
+
+    if (!Computed)
+    {
+        PadMessage();
+        Computed = true;
+    }
+
+    for(i = 0; i < 5; i++)
+    {
+        message_digest_array[i] = H[i];
+    }
+
+    return true;
+}
+
+/*  
+ *  Input
+ *
+ *  Description:
+ *      This function accepts an array of octets as the next portion of
+ *      the message.
+ *
+ *  Parameters:
+ *      message_array: [in]
+ *          An array of characters representing the next portion of the
+ *          message.
+ *
+ *  Returns:
+ *      Nothing.
+ *
+ *  Comments:
+ *
+ */
+void SHA1::Input(   const unsigned char *message_array,
+                    unsigned            length)
+{
+    if (!length)
+    {
+        return;
+    }
+
+    if (Computed || Corrupted)
+    {
+        Corrupted = true;
+        return;
+    }
+
+    while(length-- && !Corrupted)
+    {
+        Message_Block[Message_Block_Index++] = (*message_array & 0xFF);
+
+        Length_Low += 8;
+        Length_Low &= 0xFFFFFFFF;               // Force it to 32 bits
+        if (Length_Low == 0)
+        {
+            Length_High++;
+            Length_High &= 0xFFFFFFFF;          // Force it to 32 bits
+            if (Length_High == 0)
+            {
+                Corrupted = true;               // Message is too long
+            }
+        }
+
+        if (Message_Block_Index == 64)
+        {
+            ProcessMessageBlock();
+        }
+
+        message_array++;
+    }
+}
+
+/*  
+ *  Input
+ *
+ *  Description:
+ *      This function accepts an array of octets as the next portion of
+ *      the message.
+ *
+ *  Parameters:
+ *      message_array: [in]
+ *          An array of characters representing the next portion of the
+ *          message.
+ *      length: [in]
+ *          The length of the message_array
+ *
+ *  Returns:
+ *      Nothing.
+ *
+ *  Comments:
+ *
+ */
+void SHA1::Input(   const char  *message_array,
+                    unsigned    length)
+{
+    Input((unsigned char *) message_array, length);
+}
+
+/*  
+ *  Input
+ *
+ *  Description:
+ *      This function accepts a single octets as the next message element.
+ *
+ *  Parameters:
+ *      message_element: [in]
+ *          The next octet in the message.
+ *
+ *  Returns:
+ *      Nothing.
+ *
+ *  Comments:
+ *
+ */
+void SHA1::Input(unsigned char message_element)
+{
+    Input(&message_element, 1);
+}
+
+/*  
+ *  Input
+ *
+ *  Description:
+ *      This function accepts a single octet as the next message element.
+ *
+ *  Parameters:
+ *      message_element: [in]
+ *          The next octet in the message.
+ *
+ *  Returns:
+ *      Nothing.
+ *
+ *  Comments:
+ *
+ */
+void SHA1::Input(char message_element)
+{
+    Input((unsigned char *) &message_element, 1);
+}
+
+/*  
+ *  operator<<
+ *
+ *  Description:
+ *      This operator makes it convenient to provide character strings to
+ *      the SHA1 object for processing.
+ *
+ *  Parameters:
+ *      message_array: [in]
+ *          The character array to take as input.
+ *
+ *  Returns:
+ *      A reference to the SHA1 object.
+ *
+ *  Comments:
+ *      Each character is assumed to hold 8 bits of information.
+ *
+ */
+SHA1& SHA1::operator<<(const char *message_array)
+{
+    const char *p = message_array;
+
+    while(*p)
+    {
+        Input(*p);
+        p++;
+    }
+
+    return *this;
+}
+
+/*  
+ *  operator<<
+ *
+ *  Description:
+ *      This operator makes it convenient to provide character strings to
+ *      the SHA1 object for processing.
+ *
+ *  Parameters:
+ *      message_array: [in]
+ *          The character array to take as input.
+ *
+ *  Returns:
+ *      A reference to the SHA1 object.
+ *
+ *  Comments:
+ *      Each character is assumed to hold 8 bits of information.
+ *
+ */
+SHA1& SHA1::operator<<(const unsigned char *message_array)
+{
+    const unsigned char *p = message_array;
+
+    while(*p)
+    {
+        Input(*p);
+        p++;
+    }
+
+    return *this;
+}
+
+/*  
+ *  operator<<
+ *
+ *  Description:
+ *      This function provides the next octet in the message.
+ *
+ *  Parameters:
+ *      message_element: [in]
+ *          The next octet in the message
+ *
+ *  Returns:
+ *      A reference to the SHA1 object.
+ *
+ *  Comments:
+ *      The character is assumed to hold 8 bits of information.
+ *
+ */
+SHA1& SHA1::operator<<(const char message_element)
+{
+    Input((unsigned char *) &message_element, 1);
+
+    return *this;
+}
+
+/*  
+ *  operator<<
+ *
+ *  Description:
+ *      This function provides the next octet in the message.
+ *
+ *  Parameters:
+ *      message_element: [in]
+ *          The next octet in the message
+ *
+ *  Returns:
+ *      A reference to the SHA1 object.
+ *
+ *  Comments:
+ *      The character is assumed to hold 8 bits of information.
+ *
+ */
+SHA1& SHA1::operator<<(const unsigned char message_element)
+{
+    Input(&message_element, 1);
+
+    return *this;
+}
+
+/*  
+ *  ProcessMessageBlock
+ *
+ *  Description:
+ *      This function will process the next 512 bits of the message
+ *      stored in the Message_Block array.
+ *
+ *  Parameters:
+ *      None.
+ *
+ *  Returns:
+ *      Nothing.
+ *
+ *  Comments:
+ *      Many of the variable names in this function, especially the single
+ *      character names, were used because those were the names used
+ *      in the publication.
+ *
+ */
+void SHA1::ProcessMessageBlock()
+{
+    const unsigned K[] =    {               // Constants defined for SHA-1
+                                0x5A827999,
+                                0x6ED9EBA1,
+                                0x8F1BBCDC,
+                                0xCA62C1D6
+                            };
+    int         t;                          // Loop counter
+    unsigned    temp;                       // Temporary word value
+    unsigned    W[80];                      // Word sequence
+    unsigned    A, B, C, D, E;              // Word buffers
+
+    /*
+     *  Initialize the first 16 words in the array W
+     */
+    for(t = 0; t < 16; t++)
+    {
+        W[t] = ((unsigned) Message_Block[t * 4]) << 24;
+        W[t] |= ((unsigned) Message_Block[t * 4 + 1]) << 16;
+        W[t] |= ((unsigned) Message_Block[t * 4 + 2]) << 8;
+        W[t] |= ((unsigned) Message_Block[t * 4 + 3]);
+    }
+
+    for(t = 16; t < 80; t++)
+    {
+       W[t] = CircularShift(1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]);
+    }
+
+    A = H[0];
+    B = H[1];
+    C = H[2];
+    D = H[3];
+    E = H[4];
+
+    for(t = 0; t < 20; t++)
+    {
+        temp = CircularShift(5,A) + ((B & C) | ((~B) & D)) + E + W[t] + K[0];
+        temp &= 0xFFFFFFFF;
+        E = D;
+        D = C;
+        C = CircularShift(30,B);
+        B = A;
+        A = temp;
+    }
+
+    for(t = 20; t < 40; t++)
+    {
+        temp = CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[1];
+        temp &= 0xFFFFFFFF;
+        E = D;
+        D = C;
+        C = CircularShift(30,B);
+        B = A;
+        A = temp;
+    }
+
+    for(t = 40; t < 60; t++)
+    {
+        temp = CircularShift(5,A) +
+               ((B & C) | (B & D) | (C & D)) + E + W[t] + K[2];
+        temp &= 0xFFFFFFFF;
+        E = D;
+        D = C;
+        C = CircularShift(30,B);
+        B = A;
+        A = temp;
+    }
+
+    for(t = 60; t < 80; t++)
+    {
+        temp = CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[3];
+        temp &= 0xFFFFFFFF;
+        E = D;
+        D = C;
+        C = CircularShift(30,B);
+        B = A;
+        A = temp;
+    }
+
+    H[0] = (H[0] + A) & 0xFFFFFFFF;
+    H[1] = (H[1] + B) & 0xFFFFFFFF;
+    H[2] = (H[2] + C) & 0xFFFFFFFF;
+    H[3] = (H[3] + D) & 0xFFFFFFFF;
+    H[4] = (H[4] + E) & 0xFFFFFFFF;
+
+    Message_Block_Index = 0;
+}
+
+/*  
+ *  PadMessage
+ *
+ *  Description:
+ *      According to the standard, the message must be padded to an even
+ *      512 bits.  The first padding bit must be a '1'.  The last 64 bits
+ *      represent the length of the original message.  All bits in between
+ *      should be 0.  This function will pad the message according to those
+ *      rules by filling the message_block array accordingly.  It will also
+ *      call ProcessMessageBlock() appropriately.  When it returns, it
+ *      can be assumed that the message digest has been computed.
+ *
+ *  Parameters:
+ *      None.
+ *
+ *  Returns:
+ *      Nothing.
+ *
+ *  Comments:
+ *
+ */
+void SHA1::PadMessage()
+{
+    /*
+     *  Check to see if the current message block is too small to hold
+     *  the initial padding bits and length.  If so, we will pad the
+     *  block, process it, and then continue padding into a second block.
+     */
+    if (Message_Block_Index > 55)
+    {
+        Message_Block[Message_Block_Index++] = 0x80;
+        while(Message_Block_Index < 64)
+        {
+            Message_Block[Message_Block_Index++] = 0;
+        }
+
+        ProcessMessageBlock();
+
+        while(Message_Block_Index < 56)
+        {
+            Message_Block[Message_Block_Index++] = 0;
+        }
+    }
+    else
+    {
+        Message_Block[Message_Block_Index++] = 0x80;
+        while(Message_Block_Index < 56)
+        {
+            Message_Block[Message_Block_Index++] = 0;
+        }
+
+    }
+
+    /*
+     *  Store the message length as the last 8 octets
+     */
+    Message_Block[56] = (Length_High >> 24) & 0xFF;
+    Message_Block[57] = (Length_High >> 16) & 0xFF;
+    Message_Block[58] = (Length_High >> 8) & 0xFF;
+    Message_Block[59] = (Length_High) & 0xFF;
+    Message_Block[60] = (Length_Low >> 24) & 0xFF;
+    Message_Block[61] = (Length_Low >> 16) & 0xFF;
+    Message_Block[62] = (Length_Low >> 8) & 0xFF;
+    Message_Block[63] = (Length_Low) & 0xFF;
+
+    ProcessMessageBlock();
+}
+
+
+/*  
+ *  CircularShift
+ *
+ *  Description:
+ *      This member function will perform a circular shifting operation.
+ *
+ *  Parameters:
+ *      bits: [in]
+ *          The number of bits to shift (1-31)
+ *      word: [in]
+ *          The value to shift (assumes a 32-bit integer)
+ *
+ *  Returns:
+ *      The shifted value.
+ *
+ *  Comments:
+ *
+ */
+unsigned SHA1::CircularShift(int bits, unsigned word)
+{
+    return ((word << bits) & 0xFFFFFFFF) | ((word & 0xFFFFFFFF) >> (32-bits));
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Resources/sha1/sha1.h	Fri Nov 09 10:42:00 2012 +0100
@@ -0,0 +1,89 @@
+/*
+ *  sha1.h
+ *
+ *  Copyright (C) 1998, 2009
+ *  Paul E. Jones <paulej@packetizer.com>
+ *  All Rights Reserved.
+ *
+ *****************************************************************************
+ *  $Id: sha1.h 12 2009-06-22 19:34:25Z paulej $
+ *****************************************************************************
+ *
+ *  Description:
+ *      This class implements the Secure Hashing Standard as defined
+ *      in FIPS PUB 180-1 published April 17, 1995.
+ *
+ *      Many of the variable names in this class, especially the single
+ *      character names, were used because those were the names used
+ *      in the publication.
+ *
+ *      Please read the file sha1.cpp for more information.
+ *
+ */
+
+#ifndef _SHA1_H_
+#define _SHA1_H_
+
+class SHA1
+{
+
+    public:
+
+        SHA1();
+        virtual ~SHA1();
+
+        /*
+         *  Re-initialize the class
+         */
+        void Reset();
+
+        /*
+         *  Returns the message digest
+         */
+        bool Result(unsigned *message_digest_array);
+
+        /*
+         *  Provide input to SHA1
+         */
+        void Input( const unsigned char *message_array,
+                    unsigned            length);
+        void Input( const char  *message_array,
+                    unsigned    length);
+        void Input(unsigned char message_element);
+        void Input(char message_element);
+        SHA1& operator<<(const char *message_array);
+        SHA1& operator<<(const unsigned char *message_array);
+        SHA1& operator<<(const char message_element);
+        SHA1& operator<<(const unsigned char message_element);
+
+    private:
+
+        /*
+         *  Process the next 512 bits of the message
+         */
+        void ProcessMessageBlock();
+
+        /*
+         *  Pads the current message block to 512 bits
+         */
+        void PadMessage();
+
+        /*
+         *  Performs a circular left shift operation
+         */
+        inline unsigned CircularShift(int bits, unsigned word);
+
+        unsigned H[5];                      // Message digest buffers
+
+        unsigned Length_Low;                // Message length in bits
+        unsigned Length_High;               // Message length in bits
+
+        unsigned char Message_Block[64];    // 512-bit message blocks
+        int Message_Block_Index;            // Index into message block array
+
+        bool Computed;                      // Is the digest computed?
+        bool Corrupted;                     // Is the message digest corruped?
+    
+};
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Resources/sha1/shacmp.cpp	Fri Nov 09 10:42:00 2012 +0100
@@ -0,0 +1,169 @@
+/*
+ *  shacmp.cpp
+ *
+ *  Copyright (C) 1998, 2009
+ *  Paul E. Jones <paulej@packetizer.com>
+ *  All Rights Reserved
+ *
+ *****************************************************************************
+ *  $Id: shacmp.cpp 12 2009-06-22 19:34:25Z paulej $
+ *****************************************************************************
+ *
+ *  Description:
+ *      This utility will compare two files by producing a message digest
+ *      for each file using the Secure Hashing Algorithm and comparing
+ *      the message digests.  This function will return 0 if they
+ *      compare or 1 if they do not or if there is an error.
+ *      Errors result in a return code higher than 1.
+ *
+ *  Portability Issues:
+ *      none.
+ *
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include "sha1.h"
+
+/*
+ *  Return codes
+ */
+#define SHA1_COMPARE        0
+#define SHA1_NO_COMPARE     1
+#define SHA1_USAGE_ERROR    2
+#define SHA1_FILE_ERROR     3
+
+/*
+ *  Function prototype
+ */
+void usage();
+
+/*  
+ *  main
+ *
+ *  Description:
+ *      This is the entry point for the program
+ *
+ *  Parameters:
+ *      argc: [in]
+ *          This is the count of arguments in the argv array
+ *      argv: [in]
+ *          This is an array of filenames for which to compute message digests
+ *
+ *  Returns:
+ *      Nothing.
+ *
+ *  Comments:
+ *
+ */
+int main(int argc, char *argv[])
+{
+    SHA1        sha;                        // SHA-1 class
+    FILE        *fp;                        // File pointer for reading files
+    char        c;                          // Character read from file
+    unsigned    message_digest[2][5];       // Message digest for files
+    int         i;                          // Counter
+    bool        message_match;              // Message digest match flag
+    int         returncode;
+
+    /*
+     *  If we have two arguments, we will assume they are filenames.  If
+     *  we do not have to arguments, call usage() and exit.
+     */
+    if (argc != 3)
+    {
+        usage();
+        return SHA1_USAGE_ERROR;
+    }
+
+    /*
+     *  Get the message digests for each file
+     */
+    for(i = 1; i <= 2; i++)
+    {
+        sha.Reset();
+
+        if (!(fp = fopen(argv[i],"rb")))
+        {
+            fprintf(stderr, "sha: unable to open file %s\n", argv[i]);
+            return SHA1_FILE_ERROR;
+        }
+
+        c = fgetc(fp);
+        while(!feof(fp))
+        {
+            sha.Input(c);
+            c = fgetc(fp);
+        }
+
+        fclose(fp);
+
+        if (!sha.Result(message_digest[i-1]))
+        {
+            fprintf(stderr,"shacmp: could not compute message digest for %s\n",
+                    argv[i]);
+            return SHA1_FILE_ERROR;
+        }
+    }
+
+    /*
+     *  Compare the message digest values
+     */
+    message_match = true;
+    for(i = 0; i < 5; i++)
+    {
+        if (message_digest[0][i] != message_digest[1][i])
+        {
+            message_match = false;
+            break;
+        }
+    }
+
+    if (message_match)
+    {
+        printf("Fingerprints match:\n");
+        returncode = SHA1_COMPARE;
+    }
+    else
+    {
+        printf("Fingerprints do not match:\n");
+        returncode = SHA1_NO_COMPARE;
+    }
+
+    printf( "\t%08X %08X %08X %08X %08X\n",
+            message_digest[0][0],
+            message_digest[0][1],
+            message_digest[0][2],
+            message_digest[0][3],
+            message_digest[0][4]);
+    printf( "\t%08X %08X %08X %08X %08X\n",
+            message_digest[1][0],
+            message_digest[1][1],
+            message_digest[1][2],
+            message_digest[1][3],
+            message_digest[1][4]);
+
+    return returncode;
+}
+
+/*  
+ *  usage
+ *
+ *  Description:
+ *      This function will display program usage information to the user.
+ *
+ *  Parameters:
+ *      None.
+ *
+ *  Returns:
+ *      Nothing.
+ *
+ *  Comments:
+ *
+ */
+void usage()
+{
+    printf("usage: shacmp <file> <file>\n");
+    printf("\tThis program will compare the message digests (fingerprints)\n");
+    printf("\tfor two files using the Secure Hashing Algorithm (SHA-1).\n");
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Resources/sha1/shatest.cpp	Fri Nov 09 10:42:00 2012 +0100
@@ -0,0 +1,149 @@
+/*
+ *  shatest.cpp
+ *
+ *  Copyright (C) 1998, 2009
+ *  Paul E. Jones <paulej@packetizer.com>
+ *  All Rights Reserved
+ *
+ *****************************************************************************
+ *  $Id: shatest.cpp 12 2009-06-22 19:34:25Z paulej $
+ *****************************************************************************
+ *
+ *  Description:
+ *      This file will exercise the SHA1 class and perform the three
+ *      tests documented in FIPS PUB 180-1.
+ *
+ *  Portability Issues:
+ *      None.
+ *
+ */
+
+#include <iostream>
+#include "sha1.h"
+
+using namespace std;
+
+/*
+ *  Define patterns for testing
+ */
+#define TESTA   "abc"
+#define TESTB   "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
+
+/*
+ *  Function prototype
+ */
+void DisplayMessageDigest(unsigned *message_digest);
+
+/*  
+ *  main
+ *
+ *  Description:
+ *      This is the entry point for the program
+ *
+ *  Parameters:
+ *      None.
+ *
+ *  Returns:
+ *      Nothing.
+ *
+ *  Comments:
+ *
+ */
+int main()
+{
+    SHA1        sha;
+    unsigned    message_digest[5];
+
+    /*
+     *  Perform test A
+     */
+    cout << endl << "Test A: 'abc'" << endl;
+
+    sha.Reset();
+    sha << TESTA;
+
+    if (!sha.Result(message_digest))
+    {
+        cerr << "ERROR-- could not compute message digest" << endl;
+    }
+    else
+    {
+        DisplayMessageDigest(message_digest);
+        cout << "Should match:" << endl;
+        cout << '\t' << "A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D" << endl;
+    }
+
+    /*
+     *  Perform test B
+     */
+    cout << endl << "Test B: " << TESTB << endl;
+
+    sha.Reset();
+    sha << TESTB;
+
+    if (!sha.Result(message_digest))
+    {
+        cerr << "ERROR-- could not compute message digest" << endl;
+    }
+    else
+    {
+        DisplayMessageDigest(message_digest);
+        cout << "Should match:" << endl;
+        cout << '\t' << "84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1" << endl;
+    }
+
+    /*
+     *  Perform test C
+     */
+    cout << endl << "Test C: One million 'a' characters" << endl;
+
+    sha.Reset();
+    for(int i = 1; i <= 1000000; i++) sha.Input('a');
+
+    if (!sha.Result(message_digest))
+    {
+        cerr << "ERROR-- could not compute message digest" << endl;
+    }
+    else
+    {
+        DisplayMessageDigest(message_digest);
+        cout << "Should match:" << endl;
+        cout << '\t' << "34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F" << endl;
+    }
+
+    return 0;
+}
+
+/*  
+ *  DisplayMessageDigest
+ *
+ *  Description:
+ *      Display Message Digest array
+ *
+ *  Parameters:
+ *      None.
+ *
+ *  Returns:
+ *      Nothing.
+ *
+ *  Comments:
+ *
+ */
+void DisplayMessageDigest(unsigned *message_digest)
+{
+    ios::fmtflags   flags;
+
+    cout << '\t';
+
+    flags = cout.setf(ios::hex|ios::uppercase,ios::basefield);
+    cout.setf(ios::uppercase);
+
+    for(int i = 0; i < 5 ; i++)
+    {
+        cout << message_digest[i] << ' ';
+    }
+
+    cout << endl;
+
+    cout.setf(flags);
+}
--- a/UnitTests/main.cpp	Wed Nov 07 17:21:21 2012 +0100
+++ b/UnitTests/main.cpp	Fri Nov 09 10:42:00 2012 +0100
@@ -266,6 +266,17 @@
   ASSERT_EQ("d41d8cd98f00b204e9800998ecf8427e", s);
 }
 
+TEST(Toolbox, ComputeSHA1)
+{
+  std::string s;
+  
+  Toolbox::ComputeSHA1(s, "The quick brown fox jumps over the lazy dog");
+  ASSERT_EQ("2fd4e1c6-7a2d28fc-ed849ee1-bb76e739-1b93eb12", s);
+  Toolbox::ComputeSHA1(s, "");
+  ASSERT_EQ("da39a3ee-5e6b4b0d-3255bfef-95601890-afd80709", s);
+}
+
+
 TEST(Toolbox, Base64)
 {
   ASSERT_EQ("", Toolbox::EncodeBase64(""));