SHA1Digest.C
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-2015 OpenFOAM Foundation
9 Copyright (C) 2019-2022 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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\*---------------------------------------------------------------------------*/
28
29#include "SHA1Digest.H"
30#include "IOstreams.H"
31#include <cstring>
32
33// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34
36
37static const char hexChars[] = "0123456789abcdef";
38
39// The char '0' == 0
40static constexpr int offsetZero = int('0');
41
42// The char 'A' (or 'a') == 10
43static constexpr int offsetAlpha = int('A') - 10;
44
45
46// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
47
48namespace Foam
49{
50
51// Read hexadecimal value, ignoring leading or intermediate '_'
52static unsigned char readHexDigit(Istream& is)
53{
54 // Silently ignore leading or intermediate '_'
55 char c = 0;
56 do
57 {
58 is.read(c);
59 }
60 while (c == '_');
61
62 if (isdigit(c))
63 {
64 return int(c) - offsetZero;
65 }
66 else if (!isxdigit(c))
67 {
69 << "Illegal hex digit: '" << c << "'"
71 }
72
73 return toupper(c) - offsetAlpha;
74}
75
76} // End namespace Foam
77
78
79// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
80
81bool Foam::SHA1Digest::isEqual(const char* hexdigits, std::size_t len) const
82{
83 // Skip possible '_' prefix
84 if (*hexdigits == '_')
85 {
86 ++hexdigits;
87 --len;
88 }
89
90 // Incorrect length - can never match
91 if (len != 2*dig_.size())
92 {
93 return false;
94 }
95
96 for (const auto& byteVal : dig_)
97 {
98 const char upp = hexChars[((byteVal >> 4) & 0xF)];
99 const char low = hexChars[(byteVal & 0xF)];
100
101 if (upp != *hexdigits++) return false;
102 if (low != *hexdigits++) return false;
103 }
104
105 return true;
106}
107
108
109// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
110
112{
113 clear();
114}
115
116
118{
119 clear();
120 read(is);
121}
122
123
124// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
125
127{
128 dig_.fill(0); // Same as memset(dig_.data(), 0, dig_.size());
129}
130
131
133{
134 for (const auto& byteVal : dig_)
135 {
136 if (byteVal)
137 {
138 return false;
139 }
140 }
141
142 return true;
143}
144
145
147{
148 for (auto& byteVal : dig_)
149 {
150 const unsigned char upp = readHexDigit(is);
151 const unsigned char low = readHexDigit(is);
152
153 byteVal = (upp << 4) + low;
154 }
155
157 return is;
158}
159
160
161std::string Foam::SHA1Digest::str(const bool prefixed) const
162{
163 std::string buf;
164 std::size_t nChar = 0;
165
166 if (prefixed)
167 {
168 buf.resize(1 + 2*dig_.size());
169 buf[nChar++] = '_';
170 }
171 else
172 {
173 buf.resize(2*dig_.size());
174 }
175
176 for (const auto& byteVal : dig_)
177 {
178 buf[nChar++] = hexChars[((byteVal >> 4) & 0xF)]; // Upper nibble
179 buf[nChar++] = hexChars[(byteVal & 0xF)]; // Lower nibble
180 }
181
182 return buf;
183}
184
185
186Foam::Ostream& Foam::SHA1Digest::write(Ostream& os, const bool prefixed) const
187{
188 if (prefixed)
189 {
190 os.write('_');
191 }
192
193 for (const auto& byteVal : dig_)
194 {
195 os.write(hexChars[((byteVal >> 4) & 0xF)]); // Upper nibble
196 os.write(hexChars[(byteVal & 0xF)]); // Lower nibble
197 }
198
200 return os;
201}
202
203
204// * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * * //
205
207{
208 return (dig_ == rhs.dig_);
209}
210
211
212bool Foam::SHA1Digest::operator==(const std::string& hexdigits) const
213{
214 // Null or empty string is not an error - interpret as '0000..'
215 if (hexdigits.empty())
216 {
217 return empty();
218 }
219
220 return isEqual(hexdigits.data(), hexdigits.length());
221}
222
223
224bool Foam::SHA1Digest::operator==(const char* hexdigits) const
225{
226 // Null or empty string is not an error - interpret as '0000..'
227 if (!hexdigits || !*hexdigits)
228 {
229 return empty();
230 }
231
232 return isEqual(hexdigits, std::char_traits<char>::length(hexdigits));
233}
234
235
237{
238 return !this->operator==(rhs);
239}
240
241
242bool Foam::SHA1Digest::operator!=(const std::string& hexdigits) const
243{
244 return !this->operator==(hexdigits);
245}
246
247
248bool Foam::SHA1Digest::operator!=(const char* hexdigits) const
249{
250 return !this->operator==(hexdigits);
251}
252
253
254// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
255
257{
258 return dig.read(is);
259}
260
261
263{
264 // Write with prefixed = false
265 return dig.write(os, false);
266}
267
268
269// ************************************************************************* //
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
static const char hexChars[]
Definition: SHA1Digest.C:37
static constexpr int offsetAlpha
Definition: SHA1Digest.C:43
static constexpr int offsetZero
Definition: SHA1Digest.C:40
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:58
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
virtual Istream & read(token &)=0
Return next token from stream.
virtual Ostream & write(const char c)
Write character.
Definition: OBJstream.C:78
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
virtual bool read()
Re-read model coefficients if they have changed.
The SHA1 message digest.
Definition: SHA1Digest.H:61
bool empty() const
Return true if the digest is empty (ie, all zero).
Definition: SHA1Digest.C:132
SHA1Digest()
Default construct a zero digest.
Definition: SHA1Digest.C:111
Ostream & write(Ostream &os, const bool prefixed=false) const
Write (40-byte) text representation, optionally with '_' prefix.
Definition: SHA1Digest.C:186
void clear()
Reset the digest to zero.
Definition: SHA1Digest.C:126
static const SHA1Digest null
A null digest (ie, all zero)
Definition: SHA1Digest.H:88
Istream & read(Istream &is)
Read (40-byte) text representation.
Definition: SHA1Digest.C:146
virtual bool write()
Write the output fields.
friend bool operator!=(const refineCell &rc1, const refineCell &rc2)
Definition: refineCell.H:106
friend bool operator==(const refineCell &rc1, const refineCell &rc2)
Definition: refineCell.H:97
const word & str() const
The string representation of the volume type enumeration.
Definition: volumeType.C:62
patchWriters clear()
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
OBJstream os(runTime.globalPath()/outputName)
#define FUNCTION_NAME
Namespace for OpenFOAM.
bool read(const char *buf, int32_t &val)
Same as readInt32.
Definition: int32.H:108
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
tmp< faMatrix< Type > > operator==(const faMatrix< Type > &, const faMatrix< Type > &)
Istream & operator>>(Istream &, directionInfo &)
IOerror FatalIOError
static unsigned char readHexDigit(Istream &is)
Definition: SHA1Digest.C:52
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130