IBR-DTNSuite  0.10
HashStream.h
Go to the documentation of this file.
1 /*
2  * HashStream.h
3  *
4  * Copyright (C) 2011 IBR, TU Braunschweig
5  *
6  * Written-by: Johannes Morgenroth <morgenroth@ibr.cs.tu-bs.de>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 
22 #ifndef HASHSTREAM_H_
23 #define HASHSTREAM_H_
24 
25 #include "ibrcommon/Exceptions.h"
26 #include <streambuf>
27 #include <iostream>
28 #include <vector>
29 
30 namespace ibrcommon
31 {
32  class HashStream : public std::basic_streambuf<char, std::char_traits<char> >, public std::iostream
33  {
34  public:
35  HashStream(const unsigned int hash, const size_t buffer = 2048);
36  virtual ~HashStream();
37 
38  static std::string extract(std::istream &stream);
39 
40  protected:
41  virtual void update(char *buf, const size_t size) = 0;
42  virtual void finalize(char * hash, unsigned int &size) = 0;
43 
44  virtual int sync();
45  virtual std::char_traits<char>::int_type overflow(std::char_traits<char>::int_type = std::char_traits<char>::eof());
46  virtual std::char_traits<char>::int_type underflow();
47 
48  private:
49  // Output buffer
50  std::vector<char> data_buf_;
51 
52  // length of the data buffer
53  size_t data_size_;
54 
55  // Input buffer (contains the hash after finalize)
56  std::vector<char> hash_buf_;
57 
58  // length of the hash
59  unsigned int hash_size_;
60 
61  bool final_;
62  };
63 }
64 
65 #endif /* HASHSTREAM_H_ */