# HG changeset patch # User Sebastien Jodogne # Date 1380546216 -7200 # Node ID 2737806bcf6031de78bab2f0b2acfb72f69fda65 # Parent bf2fda1c6760ccc76ee14bfd903ba70d75d19707# Parent c5f1986d9ddcf379dfe660a9127d35b906824297 integration mainline->laaw diff -r bf2fda1c6760 -r 2737806bcf60 CMakeLists.txt --- a/CMakeLists.txt Mon Sep 30 13:54:32 2013 +0200 +++ b/CMakeLists.txt Mon Sep 30 15:03:36 2013 +0200 @@ -62,7 +62,6 @@ 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) diff -r bf2fda1c6760 -r 2737806bcf60 Core/Toolbox.cpp --- a/Core/Toolbox.cpp Mon Sep 30 13:54:32 2013 +0200 +++ b/Core/Toolbox.cpp Mon Sep 30 15:03:36 2013 +0200 @@ -39,6 +39,7 @@ #include #include #include +#include #include #include @@ -65,7 +66,6 @@ #include "../Resources/md5/md5.h" #include "../Resources/base64/base64.h" -#include "../Resources/sha1/sha1.h" #ifdef _MSC_VER @@ -572,31 +572,27 @@ void Toolbox::ComputeSHA1(std::string& result, const std::string& data) { - SHA1 sha1; + boost::uuids::detail::sha1 sha1; + if (data.size() > 0) { - sha1.Input(&data[0], data.size()); + sha1.process_bytes(&data[0], data.size()); } - unsigned digest[5]; + unsigned int digest[5]; // Sanity check for the memory layout: A SHA-1 digest is 160 bits wide - assert(sizeof(unsigned) == 4 && sizeof(digest) == (160 / 8)); + assert(sizeof(unsigned int) == 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); - } + sha1.get_digest(digest); + + result.resize(8 * 5 + 4); + sprintf(&result[0], "%08x-%08x-%08x-%08x-%08x", + digest[0], + digest[1], + digest[2], + digest[3], + digest[4]); } bool Toolbox::IsSHA1(const std::string& str) diff -r bf2fda1c6760 -r 2737806bcf60 NEWS --- a/NEWS Mon Sep 30 13:54:32 2013 +0200 +++ b/NEWS Mon Sep 30 15:03:36 2013 +0200 @@ -1,6 +1,7 @@ Pending changes in the mainline =============================== +* Fix of Debian bug #724947 (licensing issue with the SHA-1 library) * Switch to Boost 1.54.0 (cf. issue #9) * Build of the C++ client as a shared library * Improved C++ client API diff -r bf2fda1c6760 -r 2737806bcf60 README --- a/README Mon Sep 30 13:54:32 2013 +0200 +++ b/README Mon Sep 30 15:03:36 2013 +0200 @@ -48,10 +48,15 @@ cite our work using the following BibTeX entry: @inproceedings{Jodogne:ISBI2013, - author = "Jodogne, S. and Bernard, C. and Devillers, M. and Lenaerts, E. and Coucke, P.", - title = "Orthanc -- {A} Lightweight, {REST}ful {DICOM} Server for Healthcare and Medical Research", - booktitle = "Proc. of the International Symposium on Biomedical Imaging ({ISBI})", - year = "2013", + author = {Jodogne, S. and Bernard, C. and Devillers, M. and Lenaerts, E. and Coucke, P.}, + title = {Orthanc -- {A} Lightweight, {REST}ful {DICOM} Server for Healthcare and Medical Research}, + booktitle={Biomedical Imaging ({ISBI}), {IEEE} 10th International Symposium on}, + year={2013}, + pages={190-193}, + ISSN={1945-7928}, + month=apr, + url={http://ieeexplore.ieee.org/xpl/articleDetails.jsp?tp=&arnumber=6556444}, + address={San Francisco, {CA}, {USA}} } diff -r bf2fda1c6760 -r 2737806bcf60 Resources/CMake/BoostConfiguration.cmake --- a/Resources/CMake/BoostConfiguration.cmake Mon Sep 30 13:54:32 2013 +0200 +++ b/Resources/CMake/BoostConfiguration.cmake Mon Sep 30 15:03:36 2013 +0200 @@ -41,13 +41,14 @@ if (BOOST_STATIC) # Parameters for Boost 1.54.0 SET(BOOST_NAME boost_1_54_0) - SET(BOOST_MD5 "cee688c35a9c7775b7305587e782e3f5") + SET(BOOST_BCP_SUFFIX bcpdigest-0.6.2) + SET(BOOST_MD5 "a464288a976ba133f9b325f454cb503d") SET(BOOST_FILESYSTEM_SOURCES_DIR "${BOOST_NAME}/libs/filesystem/src") SET(BOOST_SOURCES_DIR ${CMAKE_BINARY_DIR}/${BOOST_NAME}) DownloadPackage( "${BOOST_MD5}" - "http://www.montefiore.ulg.ac.be/~jodogne/Orthanc/ThirdPartyDownloads/${BOOST_NAME}_bcpdigest.tar.gz" + "http://www.montefiore.ulg.ac.be/~jodogne/Orthanc/ThirdPartyDownloads/${BOOST_NAME}_${BOOST_BCP_SUFFIX}.tar.gz" "${BOOST_SOURCES_DIR}" ) diff -r bf2fda1c6760 -r 2737806bcf60 Resources/CMake/BoostConfiguration.sh --- a/Resources/CMake/BoostConfiguration.sh Mon Sep 30 13:54:32 2013 +0200 +++ b/Resources/CMake/BoostConfiguration.sh Mon Sep 30 15:03:36 2013 +0200 @@ -19,10 +19,11 @@ echo "Generating the subset..." mkdir -p /tmp/bcp/boost_1_54_0 -bcp --boost=/tmp/boost_1_54_0 thread system locale date_time filesystem math/special_functions algorithm /tmp/bcp/boost_1_54_0 +bcp --boost=/tmp/boost_1_54_0 thread system locale date_time filesystem math/special_functions algorithm uuid /tmp/bcp/boost_1_54_0 cd /tmp/bcp echo "Compressing the subset..." -tar cfz boost_1_54_0_bcpdigest.tar.gz boost_1_54_0 -ls -l boost_1_54_0_bcpdigest.tar.gz -md5sum boost_1_54_0_bcpdigest.tar.gz +tar cfz boost_1_54_0_bcpdigest-0.6.2.tar.gz boost_1_54_0 +ls -l boost_1_54_0_bcpdigest-0.6.2.tar.gz +md5sum boost_1_54_0_bcpdigest-0.6.2.tar.gz +readlink -f boost_1_54_0_bcpdigest-0.6.2.tar.gz diff -r bf2fda1c6760 -r 2737806bcf60 Resources/sha1/Makefile --- a/Resources/sha1/Makefile Mon Sep 30 13:54:32 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,41 +0,0 @@ -# -# Makefile -# -# Copyright (C) 1998, 2009 -# Paul E. Jones -# 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 diff -r bf2fda1c6760 -r 2737806bcf60 Resources/sha1/Makefile.nt --- a/Resources/sha1/Makefile.nt Mon Sep 30 13:54:32 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,48 +0,0 @@ -# -# Makefile.nt -# -# Copyright (C) 1998, 2009 -# Paul E. Jones -# 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 - -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 diff -r bf2fda1c6760 -r 2737806bcf60 Resources/sha1/license.txt --- a/Resources/sha1/license.txt Mon Sep 30 13:54:32 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,14 +0,0 @@ -Copyright (C) 1998, 2009 -Paul E. Jones - -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. diff -r bf2fda1c6760 -r 2737806bcf60 Resources/sha1/sha.cpp --- a/Resources/sha1/sha.cpp Mon Sep 30 13:54:32 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,176 +0,0 @@ -/* - * sha.cpp - * - * Copyright (C) 1998, 2009 - * Paul E. Jones - * 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 -#include -#ifdef WIN32 -#include -#endif -#include -#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 [ ...]\n"); - printf("\tThis program will display the message digest (fingerprint)\n"); - printf("\tfor files using the Secure Hashing Algorithm (SHA-1).\n"); -} diff -r bf2fda1c6760 -r 2737806bcf60 Resources/sha1/sha1.cpp --- a/Resources/sha1/sha1.cpp Mon Sep 30 13:54:32 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,589 +0,0 @@ -/* - * sha1.cpp - * - * Copyright (C) 1998, 2009 - * Paul E. Jones - * 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)); -} diff -r bf2fda1c6760 -r 2737806bcf60 Resources/sha1/sha1.h --- a/Resources/sha1/sha1.h Mon Sep 30 13:54:32 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,89 +0,0 @@ -/* - * sha1.h - * - * Copyright (C) 1998, 2009 - * Paul E. Jones - * 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 diff -r bf2fda1c6760 -r 2737806bcf60 Resources/sha1/shacmp.cpp --- a/Resources/sha1/shacmp.cpp Mon Sep 30 13:54:32 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,169 +0,0 @@ -/* - * shacmp.cpp - * - * Copyright (C) 1998, 2009 - * Paul E. Jones - * 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 -#include -#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 \n"); - printf("\tThis program will compare the message digests (fingerprints)\n"); - printf("\tfor two files using the Secure Hashing Algorithm (SHA-1).\n"); -} diff -r bf2fda1c6760 -r 2737806bcf60 Resources/sha1/shatest.cpp --- a/Resources/sha1/shatest.cpp Mon Sep 30 13:54:32 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,149 +0,0 @@ -/* - * shatest.cpp - * - * Copyright (C) 1998, 2009 - * Paul E. Jones - * 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 -#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); -}