ProteoWizard
SHA1_ostream.hpp
Go to the documentation of this file.
1 //
2 // $Id: SHA1_ostream.hpp 1195 2009-08-14 22:12:04Z chambm $
3 //
4 //
5 // Original author: Darren Kessner <darren@proteowizard.org>
6 //
7 // Copyright 2007 Spielberg Family Center for Applied Proteomics
8 // Cedars-Sinai Medical Center, Los Angeles, California 90048
9 //
10 // Licensed under the Apache License, Version 2.0 (the "License");
11 // you may not use this file except in compliance with the License.
12 // You may obtain a copy of the License at
13 //
14 // http://www.apache.org/licenses/LICENSE-2.0
15 //
16 // Unless required by applicable law or agreed to in writing, software
17 // distributed under the License is distributed on an "AS IS" BASIS,
18 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 // See the License for the specific language governing permissions and
20 // limitations under the License.
21 //
22 
23 
24 #ifndef _SHA1_OSTREAM_HPP_
25 #define _SHA1_OSTREAM_HPP_
26 
27 
28 #include "SHA1Calculator.hpp"
29 #include "boost/iostreams/filtering_stream.hpp"
30 #include "boost/iostreams/filter/symmetric.hpp"
31 #include <string>
32 
33 
34 namespace pwiz {
35 namespace util {
36 
37 
38 /// model of boost::iostreams::SymmetricFilter
40 {
41  public:
42 
43  typedef char char_type;
44 
45  bool filter(const char*& src_begin, const char* src_end,
46  char*& dest_begin, char* dest_end, bool flush)
47  {
48  const char* dest_begin_orig = dest_begin;
49 
50  for (; src_begin!=src_end && dest_begin!=dest_end; ++src_begin, ++dest_begin)
51  *dest_begin = *src_begin;
52 
53  sha1_.update(reinterpret_cast<const unsigned char*>(dest_begin_orig),
54  dest_begin - dest_begin_orig);
55 
56  return false;
57  }
58 
59  void close() {}
60 
61  std::string hash()
62  {
63  return sha1_.hashProjected();
64  };
65 
66  private:
68 };
69 
70 
71 /// model of boost::iostreams::Filter
72 class SHA1Filter : public boost::iostreams::symmetric_filter<SHA1SymmetricFilter>
73 {
74  public:
75 
76  typedef boost::iostreams::symmetric_filter<SHA1SymmetricFilter> base_type;
77 
78  SHA1Filter(int bufferSize)
79  : base_type(bufferSize)
80  {}
81 
82  std::string hash() {return this->filter().hash();}
83 };
84 
85 
86 /// ostream filter for calculating a SHA-1 hash of data on the fly
87 class SHA1_ostream : public boost::iostreams::filtering_ostream
88 {
89  public:
90 
91  SHA1_ostream(std::ostream& os, int bufferSize = 4096)
92  : os_(os), filter_(bufferSize)
93  {
94  push(filter_);
95  push(os);
96  }
97 
98  std::string hash() {return filter_.hash();}
99 
101  {
102  // hack: not flushing properly with the filter in the pipeline
103  pop(); // this flushes os_ explicitly
104  push(os_);
105  }
106 
107  private:
108  std::ostream& os_;
110 };
111 
112 
113 } // namespace util
114 } // namespace pwiz
115 
116 
117 #endif // _SHA1_OSTREAM_HPP_
118 
119