00001 /*---------------------------------------------------------------------------*\ 00002 ========= | 00003 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox 00004 \\ / O peration | 00005 \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd. 00006 \\/ M anipulation | 00007 ------------------------------------------------------------------------------- 00008 License 00009 This file is part of OpenFOAM. 00010 00011 OpenFOAM is free software: you can redistribute it and/or modify it 00012 under the terms of the GNU General Public License as published by 00013 the Free Software Foundation, either version 3 of the License, or 00014 (at your option) any later version. 00015 00016 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT 00017 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00018 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 00019 for more details. 00020 00021 You should have received a copy of the GNU General Public License 00022 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>. 00023 00024 Class 00025 Foam::SHA1 00026 00027 Description 00028 Functions to compute SHA1 message digest according to the NIST 00029 specification FIPS-180-1. 00030 00031 Adapted from the gnulib implementation. 00032 00033 SeeAlso 00034 Foam::SHA1Digest 00035 00036 SourceFiles 00037 SHA1I.H 00038 SHA1.C 00039 00040 \*---------------------------------------------------------------------------*/ 00041 00042 #ifndef SHA1_H 00043 #define SHA1_H 00044 00045 #include <string> 00046 #include <cstddef> 00047 #include <stdint.h> // C++0x uses <cstdint> 00048 00049 #include "SHA1Digest.H" 00050 00051 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // 00052 00053 namespace Foam 00054 { 00055 00056 // Forward declaration of classes 00057 class Ostream; 00058 00059 // Forward declaration of friend functions and operators 00060 class SHA1; 00061 class SHA1Digest; 00062 Ostream& operator<<(Ostream&, const SHA1&); 00063 00064 00065 /*---------------------------------------------------------------------------*\ 00066 Class SHA1 Declaration 00067 \*---------------------------------------------------------------------------*/ 00068 00069 class SHA1 00070 { 00071 // Private data 00072 00073 //- Track if the hashsum has been finalized (added count, etc) 00074 bool finalized_; 00075 00076 //- The hash sums 00077 uint32_t hashsumA_; 00078 uint32_t hashsumB_; 00079 uint32_t hashsumC_; 00080 uint32_t hashsumD_; 00081 uint32_t hashsumE_; 00082 00083 //- The total number processed, saved as 64-bit 00084 uint32_t bufTotal_[2]; 00085 00086 //- The number of elements pending in the buffer 00087 uint32_t bufLen_; 00088 00089 //- The input processing buffer 00090 uint32_t buffer_[32]; 00091 00092 // Private Member Functions 00093 00094 //- Swap bytes from internal to network (big-endian) order 00095 static inline uint32_t swapBytes(uint32_t); 00096 00097 //- Copy the 4-byte value into the memory location pointed to by *dst. 00098 // If the architecture allows unaligned access this is equivalent to 00099 // *(uint32_t *) cp = val 00100 static inline void set_uint32(unsigned char *cp, uint32_t); 00101 00102 //- Process data block-wise, LEN must be a multiple of 64! 00103 void processBlock(const void *data, size_t len); 00104 00105 //- Process for the next LEN bytes, LEN need not be a multiple of 64. 00106 void processBytes(const void *data, size_t len); 00107 00108 //- Calculate current digest from appended data. 00109 void calcDigest(SHA1Digest&) const; 00110 00111 public: 00112 00113 // Constructors 00114 00115 //- Construct null 00116 inline SHA1(); 00117 00118 //- Construct and append initial std::string 00119 explicit inline SHA1(const std::string&); 00120 00121 //- Construct and append initial string 00122 explicit inline SHA1(const char*); 00123 00124 // Member Functions 00125 00126 //- Reset the hashed data before appending more 00127 void clear(); 00128 00129 //- Append data for processing 00130 inline SHA1& append(const char* data, size_t len); 00131 00132 //- Append string for processing 00133 inline SHA1& append(const std::string&); 00134 00135 //- Append string for processing 00136 inline SHA1& append(const char* str); 00137 00138 //- Finalized the calculations (normally not needed directly). 00139 // Returns false if no bytes were passed for processing 00140 bool finalize(); 00141 00142 //- Calculate current digest from appended data. 00143 SHA1Digest digest() const; 00144 00145 00146 // Member Operators 00147 00148 //- Equality operator 00149 inline bool operator==(const SHA1Digest&) const; 00150 00151 //- Inequality operator 00152 inline bool operator!=(const SHA1Digest&) const; 00153 00154 //- Equality operator 00155 inline bool operator==(const SHA1&) const; 00156 00157 //- Inequality operator 00158 inline bool operator!=(const SHA1&) const; 00159 00160 //- Convert to a digest, calculate current digest from appended data. 00161 inline operator SHA1Digest() const; 00162 00163 // Friend Functions 00164 00165 // Friend Operators 00166 00167 inline friend Ostream& operator<<(Ostream&, const SHA1&); 00168 00169 }; 00170 00171 00172 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // 00173 00174 } // End namespace Foam 00175 00176 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // 00177 00178 #include "SHA1I.H" 00179 00180 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // 00181 00182 #endif 00183 00184 // ************************ vim: set sw=4 sts=4 et: ************************ //