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-2023 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 
47 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
48 
49 namespace Foam
50 {
51 
52 // Forward Declarations
53 class Istream;
54 class Ostream;
55 class SHA1;
56 
57 /*---------------------------------------------------------------------------*\
58  Class SHA1Digest Declaration
59 \*---------------------------------------------------------------------------*/
60 
61 class SHA1Digest
62 {
63  // Private Data
64 
65  //- The digest contents, which has 20 (uncoded) bytes
66  std::array<unsigned char, 20> dig_;
67 
68 
69  // Private Member Functions
70 
71  // Permit SHA1 to calculate the digest
72  friend class SHA1;
73 
74  //- Raw digest data (20 bytes). Non-const access for SHA1
75  unsigned char* data() noexcept { return dig_.data(); }
76 
77 
78 public:
79 
80  // Static Data Members
81 
82  //- A null digest (ie, all zero)
83  static const SHA1Digest null;
84 
85 
86  // Constructors
87 
88  //- Default construct a zero digest
89  SHA1Digest();
90 
91  //- Read construct a digest from stringified content
92  explicit SHA1Digest(Istream& is);
93 
94  //- Construct digest from raw or stringified content.
95  //- The length is 20 for raw digest content and 40 (or 41) for
96  //- stringified versions.
97  SHA1Digest(const char* content, std::size_t len);
98 
99  //- Construct digest from raw or stringified content.
100  //- The length is 20 for raw digest content and 40 (or 41) for
101  //- stringified versions.
102  SHA1Digest(const unsigned char* content, std::size_t len);
103 
104 
105  // Member Functions
106 
107  //- Reset the digest to zero
108  void clear();
109 
110  //- Return true if the digest is empty (ie, all zero).
111  bool empty() const;
112 
113  //- Return (40-byte) text representation, optionally with '_' prefix
114  std::string str(const bool prefixed=false) const;
115 
116  //- Read (40-byte) text representation.
117  // Since leading and intermediate underscores are skipped, a '_' can
118  // be prefixed to the text representation to use an unquoted
119  // SHA1Digest without parsing ambiguities as a number.
120  Istream& read(Istream& is);
121 
122  //- Write (40-byte) text representation, optionally with '_' prefix
123  Ostream& write(Ostream& os, const bool prefixed=false) const;
124 
125 
126  // Low-level access
127 
128  //- Raw digest data (20 bytes) - const access
129  const unsigned char* cdata() const noexcept { return dig_.data(); }
130 
131  //- Raw digest char data (20 bytes) - const access.
132  //- For consistency with other objects, these are \em not unsigned.
133  const char* cdata_bytes() const noexcept
134  {
135  return reinterpret_cast<const char*>(dig_.data());
136  }
137 
138  //- Raw digest char data (20 bytes) - non-const access.
139  //- For consistency with other objects, these are \em not unsigned.
140  //- Use with caution - generally for broadcasting only.
141  char* data_bytes() noexcept
142  {
143  return reinterpret_cast<char*>(dig_.data());
144  }
145 
146  //- The number of bytes in digest (20)
147  static constexpr unsigned size_bytes() noexcept { return 20; }
148 
149  //- The dimensioned size of the digest is always 20 bytes
150  static constexpr unsigned max_size() noexcept { return 20; }
152 
153  // Member Operators
154 
155  //- Equality operator
156  bool operator==(const SHA1Digest& rhs) const;
158  //- Compare to (40-byte) text representation (eg, from sha1sum)
159  // An %empty string is equivalent to
160  // "0000000000000000000000000000000000000000"
161  // The hexdigits may optionally start with a '_' prefix
162  bool operator==(const std::string& hexdigits) const;
163 
164  //- Compare to (40-byte) text representation (eg, from sha1sum)
165  // A %null or %empty string is equivalent to
166  // "0000000000000000000000000000000000000000"
167  // The hexdigits may optionally start with a '_' prefix
168  bool operator==(const char* hexdigits) const;
169 
170 
171  //- Inequality operator
172  bool operator!=(const SHA1Digest& rhs) const;
173 
174  //- Inequality operator
175  bool operator!=(const std::string& hexdigits) const;
176 
177  //- Inequality operator
178  bool operator!=(const char* hexdigits) const;
179 };
181 
182 // IOstream Operators
183 
184 //- Read (40-byte) text representation (ignoring leading '_' prefix)
186 
187 //- Write (40-byte) text representation, unquoted and without prefix
188 Ostream& operator<<(Ostream& os, const SHA1Digest& dig);
189 
190 
191 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
192 
193 } // End namespace Foam
194 
195 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
196 
197 #endif
198 
199 // ************************************************************************* //
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:167
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:56
bool operator!=(const SHA1Digest &rhs) const
Inequality operator.
Definition: SHA1Digest.C:296
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:157
Istream & operator>>(Istream &, directionInfo &)
SHA1Digest()
Default construct a zero digest.
Definition: SHA1Digest.C:163
const unsigned char * cdata() const noexcept
Raw digest data (20 bytes) - const access.
Definition: SHA1Digest.H:151
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
bool empty() const
Return true if the digest is empty (ie, all zero).
Definition: SHA1Digest.C:198
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:252
void clear()
Reset the digest to zero.
Definition: SHA1Digest.C:192
bool operator==(const SHA1Digest &rhs) const
Equality operator.
Definition: SHA1Digest.C:272
static constexpr unsigned max_size() noexcept
The dimensioned size of the digest is always 20 bytes.
Definition: SHA1Digest.H:180
std::string str(const bool prefixed=false) const
Return (40-byte) text representation, optionally with &#39;_&#39; prefix.
Definition: SHA1Digest.C:227
static constexpr unsigned size_bytes() noexcept
The number of bytes in digest (20)
Definition: SHA1Digest.H:175
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:212
static const SHA1Digest null
A null digest (ie, all zero)
Definition: SHA1Digest.H:84
Namespace for OpenFOAM.