| LEFT | RIGHT | 
| (no file at all) |  | 
|    1 /* |  | 
|    2 100% free public domain implementation of the SHA-1 algorithm |  | 
|    3 by Dominik Reichl <dominik.reichl@t-online.de> |  | 
|    4 Web: http://www.dominik-reichl.de/ |  | 
|    5  |  | 
|    6 Version 1.8 - 2008-03-16 |  | 
|    7 - Converted project files to Visual Studio 2008 format. |  | 
|    8 - Added Unicode support for HashFile utility method. |  | 
|    9 - Added support for hashing files using the HashFile method that are |  | 
|   10 larger than 2 GB. |  | 
|   11 - HashFile now returns an error code instead of copying an error |  | 
|   12 message into the output buffer. |  | 
|   13 - GetHash now returns an error code and validates the input parameter. |  | 
|   14 - Added ReportHashStl STL utility method. |  | 
|   15 - Added REPORT_HEX_SHORT reporting mode. |  | 
|   16 - Improved Linux compatibility of test program. |  | 
|   17  |  | 
|   18 Version 1.7 - 2006-12-21 |  | 
|   19 - Fixed buffer underrun warning that appeared when compiling with |  | 
|   20 Borland C Builder (thanks to Rex Bloom and Tim Gallagher for the |  | 
|   21 patch). |  | 
|   22 - Breaking change: ReportHash writes the final hash to the start |  | 
|   23 of the buffer, i.e. it's not appending it to the string anymore. |  | 
|   24 - Made some function parameters const. |  | 
|   25 - Added Visual Studio 2005 project files to demo project. |  | 
|   26  |  | 
|   27 Version 1.6 - 2005-02-07 (thanks to Howard Kapustein for patches) |  | 
|   28 - You can set the endianness in your files, no need to modify the |  | 
|   29 header file of the CSHA1 class anymore. |  | 
|   30 - Aligned data support. |  | 
|   31 - Made support/compilation of the utility functions (ReportHash and |  | 
|   32 HashFile) optional (useful when bytes count, for example in embedded |  | 
|   33 environments). |  | 
|   34  |  | 
|   35 Version 1.5 - 2005-01-01 |  | 
|   36 - 64-bit compiler compatibility added. |  | 
|   37 - Made variable wiping optional (define SHA1_WIPE_VARIABLES). |  | 
|   38 - Removed unnecessary variable initializations. |  | 
|   39 - ROL32 improvement for the Microsoft compiler (using _rotl). |  | 
|   40  |  | 
|   41 Version 1.4 - 2004-07-22 |  | 
|   42 - CSHA1 now compiles fine with GCC 3.3 under MacOS X  (thanks to Larry |  | 
|   43 Hastings). |  | 
|   44  |  | 
|   45 Version 1.3 - 2003-08-17 |  | 
|   46 - Fixed a small memory bug and made a buffer array a class member to |  | 
|   47 ensure correct working when using multiple CSHA1 class instances at |  | 
|   48 one time. |  | 
|   49  |  | 
|   50 Version 1.2 - 2002-11-16 |  | 
|   51 - Borlands C++ compiler seems to have problems with string addition |  | 
|   52 using sprintf. Fixed the bug which caused the digest report function |  | 
|   53 not to work properly. CSHA1 is now Borland compatible. |  | 
|   54  |  | 
|   55 Version 1.1 - 2002-10-11 |  | 
|   56 - Removed two unnecessary header file includes and changed BOOL to |  | 
|   57 bool. Fixed some minor bugs in the web page contents. |  | 
|   58  |  | 
|   59 Version 1.0 - 2002-06-20 |  | 
|   60 - First official release. |  | 
|   61  |  | 
|   62 ======== Test Vectors (from FIPS PUB 180-1) ======== |  | 
|   63  |  | 
|   64 SHA1("abc") = |  | 
|   65 A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D |  | 
|   66  |  | 
|   67 SHA1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq") = |  | 
|   68 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1 |  | 
|   69  |  | 
|   70 SHA1(A million repetitions of "a") = |  | 
|   71 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F |  | 
|   72 */ |  | 
|   73  |  | 
|   74 #ifndef ___SHA1_HDR___ |  | 
|   75 #define ___SHA1_HDR___ |  | 
|   76  |  | 
|   77 #if !defined(SHA1_UTILITY_FUNCTIONS) && !defined(SHA1_NO_UTILITY_FUNCTIONS) |  | 
|   78 #define SHA1_UTILITY_FUNCTIONS |  | 
|   79 #endif |  | 
|   80  |  | 
|   81 #if !defined(SHA1_STL_FUNCTIONS) && !defined(SHA1_NO_STL_FUNCTIONS) |  | 
|   82 #define SHA1_STL_FUNCTIONS |  | 
|   83 #if !defined(SHA1_UTILITY_FUNCTIONS) |  | 
|   84 #error STL functions require SHA1_UTILITY_FUNCTIONS. |  | 
|   85 #endif |  | 
|   86 #endif |  | 
|   87  |  | 
|   88 #include <memory.h> |  | 
|   89  |  | 
|   90 #ifdef SHA1_UTILITY_FUNCTIONS |  | 
|   91 #include <stdio.h> |  | 
|   92 #include <string.h> |  | 
|   93 #endif |  | 
|   94  |  | 
|   95 #ifdef SHA1_STL_FUNCTIONS |  | 
|   96 #include <string> |  | 
|   97 #endif |  | 
|   98  |  | 
|   99 #ifdef _MSC_VER |  | 
|  100 #include <stdlib.h> |  | 
|  101 #endif |  | 
|  102  |  | 
|  103 // You can define the endian mode in your files without modifying the SHA-1 |  | 
|  104 // source files. Just #define SHA1_LITTLE_ENDIAN or #define SHA1_BIG_ENDIAN |  | 
|  105 // in your files, before including the SHA1.h header file. If you don't |  | 
|  106 // define anything, the class defaults to little endian. |  | 
|  107 #if !defined(SHA1_LITTLE_ENDIAN) && !defined(SHA1_BIG_ENDIAN) |  | 
|  108 #define SHA1_LITTLE_ENDIAN |  | 
|  109 #endif |  | 
|  110  |  | 
|  111 // If you want variable wiping, #define SHA1_WIPE_VARIABLES, if not, |  | 
|  112 // #define SHA1_NO_WIPE_VARIABLES. If you don't define anything, it |  | 
|  113 // defaults to wiping. |  | 
|  114 #if !defined(SHA1_WIPE_VARIABLES) && !defined(SHA1_NO_WIPE_VARIABLES) |  | 
|  115 #define SHA1_WIPE_VARIABLES |  | 
|  116 #endif |  | 
|  117  |  | 
|  118 #if defined(SHA1_HAS_TCHAR) |  | 
|  119 #include <tchar.h> |  | 
|  120 #else |  | 
|  121 #ifdef _MSC_VER |  | 
|  122 #include <tchar.h> |  | 
|  123 #else |  | 
|  124 #ifndef TCHAR |  | 
|  125 #define TCHAR char |  | 
|  126 #endif |  | 
|  127 #ifndef _T |  | 
|  128 #define _T(__x) (__x) |  | 
|  129 #define _tmain main |  | 
|  130 #define _tprintf printf |  | 
|  131 #define _getts gets |  | 
|  132 #define _tcslen strlen |  | 
|  133 #define _tfopen fopen |  | 
|  134 #define _tcscpy strcpy |  | 
|  135 #define _tcscat strcat |  | 
|  136 #define _sntprintf snprintf |  | 
|  137 #endif |  | 
|  138 #endif |  | 
|  139 #endif |  | 
|  140  |  | 
|  141 // Fallback, if no 64-bit support |  | 
|  142 #ifndef _fseeki64 |  | 
|  143 #define _fseeki64 fseek |  | 
|  144 #endif |  | 
|  145 #ifndef _ftelli64 |  | 
|  146 #define _ftelli64 ftell |  | 
|  147 #endif |  | 
|  148  |  | 
|  149 /////////////////////////////////////////////////////////////////////////// |  | 
|  150 // Define variable types |  | 
|  151  |  | 
|  152 #ifndef UINT_8 |  | 
|  153 #ifdef _MSC_VER // Compiling with Microsoft compiler |  | 
|  154 #define UINT_8  unsigned __int8 |  | 
|  155 #else // !_MSC_VER |  | 
|  156 #define UINT_8 unsigned char |  | 
|  157 #endif // _MSC_VER |  | 
|  158 #endif |  | 
|  159  |  | 
|  160 #ifndef UINT_32 |  | 
|  161 #ifdef _MSC_VER // Compiling with Microsoft compiler |  | 
|  162 #define UINT_32 unsigned __int32 |  | 
|  163 #else // !_MSC_VER |  | 
|  164 #if (ULONG_MAX == 0xFFFFFFFF) |  | 
|  165 #define UINT_32 unsigned long |  | 
|  166 #else |  | 
|  167 #define UINT_32 unsigned int |  | 
|  168 #endif |  | 
|  169 #endif // _MSC_VER |  | 
|  170 #endif // UINT_32 |  | 
|  171  |  | 
|  172 #ifndef INT_64 |  | 
|  173 #ifdef _MSC_VER // Compiling with Microsoft compiler |  | 
|  174 #define INT_64 __int64 |  | 
|  175 #else // !_MSC_VER |  | 
|  176 #define INT_64 long long |  | 
|  177 #endif // _MSC_VER |  | 
|  178 #endif // INT_64 |  | 
|  179  |  | 
|  180 #ifndef UINT_64 |  | 
|  181 #ifdef _MSC_VER // Compiling with Microsoft compiler |  | 
|  182 #define UINT_64 unsigned __int64 |  | 
|  183 #else // !_MSC_VER |  | 
|  184 #define UINT_64 unsigned long long |  | 
|  185 #endif // _MSC_VER |  | 
|  186 #endif // UINT_64 |  | 
|  187  |  | 
|  188 /////////////////////////////////////////////////////////////////////////// |  | 
|  189 // Declare SHA-1 workspace |  | 
|  190  |  | 
|  191 typedef union |  | 
|  192 { |  | 
|  193   UINT_8 c[64]; |  | 
|  194   UINT_32 l[16]; |  | 
|  195 } SHA1_WORKSPACE_BLOCK; |  | 
|  196  |  | 
|  197 class CSHA1 |  | 
|  198 { |  | 
|  199 public: |  | 
|  200 #ifdef SHA1_UTILITY_FUNCTIONS |  | 
|  201   // Different formats for ReportHash |  | 
|  202   enum REPORT_TYPE |  | 
|  203   { |  | 
|  204     REPORT_HEX = 0, |  | 
|  205     REPORT_DIGIT = 1, |  | 
|  206     REPORT_HEX_SHORT = 2 |  | 
|  207   }; |  | 
|  208 #endif |  | 
|  209  |  | 
|  210   // Constructor and destructor |  | 
|  211   CSHA1(); |  | 
|  212   ~CSHA1(); |  | 
|  213  |  | 
|  214   UINT_32 m_state[5]; |  | 
|  215   UINT_32 m_count[2]; |  | 
|  216   UINT_32 m_reserved0[1]; // Memory alignment padding |  | 
|  217   UINT_8 m_buffer[64]; |  | 
|  218   UINT_8 m_digest[20]; |  | 
|  219   UINT_32 m_reserved1[3]; // Memory alignment padding |  | 
|  220  |  | 
|  221   void Reset(); |  | 
|  222  |  | 
|  223   // Update the hash value |  | 
|  224   void Update(const UINT_8* pbData, UINT_32 uLen); |  | 
|  225  |  | 
|  226 #ifdef SHA1_UTILITY_FUNCTIONS |  | 
|  227   // Hash in file contents |  | 
|  228   bool HashFile(const TCHAR* tszFileName); |  | 
|  229 #endif |  | 
|  230  |  | 
|  231   // Finalize hash, call before using ReportHash(Stl) |  | 
|  232   void Final(); |  | 
|  233  |  | 
|  234 #ifdef SHA1_UTILITY_FUNCTIONS |  | 
|  235   bool ReportHash(TCHAR* tszReport, REPORT_TYPE rtReportType = REPORT_HEX) const
     ; |  | 
|  236 #endif |  | 
|  237  |  | 
|  238 #ifdef SHA1_STL_FUNCTIONS |  | 
|  239   bool ReportHashStl(std::basic_string<TCHAR>& strOut, REPORT_TYPE rtReportType 
     = |  | 
|  240     REPORT_HEX) const; |  | 
|  241 #endif |  | 
|  242  |  | 
|  243   bool GetHash(UINT_8* pbDest) const; |  | 
|  244  |  | 
|  245 private: |  | 
|  246   // Private SHA-1 transformation |  | 
|  247   void Transform(UINT_32* pState, const UINT_8* pBuffer); |  | 
|  248  |  | 
|  249   // Member variables |  | 
|  250   UINT_8 m_workspace[64]; |  | 
|  251   SHA1_WORKSPACE_BLOCK* m_block; // SHA1 pointer to the byte array above |  | 
|  252 }; |  | 
|  253  |  | 
|  254 #endif // ___SHA1_HDR___ |  | 
| LEFT | RIGHT |