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