OSHA1stream.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2019 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::OSHA1stream
26 
27 Description
28  An output stream for calculating SHA1 digests.
29 
30 SourceFiles
31  OSHA1stream.C
32 
33 \*---------------------------------------------------------------------------*/
34 
35 #ifndef OSHA1stream_H
36 #define OSHA1stream_H
37 
38 #include "OSstream.H"
39 #include "SHA1.H"
40 
41 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
42 
43 namespace Foam
44 {
45 
46 class osha1stream;
47 class OSHA1stream;
48 
49 /*---------------------------------------------------------------------------*\
50  Class sha1streambuf Declaration
51 \*---------------------------------------------------------------------------*/
52 
53 //- A streambuf class for calculating SHA1 digests
54 class sha1streambuf
55 :
56  public std::streambuf
57 {
58  // Private Data
59 
60  //- This does all the work and has its own buffering
61  SHA1 sha1_;
62 
63  friend class osha1stream;
64 
65 public:
66 
67  // Constructors
68 
69  //- Construct null
71  {}
72 
73  // Member Functions
74 
75  // Write
76 
77  //- Process unbuffered
78  virtual std::streamsize xsputn(const char* str, std::streamsize n)
79  {
80  sha1_.append(str, n);
81  return n;
82  }
83 };
84 
85 
86 /*---------------------------------------------------------------------------*\
87  Class osha1stream Declaration
88 \*---------------------------------------------------------------------------*/
89 
90 //- A basic output stream for calculating SHA1 digests
91 class osha1stream
92 :
93  virtual public std::ios,
94  public std::ostream
95 {
96  // Private Data
97 
98  sha1streambuf sbuf_;
99 
100 public:
101 
102  // Constructors
103 
104  //- Construct null
105  osha1stream()
106  :
107  std::ostream(&sbuf_)
108  {}
109 
110  // Member Functions
111 
112  // Access
113 
114  //- This hides both signatures of std::basic_ios::rdbuf()
115  sha1streambuf* rdbuf()
116  {
117  return &sbuf_;
118  }
119 
120  //- Full access to the sha1
121  SHA1& sha1()
122  {
123  return sbuf_.sha1_;
124  }
125 
126 };
127 
128 
129 /*---------------------------------------------------------------------------*\
130  Class OSHA1stream Declaration
131 \*---------------------------------------------------------------------------*/
132 
133 //- The output stream for calculating SHA1 digests
134 class OSHA1stream
135 :
136  public OSstream
137 {
138 
139 public:
140 
141  // Constructors
142 
143  //- Construct and set stream status
145  (
146  streamFormat format=ASCII,
147  versionNumber version=currentVersion
148  )
149  :
150  OSstream
151  (
152  *(new osha1stream),
153  "OSHA1stream.sinkFile_",
154  format,
155  version
156  )
157  {}
158 
159  //- Disallow default bitwise copy construction
160  OSHA1stream(const OSHA1stream&) = delete;
161 
162 
163  //- Destructor
164  ~OSHA1stream()
165  {
166  delete &dynamic_cast<osha1stream&>(stdStream());
167  }
168 
169 
170  // Member Functions
171 
172  //- Full access to the sha1
173  Foam::SHA1& sha1()
174  {
175  return dynamic_cast<osha1stream&>(stdStream()).sha1();
176  }
177 
178  //- Return SHA1::Digest for the data processed until now
179  Foam::SHA1Digest digest()
180  {
181  return sha1().digest();
182  }
183 
184  //- Clear the SHA1 calculation
185  void rewind()
186  {
187  sha1().clear();
188  }
189 
190 
191  // Member Operators
192 
193  //- Disallow default bitwise assignment
194  void operator=(const OSHA1stream&) = delete;
195 };
196 
197 
198 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
199 
200 } // End namespace Foam
201 
202 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
203 
204 #endif
205 
206 // ************************************************************************* //
Generic output stream.
Definition: OSstream.H:51
SHA1 & append(const char *data, size_t len)
Append data for processing.
Definition: SHA1I.H:53
A basic output stream for calculating SHA1 digests.
Definition: OSHA1stream.H:90
The SHA1 message digest.
Definition: SHA1Digest.H:62
word format(conversionProperties.lookup("format"))
The output stream for calculating SHA1 digests.
Definition: OSHA1stream.H:133
sha1streambuf()
Construct null.
Definition: OSHA1stream.H:69
A streambuf class for calculating SHA1 digests.
Definition: OSHA1stream.H:53
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:86
friend class osha1stream
Definition: OSHA1stream.H:62
virtual std::streamsize xsputn(const char *str, std::streamsize n)
Process unbuffered.
Definition: OSHA1stream.H:77
void clear()
Reset the digest to zero.
Definition: SHA1Digest.C:92
Version number type.
Definition: IOstream.H:96
label n
Functions to compute SHA1 message digest according to the NIST specification FIPS-180-1.
Definition: SHA1.H:68
Namespace for OpenFOAM.