SHA1.h

Go to the documentation of this file.
00001 /*
00002         100% free public domain implementation of the SHA-1 algorithm
00003         by Dominik Reichl <dominik.reichl@t-online.de>
00004  
00005         Version 1.5 - 2005-01-01
00006         - 64-bit compiler compatibility added
00007         - Made variable wiping optional (define SHA1_WIPE_VARIABLES)
00008         - Removed unnecessary variable initializations
00009         - ROL32 improvement for the Microsoft compiler (using _rotl)
00010  
00011         ======== Test Vectors (from FIPS PUB 180-1) ========
00012  
00013         SHA1("abc") =
00014                 A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
00015  
00016         SHA1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq") =
00017                 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
00018  
00019         SHA1(A million repetitions of "a") =
00020                 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
00021 */
00022 
00028 #ifndef ___SHA1_HDR___
00029 #define ___SHA1_HDR___
00030 
00031 #include <omnetpp.h>
00032 
00033 #if !defined(SHA1_UTILITY_FUNCTIONS) && !defined(SHA1_NO_UTILITY_FUNCTIONS)
00034 #define SHA1_UTILITY_FUNCTIONS
00035 #endif
00036 
00037 #include <memory.h> // Needed for memset and memcpy
00038 
00039 #ifdef SHA1_UTILITY_FUNCTIONS
00040 #include <stdio.h>  // Needed for file access and sprintf
00041 #include <string.h> // Needed for strcat and strcpy
00042 #endif
00043 
00044 #ifdef _MSC_VER
00045 #include <stdlib.h>
00046 #endif
00047 
00048 // You can define the endian mode in your files, without modifying the SHA1
00049 // source files. Just #define SHA1_LITTLE_ENDIAN or #define SHA1_BIG_ENDIAN
00050 // in your files, before including the SHA1.h header file. If you don't
00051 // define anything, the class defaults to little endian.
00052 
00053 #if !defined(SHA1_LITTLE_ENDIAN) && !defined(SHA1_BIG_ENDIAN)
00054 #define SHA1_LITTLE_ENDIAN
00055 #endif
00056 
00057 // Same here. If you want variable wiping, #define SHA1_WIPE_VARIABLES, if
00058 // not, #define SHA1_NO_WIPE_VARIABLES. If you don't define anything, it
00059 // defaults to wiping.
00060 
00061 #if !defined(SHA1_WIPE_VARIABLES) && !defined(SHA1_NO_WIPE_VARIABLES)
00062 #define SHA1_WIPE_VARIABLES
00063 #endif
00064 
00066 // Define 8- and 32-bit variables
00067 
00068 #ifndef UINT_32
00069 
00070 #ifdef _MSC_VER
00071 
00072 #define UINT_8  unsigned __int8
00073 #define UINT_32 unsigned __int32
00074 
00075 #else
00076 
00077 #define UINT_8 unsigned char
00078 
00079 #if (ULONG_MAX == 0xFFFFFFFF)
00080 #define UINT_32 unsigned long
00081 #else
00082 #define UINT_32 unsigned int
00083 #endif
00084 
00085 #endif
00086 #endif
00087 
00089 // Declare SHA1 workspace
00090 
00091 typedef union
00092 {
00093     UINT_8  c[64];
00094     UINT_32 l[16];
00095 } SHA1_WORKSPACE_BLOCK;
00096 
00097 class CSHA1
00098 {
00099 public:
00100 #ifdef SHA1_UTILITY_FUNCTIONS
00101     // Two different formats for ReportHash(...)
00102     enum
00103     {
00104         REPORT_HEX = 0,
00105         REPORT_DIGIT = 1
00106     };
00107 #endif
00108 
00109     // Constructor and Destructor
00110     CSHA1();
00111     ~CSHA1();
00112 
00113     UINT_32 m_state[5];
00114     UINT_32 m_count[2];
00115     UINT_32 __reserved1[1];
00116     UINT_8  m_buffer[64];
00117     UINT_8  m_digest[20];
00118     UINT_32 __reserved2[3];
00119 
00120     void Reset();
00121 
00122     // Update the hash value
00123     void Update(UINT_8 *data, UINT_32 len);
00124 #ifdef SHA1_UTILITY_FUNCTIONS
00125 
00126     bool HashFile(char *szFileName);
00127 #endif
00128 
00129     // Finalize hash and report
00130     void Final();
00131 
00132     // Report functions: as pre-formatted and raw data
00133 #ifdef SHA1_UTILITY_FUNCTIONS
00134 
00135     void ReportHash(char *szReport, unsigned char uReportType = REPORT_HEX);
00136 #endif
00137 
00138     void GetHash(UINT_8 *puDest);
00139 
00140 private:
00141     // Private SHA-1 transformation
00142     void Transform(UINT_32 *state, UINT_8 *buffer);
00143 
00144     // Member variables
00145     UINT_8 m_workspace[64];
00146     SHA1_WORKSPACE_BLOCK *m_block; // SHA1 pointer to the byte array above
00147 };
00148 
00149 #endif
Generated on Wed May 26 16:21:15 2010 for OverSim by  doxygen 1.6.3