1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | foam-extend: Open Source CFD
5 \\ / A nd | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
9 This file is part of foam-extend.
11 foam-extend is free software: you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by the
13 Free Software Foundation, either version 3 of the License, or (at your
14 option) any later version.
16 foam-extend is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with foam-extend. If not, see <http://www.gnu.org/licenses/>.
28 Functions to compute SHA1 message digest according to the NIST
29 specification FIPS-180-1.
31 Adapted from the gnulib implementation.
40 \*---------------------------------------------------------------------------*/
47 #include <stdint.h> // C++0x uses <cstdint>
49 #include "SHA1Digest.H"
51 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
56 // Forward declaration of classes
59 // Forward declaration of friend functions and operators
62 Ostream& operator<<(Ostream&, const SHA1&);
65 /*---------------------------------------------------------------------------*\
66 Class SHA1 Declaration
67 \*---------------------------------------------------------------------------*/
73 //- Track if the hashsum has been finalized (added count, etc)
83 //- The total number processed, saved as 64-bit
84 uint32_t bufTotal_[2];
86 //- The number of elements pending in the buffer
89 //- The input processing buffer
92 // Private Member Functions
94 //- Swap bytes from internal to network (big-endian) order
95 static inline uint32_t swapBytes(uint32_t);
97 //- Copy the 4-byte value into the memory location pointed to by *dst.
98 // If the architecture allows unaligned access this is equivalent to
99 // *(uint32_t *) cp = val
100 static inline void set_uint32(unsigned char *cp, uint32_t);
102 //- Process data block-wise, LEN must be a multiple of 64!
103 void processBlock(const void *data, size_t len);
105 //- Process for the next LEN bytes, LEN need not be a multiple of 64.
106 void processBytes(const void *data, size_t len);
108 //- Calculate current digest from appended data.
109 void calcDigest(SHA1Digest&) const;
118 //- Construct and append initial std::string
119 explicit inline SHA1(const std::string&);
121 //- Construct and append initial string
122 explicit inline SHA1(const char*);
126 //- Reset the hashed data before appending more
129 //- Append data for processing
130 inline SHA1& append(const char* data, size_t len);
132 //- Append string for processing
133 inline SHA1& append(const std::string&);
135 //- Append string for processing
136 inline SHA1& append(const char* str);
138 //- Finalized the calculations (normally not needed directly).
139 // Returns false if no bytes were passed for processing
142 //- Calculate current digest from appended data.
143 SHA1Digest digest() const;
148 //- Equality operator
149 inline bool operator==(const SHA1Digest&) const;
151 //- Inequality operator
152 inline bool operator!=(const SHA1Digest&) const;
154 //- Equality operator
155 inline bool operator==(const SHA1&) const;
157 //- Inequality operator
158 inline bool operator!=(const SHA1&) const;
160 //- Convert to a digest, calculate current digest from appended data.
161 inline operator SHA1Digest() const;
167 inline friend Ostream& operator<<(Ostream&, const SHA1&);
172 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
174 } // End namespace Foam
176 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
180 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
184 // ************************************************************************* //