SHA1I.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 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 \*---------------------------------------------------------------------------*/
28 
29 #include "SHA1.H"
30 #include <cstring>
31 
32 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
33 
35 {
36  clear();
37 }
38 
39 
40 inline Foam::SHA1::SHA1(const char* str)
41 {
42  clear();
43  append(str);
44 }
45 
46 
47 inline Foam::SHA1::SHA1(const std::string& str)
48 {
49  clear();
50  append(str);
51 }
52 
53 
54 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
55 
56 inline Foam::SHA1& Foam::SHA1::append(const char* data, size_t len)
57 {
58  processBytes(data, len);
59  return *this;
60 }
61 
62 
63 inline Foam::SHA1& Foam::SHA1::append(const char* str)
64 {
65  if (str && *str)
66  {
67  processBytes(str, strlen(str));
68  }
69  return *this;
70 }
71 
72 
73 inline Foam::SHA1& Foam::SHA1::append(const std::string& str)
74 {
75  processBytes(str.data(), str.size());
76  return *this;
77 }
78 
79 
81 (
82  const std::string& str,
83  size_t pos,
84  size_t len
85 )
86 {
87  if (std::string::npos != pos && pos < str.length())
88  {
89  if (std::string::npos == len || pos + len > str.length())
90  {
91  len = str.length() - pos;
92  }
93 
94  processBytes(str.data() + pos, len);
95  }
96 
97  return *this;
98 }
99 
100 
102 {
103  SHA1Digest dig;
104 
105  if (finalized_)
106  {
107  calcDigest(dig);
108  }
109  else
110  {
111  // Avoid disturbing current data - use a copy
112  SHA1 sha(*this);
113  if (sha.finalize())
114  {
115  sha.calcDigest(dig);
116  }
117  }
118 
119  return dig;
120 }
121 
122 
123 inline std::string Foam::SHA1::str(const bool prefixed) const
124 {
125  return digest().str(prefixed);
126 }
127 
128 
129 inline Foam::Ostream& Foam::SHA1::write(Ostream& os, const bool prefixed) const
130 {
131  return digest().write(os, prefixed);
132 }
133 
134 
135 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
136 
137 inline Foam::SHA1::operator Foam::SHA1Digest() const
138 {
139  return digest();
140 }
141 
142 
143 inline bool Foam::SHA1::operator==(const SHA1& rhs) const
144 {
145  return this->digest() == rhs.digest();
146 }
147 
148 
149 inline bool Foam::SHA1::operator==(const SHA1Digest& dig) const
150 {
151  return this->digest() == dig;
152 }
153 
154 
155 inline bool Foam::SHA1::operator==(const std::string& hexdigits) const
156 {
157  return this->digest() == hexdigits;
158 }
159 
160 
161 inline bool Foam::SHA1::operator==(const char* hexdigits) const
162 {
163  return this->digest() == hexdigits;
164 }
165 
166 
167 inline bool Foam::SHA1::operator!=(const SHA1& rhs) const
168 {
169  return !this->operator==(rhs);
170 }
171 
172 
173 inline bool Foam::SHA1::operator!=(const SHA1Digest& rhs) const
174 {
175  return !this->operator==(rhs);
176 }
177 
178 
179 inline bool Foam::SHA1::operator!=(const std::string& rhs) const
180 {
181  return !this->operator==(rhs);
182 }
183 
184 
185 inline bool Foam::SHA1::operator!=(const char* rhs) const
186 {
187  return !this->operator==(rhs);
188 }
189 
190 
191 // * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * //
192 
193 inline Foam::Ostream& Foam::operator<<(Ostream& os, const SHA1& sha)
194 {
195  return sha.write(os);
196 }
197 
198 
199 // ************************************************************************* //
Foam::SHA1::append
SHA1 & append(const char *str)
Append data for processing.
Definition: SHA1I.H:63
Foam::SHA1::str
std::string str(const bool prefixed=false) const
The digest (40-byte) text representation, optionally with '_' prefix.
Definition: SHA1I.H:123
Foam::SHA1
Functions to compute SHA1 message digest according to the NIST specification FIPS-180-1.
Definition: SHA1.H:60
append
rAUs append(new volScalarField(IOobject::groupName("rAU", phase1.name()), 1.0/(U1Eqn.A()+byDt(max(phase1.residualAlpha() - alpha1, scalar(0)) *rho1))))
Foam::SHA1::operator==
bool operator==(const SHA1 &rhs) const
Equality operator, compares digests.
Definition: SHA1I.H:143
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Foam::operator==
tmp< faMatrix< Type > > operator==(const faMatrix< Type > &, const faMatrix< Type > &)
Foam::SHA1::write
Ostream & write(Ostream &os, const bool prefixed=false) const
Write digest (40-byte) representation, optionally with '_' prefix.
Definition: SHA1I.H:129
Foam::Ostream::write
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
Foam::SHA1::digest
SHA1Digest digest() const
Calculate digest from current data.
Definition: SHA1I.H:101
Foam::SHA1Digest
The SHA1 message digest.
Definition: SHA1Digest.H:60
clear
patchWriters clear()
Foam::SHA1::SHA1
SHA1()
Construct null.
Definition: SHA1I.H:34
Foam::SHA1::operator!=
bool operator!=(const SHA1 &) const
Inequality operator, compares digests.
Definition: SHA1I.H:167
Foam::SHA1::clear
void clear()
Reset the hashed data before appending more.
Definition: SHA1.C:315
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
SHA1.H
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