HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SHA1.h
Go to the documentation of this file.
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 // clang-format off
74 
75 #ifndef ___SHA1_HDR___
76 #define ___SHA1_HDR___
77 
78 #include <climits>
79 
80 #include <OpenImageIO/export.h>
82 #include <OpenImageIO/platform.h>
83 
84 #if !defined(SHA1_UTILITY_FUNCTIONS) && !defined(SHA1_NO_UTILITY_FUNCTIONS)
85 #define SHA1_UTILITY_FUNCTIONS
86 #endif
87 
88 #if !defined(SHA1_STL_FUNCTIONS) && !defined(SHA1_NO_STL_FUNCTIONS)
89 #define SHA1_STL_FUNCTIONS
90 #if !defined(SHA1_UTILITY_FUNCTIONS)
91 #error STL functions require SHA1_UTILITY_FUNCTIONS.
92 #endif
93 #endif
94 
95 #include <memory.h>
96 
97 #ifdef SHA1_UTILITY_FUNCTIONS
98 #include <cstdio>
99 #include <cstring>
100 #endif
101 
102 #ifdef SHA1_STL_FUNCTIONS
103 #include <string>
104 #endif
105 
106 #ifdef _MSC_VER
107 #include <stdlib.h>
108 #endif
109 
110 // You can define the endian mode in your files without modifying the SHA-1
111 // source files. Just #define SHA1_LITTLE_ENDIAN or #define SHA1_BIG_ENDIAN
112 // in your files, before including the SHA1.h header file. If you don't
113 // define anything, the class defaults to little endian.
114 #if !defined(SHA1_LITTLE_ENDIAN) && !defined(SHA1_BIG_ENDIAN)
115 #define SHA1_LITTLE_ENDIAN
116 #endif
117 
118 // If you want variable wiping, #define SHA1_WIPE_VARIABLES, if not,
119 // #define SHA1_NO_WIPE_VARIABLES. If you don't define anything, it
120 // defaults to wiping.
121 #if !defined(SHA1_WIPE_VARIABLES) && !defined(SHA1_NO_WIPE_VARIABLES)
122 #define SHA1_WIPE_VARIABLES
123 #endif
124 
125 #ifdef _MSC_VER
126 #define _sntprintf std::snprintf
127 #else
128 #define _sntprintf std::snprintf
129 #endif
130 
131 // Fallback, if no 64-bit support
132 #ifndef _fseeki64
133 #define _fseeki64 fseek
134 #endif
135 #ifndef _ftelli64
136 #define _ftelli64 ftell
137 #endif
138 
139 ///////////////////////////////////////////////////////////////////////////
140 // Define variable types
141 
142 #define UINT_8 uint8_t
143 #define UINT_32 uint32_t
144 #define UINT_64 uint64_t
145 #define INT_64 int64_t
146 
147 
148 ///////////////////////////////////////////////////////////////////////////
149 // Declare SHA-1 workspace
150 
152 
153 typedef union
154 {
155  UINT_8 c[64];
156  UINT_32 l[16];
158 
159 class CSHA1
160 {
161 public:
162 #ifdef SHA1_UTILITY_FUNCTIONS
163  // Different formats for ReportHash
165  {
169  };
170 #endif
171 
172  // Constructor and destructor
173  CSHA1();
174  ~CSHA1();
175 
178  UINT_32 m_reserved0[1]; // Memory alignment padding
181  UINT_32 m_reserved1[3]; // Memory alignment padding
182 
183  void Reset();
184 
185  // Update the hash value
186  void Update(const UINT_8* pbData, UINT_32 uLen);
187 
188 #ifdef SHA1_UTILITY_FUNCTIONS
189  // Hash in file contents
190  bool HashFile(const char* szFileName);
191 #endif
192 
193  // Finalize hash, call before using ReportHash(Stl)
194  void Final();
195 
196 #ifdef SHA1_UTILITY_FUNCTIONS
197  bool ReportHash(char* szReport, REPORT_TYPE rtReportType = REPORT_HEX) const;
198 #endif
199 
200 #ifdef SHA1_STL_FUNCTIONS
201  bool ReportHashStl(std::string& strOut, REPORT_TYPE rtReportType =
202  REPORT_HEX) const;
203 #endif
204 
205  bool GetHash(UINT_8* pbDest) const;
206 
207 private:
208  // Private SHA-1 transformation
209  void Transform(UINT_32* pState, const UINT_8* pBuffer);
210 
211  // Member variables
212  UINT_8 m_workspace[64];
213  SHA1_WORKSPACE_BLOCK* m_block; // SHA1 pointer to the byte array above
214 };
215 
217 
218 #endif // ___SHA1_HDR___
UINT_32 m_state[5]
Definition: SHA1.h:176
UINT_32 m_reserved1[3]
Definition: SHA1.h:181
#define UINT_32
Definition: SHA1.h:143
GLdouble l
Definition: glew.h:9122
REPORT_TYPE
Definition: SHA1.h:164
UINT_32 m_count[2]
Definition: SHA1.h:177
Platform-related macros.
bool ReportHash(char *szReport, REPORT_TYPE rtReportType=REPORT_HEX) const
#define UINT_8
Definition: SHA1.h:142
const GLfloat * c
Definition: glew.h:16296
void Final()
void Reset()
UINT_32 m_reserved0[1]
Definition: SHA1.h:178
UINT_8 m_digest[20]
Definition: SHA1.h:180
UINT_8 m_buffer[64]
Definition: SHA1.h:179
void Update(const UINT_8 *pbData, UINT_32 uLen)
bool ReportHashStl(std::string &strOut, REPORT_TYPE rtReportType=REPORT_HEX) const
GLsizei const GLchar *const * string
Definition: glew.h:1844
bool HashFile(const char *szFileName)
Definition: SHA1.h:159
bool GetHash(UINT_8 *pbDest) const
#define OIIO_NAMESPACE_END
Definition: oiioversion.h:66
#define OIIO_NAMESPACE_BEGIN
Definition: oiioversion.h:65