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 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 SHA1;
54 class Istream;
55 class Ostream;
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  //- Pointer to the underlying digest data
72  unsigned char* data()
73  {
74  return dig_.data();
75  }
76 
77  //- Byte-wise compare digest contents
78  bool isEqual(const char* hexdigits, std::size_t len) const;
79 
80  // Permit SHA1 to calculate the digest
81  friend class SHA1;
82 
83 
84 public:
85 
86  // Static Data Members
87 
88  //- A null digest (ie, all zero)
89  static const SHA1Digest null;
90 
91 
92  // Constructors
93 
94  //- Default construct a zero digest
95  SHA1Digest();
96 
97  //- Read construct a digest
98  explicit SHA1Digest(Istream& is);
99 
100 
101  // Member Functions
102 
103  //- Reset the digest to zero
104  void clear();
105 
106  //- Return true if the digest is empty (ie, all zero).
107  bool empty() const;
108 
109  //- Return (40-byte) text representation, optionally with '_' prefix
110  std::string str(const bool prefixed=false) const;
111 
112  //- Read (40-byte) text representation.
113  // Since leading and intermediate underscores are skipped, a '_' can
114  // be prefixed to the text representation to use an unquoted
115  // SHA1Digest without parsing ambiguities as a number.
116  Istream& read(Istream& is);
117 
118  //- Write (40-byte) text representation, optionally with '_' prefix
119  Ostream& write(Ostream& os, const bool prefixed=false) const;
120 
121 
122  // Member Operators
123 
124  //- Equality operator
125  bool operator==(const SHA1Digest& rhs) const;
126 
127  //- Compare to (40-byte) text representation (eg, from sha1sum)
128  // An %empty string is equivalent to
129  // "0000000000000000000000000000000000000000"
130  // The hexdigits may optionally start with a '_' prefix
131  bool operator==(const std::string& hexdigits) const;
132 
133  //- Compare to (40-byte) text representation (eg, from sha1sum)
134  // A %null or %empty string is equivalent to
135  // "0000000000000000000000000000000000000000"
136  // The hexdigits may optionally start with a '_' prefix
137  bool operator==(const char* hexdigits) const;
138 
139 
140  //- Inequality operator
141  bool operator!=(const SHA1Digest& rhs) const;
142 
143  //- Inequality operator
144  bool operator!=(const std::string& hexdigits) const;
145 
146  //- Inequality operator
147  bool operator!=(const char* hexdigits) const;
148 };
149 
150 
151 // IOstream Operators
152 
153 //- Read (40-byte) text representation (ignoring leading '_' prefix)
155 
156 //- Write (40-byte) text representation, unquoted and without prefix
157 Ostream& operator<<(Ostream& os, const SHA1Digest& dig);
158 
159 
160 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
161 
162 } // End namespace Foam
163 
164 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
165 
166 #endif
167 
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:56
bool operator!=(const SHA1Digest &rhs) const
Inequality operator.
Definition: SHA1Digest.C:223
Istream & operator>>(Istream &, directionInfo &)
SHA1Digest()
Default construct a zero digest.
Definition: SHA1Digest.C:104
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:55
bool empty() const
Return true if the digest is empty (ie, all zero).
Definition: SHA1Digest.C:125
OBJstream os(runTime.globalPath()/outputName)
Database for solution data, solver performance and other reduced data.
Definition: data.H:51
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:76
Ostream & write(Ostream &os, const bool prefixed=false) const
Write (40-byte) text representation, optionally with &#39;_&#39; prefix.
Definition: SHA1Digest.C:179
void clear()
Reset the digest to zero.
Definition: SHA1Digest.C:119
bool operator==(const SHA1Digest &rhs) const
Equality operator.
Definition: SHA1Digest.C:199
std::string str(const bool prefixed=false) const
Return (40-byte) text representation, optionally with &#39;_&#39; prefix.
Definition: SHA1Digest.C:154
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:139
static const SHA1Digest null
A null digest (ie, all zero)
Definition: SHA1Digest.H:92
Namespace for OpenFOAM.