sha256.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2017 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
7  * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License as published by the Free Software
9  * Foundation, either version 3 of the License, or (at your option) any later version.
10  *
11  * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14  *
15  * You should have received copies of the GNU General Public License and GNU Lesser
16  * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17  * <http://www.gnu.org/licenses/>.
18  *
19  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20  */
21 
22 #ifndef NDN_UTIL_SHA256_HPP
23 #define NDN_UTIL_SHA256_HPP
24 
25 #include "../encoding/block.hpp"
26 #include "../encoding/buffer-stream.hpp"
27 #include "../security/transform/step-source.hpp"
28 
29 namespace ndn {
30 namespace util {
31 
44 class Sha256
45 {
46 public:
47  class Error : public std::runtime_error
48  {
49  public:
50  explicit
51  Error(const std::string& what)
52  : std::runtime_error(what)
53  {
54  }
55  };
56 
60  static const size_t DIGEST_SIZE = 32;
61 
65  Sha256();
66 
70  explicit
71  Sha256(std::istream& is);
72 
78  bool
79  empty() const
80  {
81  return m_isEmpty;
82  }
83 
87  void
88  reset();
89 
94  computeDigest();
95 
100  bool
101  operator==(Sha256& digest);
102 
107  bool
109  {
110  return !(*this == digest);
111  }
112 
122  Sha256&
123  operator<<(Sha256& src);
124 
129  Sha256&
130  operator<<(const std::string& str);
131 
136  Sha256&
137  operator<<(const Block& block);
138 
143  Sha256&
144  operator<<(uint64_t value);
145 
152  void
153  update(const uint8_t* buffer, size_t size);
154 
159  std::string
160  toString();
161 
168  static ConstBufferPtr
169  computeDigest(const uint8_t* buffer, size_t size);
170 
171 private:
172  unique_ptr<security::transform::StepSource> m_input;
173  unique_ptr<OBufferStream> m_output;
174  bool m_isEmpty;
175  bool m_isFinalized;
176 };
177 
178 std::ostream&
179 operator<<(std::ostream& os, Sha256& digest);
180 
181 } // namespace util
182 } // namespace ndn
183 
184 #endif // NDN_UTIL_SHA256_HPP
Copyright (c) 2013-2017 Regents of the University of California.
Definition: common.hpp:66
bool empty() const
Check if digest is empty.
Definition: sha256.hpp:79
Provides stateful SHA-256 digest calculation.
Definition: sha256.hpp:44
static const size_t DIGEST_SIZE
Length in bytes of a SHA-256 digest.
Definition: sha256.hpp:60
bool operator==(Sha256 &digest)
Check if the supplied digest is equal to this digest.
Definition: sha256.cpp:75
STL namespace.
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
std::string toString()
Convert digest to std::string.
Definition: sha256.cpp:129
Sha256()
Create an empty SHA-256 digest.
Definition: sha256.cpp:34
void update(const uint8_t *buffer, size_t size)
Add a raw buffer to the digest calculation.
Definition: sha256.cpp:118
bool operator!=(Sha256 &digest)
Check if the supplied digest is not equal to this digest.
Definition: sha256.hpp:108
void reset()
Discard the current state and start a new digest calculation.
Definition: sha256.cpp:50
Error(const std::string &what)
Definition: sha256.hpp:51
ConstBufferPtr computeDigest()
Finalize and return the digest based on all previously supplied inputs.
Definition: sha256.cpp:63
Sha256 & operator<<(Sha256 &src)
Add existing digest to the digest calculation.
Definition: sha256.cpp:89
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:89