SHA1Digest.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-2025 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::SHA1Digest
29 
30 Description
31  The SHA1 message digest.
32 
33 See also
34  Foam::SHA1
35 
36 SourceFiles
37  SHA1Digest.C
38 
39 \*---------------------------------------------------------------------------*/
40 
41 #ifndef Foam_SHA1Digest_H
42 #define Foam_SHA1Digest_H
43 
44 #include <array>
45 #include <string>
46 #include "contiguous.H"
47 
48 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
49 
50 namespace Foam
51 {
52 
53 // Forward Declarations
54 class Istream;
55 class Ostream;
56 class SHA1;
57 
58 /*---------------------------------------------------------------------------*\
59  Class SHA1Digest Declaration
60 \*---------------------------------------------------------------------------*/
61 
62 class SHA1Digest
63 {
64  // Private Data
65 
66  //- The digest contents, which has 20 (uncoded) bytes
67  std::array<unsigned char, 20> dig_;
68 
69 
70  // Private Member Functions
71 
72  // Permit SHA1 to calculate the digest
73  friend class SHA1;
74 
75  //- Raw digest data (20 bytes). Non-const access for SHA1
76  unsigned char* data() noexcept { return dig_.data(); }
77 
78 
79 public:
80 
81  // Static Data Members
82 
83  //- A null digest (ie, all zero)
84  static const SHA1Digest null;
85 
86 
87  // Constructors
88 
89  //- Default construct a zero digest
90  SHA1Digest();
91 
92  //- Read construct a digest from stringified content
93  explicit SHA1Digest(Istream& is);
94 
95  //- Construct digest from raw or stringified content.
96  //- The length is 20 for raw digest content and 40 (or 41) for
97  //- stringified versions.
98  SHA1Digest(const char* content, std::size_t len);
99 
100  //- Construct digest from raw or stringified content.
101  //- The length is 20 for raw digest content and 40 (or 41) for
102  //- stringified versions.
103  SHA1Digest(const unsigned char* content, std::size_t len);
104 
105 
106  // Member Functions
107 
108  //- Reset the digest to zero
109  void clear();
110 
111  //- Return true if the digest is empty (ie, all zero).
112  bool empty() const;
113 
114  //- Return (40-byte) text representation, optionally with '_' prefix
115  std::string str(const bool prefixed=false) const;
116 
117  //- Read (40-byte) text representation.
118  // Since leading and intermediate underscores are skipped, a '_' can
119  // be prefixed to the text representation to use an unquoted
120  // SHA1Digest without parsing ambiguities as a number.
121  Istream& read(Istream& is);
122 
123  //- Write (40-byte) text representation, optionally with '_' prefix
124  Ostream& write(Ostream& os, const bool prefixed=false) const;
125 
126 
127  // Low-level access
128 
129  //- Raw digest data (20 bytes) - const access
130  const unsigned char* cdata() const noexcept { return dig_.data(); }
131 
132  //- Raw digest char data (20 bytes) - const access.
133  //- For consistency with other objects, these are \em not unsigned.
134  const char* cdata_bytes() const noexcept
135  {
136  return reinterpret_cast<const char*>(dig_.data());
137  }
138 
139  //- Raw digest char data (20 bytes) - non-const access.
140  //- For consistency with other objects, these are \em not unsigned.
141  //- Use with caution - generally for broadcasting only.
142  char* data_bytes() noexcept
143  {
144  return reinterpret_cast<char*>(dig_.data());
145  }
146 
147  //- The number of bytes in digest (20)
148  static constexpr unsigned size_bytes() noexcept { return 20; }
149 
150  //- The dimensioned size of the digest is always 20 bytes
151  static constexpr unsigned max_size() noexcept { return 20; }
153 
154  // Member Operators
155 
156  //- Equality operator
157  bool operator==(const SHA1Digest& rhs) const;
159  //- Compare to (40-byte) text representation (eg, from sha1sum)
160  // An %empty string is equivalent to
161  // "0000000000000000000000000000000000000000"
162  // The hexdigits may optionally start with a '_' prefix
163  bool operator==(const std::string& hexdigits) const;
164 
165  //- Compare to (40-byte) text representation (eg, from sha1sum)
166  // A %null or %empty string is equivalent to
167  // "0000000000000000000000000000000000000000"
168  // The hexdigits may optionally start with a '_' prefix
169  bool operator==(const char* hexdigits) const;
170 
171 
172  //- Inequality operator
173  bool operator!=(const SHA1Digest& rhs) const
174  {
175  return !(*this == rhs);
176  }
177 
178  //- Inequality operator
179  bool operator!=(const std::string& hexdigits) const
180  {
181  return !(*this == hexdigits);
182  }
183 
184  //- Inequality operator
185  bool operator!=(const char* hexdigits) const
186  {
187  return !(*this == hexdigits);
188  }
189 };
190 
191 
192 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
193 
194 // IOstream Operators
195 
196 //- Read (40-byte) text representation (ignoring leading '_' prefix)
197 Istream& operator>>(Istream& is, SHA1Digest& dig);
198 
199 //- Write (40-byte) text representation, unquoted and without prefix
200 Ostream& operator<<(Ostream& os, const SHA1Digest& dig);
201 
202 
203 // * * * * * * * * * * * * * * * * * Traits * * * * * * * * * * * * * * * * //
204 
205 //- Contiguous data for SHA1Digest
206 template<> struct is_contiguous<SHA1Digest> : std::true_type {};
207 
208 
209 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
210 
211 } // End namespace Foam
212 
213 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
214 
215 #endif
216 
217 // ************************************************************************* //
char * data_bytes() noexcept
Raw digest char data (20 bytes) - non-const access. For consistency with other objects, these are not unsigned. Use with caution - generally for broadcasting only.
Definition: SHA1Digest.H:168
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
The SHA1 message digest.
Definition: SHA1Digest.H:57
bool operator!=(const SHA1Digest &rhs) const
Inequality operator.
Definition: SHA1Digest.H:213
const char * cdata_bytes() const noexcept
Raw digest char data (20 bytes) - const access. For consistency with other objects, these are not unsigned.
Definition: SHA1Digest.H:158
Istream & operator>>(Istream &, directionInfo &)
SHA1Digest()
Default construct a zero digest.
Definition: SHA1Digest.C:158
const unsigned char * cdata() const noexcept
Raw digest data (20 bytes) - const access.
Definition: SHA1Digest.H:152
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
const direction noexcept
Definition: scalarImpl.H:255
bool empty() const
Return true if the digest is empty (ie, all zero).
Definition: SHA1Digest.C:193
OBJstream os(runTime.globalPath()/outputName)
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:77
Ostream & write(Ostream &os, const bool prefixed=false) const
Write (40-byte) text representation, optionally with &#39;_&#39; prefix.
Definition: SHA1Digest.C:247
void clear()
Reset the digest to zero.
Definition: SHA1Digest.C:187
bool operator==(const SHA1Digest &rhs) const
Equality operator.
Definition: SHA1Digest.C:267
static constexpr unsigned max_size() noexcept
The dimensioned size of the digest is always 20 bytes.
Definition: SHA1Digest.H:181
std::string str(const bool prefixed=false) const
Return (40-byte) text representation, optionally with &#39;_&#39; prefix.
Definition: SHA1Digest.C:222
static constexpr unsigned size_bytes() noexcept
The number of bytes in digest (20)
Definition: SHA1Digest.H:176
Functions to compute SHA1 message digest according to the NIST specification FIPS-180-1.
Definition: SHA1.H:56
Istream & read(Istream &is)
Read (40-byte) text representation.
Definition: SHA1Digest.C:207
static const SHA1Digest null
A null digest (ie, all zero)
Definition: SHA1Digest.H:85
Namespace for OpenFOAM.