SHA1.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2021 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::SHA1
26 
27 Description
28  Functions to compute SHA1 message digest according to the NIST
29  specification FIPS-180-1.
30 
31  Adapted from the gnulib implementation.
32 
33 See also
34  Foam::SHA1Digest
35 
36 SourceFiles
37  SHA1I.H
38  SHA1.C
39 
40 \*---------------------------------------------------------------------------*/
41 
42 #ifndef SHA1_H
43 #define SHA1_H
44 
45 #include <string>
46 #include <cstddef>
47 
48 #include "int.H"
49 #include "SHA1Digest.H"
50 
51 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
52 
53 namespace Foam
54 {
55 
56 // Forward declaration of classes
57 class Ostream;
58 
59 // Forward declaration of friend functions and operators
60 class SHA1;
61 class SHA1Digest;
62 Ostream& operator<<(Ostream&, const SHA1&);
63 
64 
65 /*---------------------------------------------------------------------------*\
66  Class SHA1 Declaration
67 \*---------------------------------------------------------------------------*/
68 
69 class SHA1
70 {
71  // Private Data
72 
73  //- Track if the hashsum has been finalised (added count, etc)
74  bool finalised_;
75 
76  //- The hash sums
77  uint32_t hashsumA_;
78  uint32_t hashsumB_;
79  uint32_t hashsumC_;
80  uint32_t hashsumD_;
81  uint32_t hashsumE_;
82 
83  //- The total number processed, saved as 64-bit
84  uint32_t bufTotal_[2];
85 
86  //- The number of elements pending in the buffer
87  uint32_t bufLen_;
88 
89  //- The input processing buffer
90  uint32_t buffer_[32];
91 
92  // Private Member Functions
93 
94  //- Swap bytes from internal to network (big-endian) order
95  static inline uint32_t swapBytes(uint32_t);
96 
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);
101 
102  //- Process data block-wise, LEN must be a multiple of 64!
103  void processBlock(const void *data, size_t len);
104 
105  //- Process for the next LEN bytes, LEN need not be a multiple of 64.
106  void processBytes(const void *data, size_t len);
107 
108  //- Calculate current digest from appended data.
109  void calcDigest(SHA1Digest&) const;
110 
111 public:
112 
113  // Constructors
114 
115  //- Construct null
116  inline SHA1();
117 
118  //- Construct null and append initial std::string
119  explicit inline SHA1(const std::string&);
120 
121  //- Construct null and append initial string
122  explicit inline SHA1(const char*);
123 
124  // Member Functions
125 
126  //- Reset the hashed data before appending more
127  void clear();
128 
129  //- Append data for processing
130  inline SHA1& append(const char* data, size_t len);
131 
132  //- Append string for processing
133  inline SHA1& append(const std::string&);
134 
135  //- Append string for processing
136  inline SHA1& append(const char* str);
137 
138  //- Finalised the calculations (normally not needed directly).
139  // Returns false if no bytes were passed for processing
140  bool finalise();
141 
142  //- Calculate current digest from appended data.
143  SHA1Digest digest() const;
144 
145 
146  // Member Operators
147 
148  //- Equality operator, compares %digests
149  inline bool operator==(const SHA1&) const;
150 
151  //- Compare %digest
152  inline bool operator==(const SHA1Digest&) const;
153 
154  //- Compare %digest to (40-byte) text representation (eg, from sha1sum)
155  // An %empty string is equivalent to
156  // "0000000000000000000000000000000000000000"
157  inline bool operator==(const std::string& hexdigits) const;
158 
159  //- Compare %digest to (40-byte) text representation (eg, from sha1sum)
160  // A %null or %empty string is equivalent to
161  // "0000000000000000000000000000000000000000"
162  inline bool operator==(const char* hexdigits) const;
163 
164 
165  //- Inequality operator, compares %digests
166  inline bool operator!=(const SHA1&) const;
167 
168  //- Inequality operator, compare %digest
169  inline bool operator!=(const SHA1Digest&) const;
170 
171  //- Inequality operator, compares %digests
172  inline bool operator!=(const std::string& hexdigits) const;
173 
174  //- Inequality operator, compare %digest
175  inline bool operator!=(const char* hexdigits) const;
176 
177 
178  //- Convert to a SHA1Digest,
179  // calculate current %digest from appended data
180  inline operator SHA1Digest() const;
181 
182 
183  // Friend Operators
184 
185  //- Output the %digest
186  inline friend Ostream& operator<<(Ostream&, const SHA1&);
187 
188 };
189 
190 
191 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
192 
193 } // End namespace Foam
194 
195 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
196 
197 #include "SHA1I.H"
198 
199 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
200 
201 #endif
202 
203 // ************************************************************************* //
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
The SHA1 message digest.
Definition: SHA1Digest.H:63
Functions to compute SHA1 message digest according to the NIST specification FIPS-180-1.
Definition: SHA1.H:69
SHA1 & append(const char *data, size_t len)
Append data for processing.
Definition: SHA1I.H:53
bool operator==(const SHA1 &) const
Equality operator, compares digests.
Definition: SHA1I.H:79
bool finalise()
Finalised the calculations (normally not needed directly).
Definition: SHA1.C:354
friend Ostream & operator<<(Ostream &, const SHA1 &)
Output the digest.
bool operator!=(const SHA1 &) const
Inequality operator, compares digests.
Definition: SHA1I.H:103
SHA1()
Construct null.
Definition: SHA1I.H:31
void clear()
Reset the hashed data before appending more.
Definition: SHA1.C:339
SHA1Digest digest() const
Calculate current digest from appended data.
Definition: SHA1.C:393
System integer.
Namespace for OpenFOAM.
bool cp(const fileName &src, const fileName &dst, const bool followLink=true)
Copy, recursively if necessary, the source to the destination.
Definition: POSIX.C:753
Ostream & operator<<(Ostream &os, const fvConstraints &constraints)