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 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 SHA1_H
46 #define 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  //- Construct null
102  inline SHA1();
103 
104  //- Construct null and append initial string
105  inline explicit SHA1(const char* str);
106 
107  //- Construct null and append initial std::string
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 data for processing
117  inline SHA1& append(const char* str);
118 
119  //- Append data for processing
120  inline SHA1& append(const char* data, size_t len);
121 
122  //- Append string for processing
123  inline SHA1& append(const std::string& str);
124 
125  //- Append substring for processing
126  inline SHA1& append
127  (
128  const std::string& str,
129  size_t pos,
130  size_t len = std::string::npos
131  );
132 
133  //- Finalized the calculations (normally not needed directly).
134  // Returns false if no bytes were passed for processing
135  bool finalize();
136 
137  //- Calculate digest from current data.
138  inline SHA1Digest digest() const;
139 
140  //- The digest (40-byte) text representation, optionally with '_' prefix
141  inline std::string str(const bool prefixed=false) const;
142 
143  //- Write digest (40-byte) representation, optionally with '_' prefix
144  inline Ostream& write(Ostream& os, const bool prefixed=false) const;
145 
146 
147  // Member Operators
148 
149  //- Cast conversion to a SHA1Digest,
150  // calculates the %digest from the current data
151  inline operator SHA1Digest() const;
152 
153  //- Equality operator, compares %digests
154  inline bool operator==(const SHA1& rhs) const;
155 
156  //- Compare %digest
157  inline bool operator==(const SHA1Digest& dig) const;
158 
159  //- Compare %digest to (40-byte) text representation (eg, from sha1sum)
160  // An %empty string is equivalent to
161  // "0000000000000000000000000000000000000000"
162  inline bool operator==(const std::string& hexdigits) const;
163 
164  //- Compare %digest to (40-byte) text representation (eg, from sha1sum)
165  // A %null or %empty string is equivalent to
166  // "0000000000000000000000000000000000000000"
167  inline bool operator==(const char* hexdigits) const;
168 
169  //- Inequality operator, compares %digests
170  inline bool operator!=(const SHA1&) const;
171 
172  //- Inequality operator, compare %digest
173  inline bool operator!=(const SHA1Digest&) const;
174 
175  //- Inequality operator, compares %digests
176  inline bool operator!=(const std::string& hexdigits) const;
177 
178  //- Inequality operator, compare %digest
179  inline bool operator!=(const char* hexdigits) const;
180 };
181 
182 
183 // IOstream Operators
184 
185 //- Output the %digest
186 inline Ostream& operator<<(Ostream& os, const SHA1& sha);
187 
188 
189 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
190 
191 } // End namespace Foam
192 
193 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
194 
195 #include "SHA1I.H"
196 
197 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
198 
199 #endif
200 
201 // ************************************************************************* //
Foam::SHA1::clear
void clear() noexcept
Reset the hashed data before appending more.
Definition: SHA1.C:315
Foam::SHA1::append
SHA1 & append(const char *str)
Append data for processing.
Definition: SHA1I.H:62
Foam::SHA1::str
std::string str(const bool prefixed=false) const
The digest (40-byte) text representation, optionally with '_' prefix.
Definition: SHA1I.H:122
Foam::SHA1
Functions to compute SHA1 message digest according to the NIST specification FIPS-180-1.
Definition: SHA1.H:60
Foam::SHA1::operator==
bool operator==(const SHA1 &rhs) const
Equality operator, compares digests.
Definition: SHA1I.H:142
SHA1I.H
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
SHA1Digest.H
Foam::SHA1::write
Ostream & write(Ostream &os, const bool prefixed=false) const
Write digest (40-byte) representation, optionally with '_' prefix.
Definition: SHA1I.H:128
os
OBJstream os(runTime.globalPath()/outputName)
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::SHA1::digest
SHA1Digest digest() const
Calculate digest from current data.
Definition: SHA1I.H:100
Foam::SHA1Digest
The SHA1 message digest.
Definition: SHA1Digest.H:60
Foam::SHA1::SHA1
SHA1()
Construct null.
Definition: SHA1I.H:33
Foam::SHA1::operator!=
bool operator!=(const SHA1 &) const
Inequality operator, compares digests.
Definition: SHA1I.H:166
Foam::SHA1::finalize
bool finalize()
Finalized the calculations (normally not needed directly).
Definition: SHA1.C:330
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::data
Database for solution data, solver performance and other reduced data.
Definition: data.H:55
Foam::pos
dimensionedScalar pos(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:177