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-2021 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_CXX_UTIL_SHA256_HPP
23 #define NDN_CXX_UTIL_SHA256_HPP
24 
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  using std::runtime_error::runtime_error;
51  };
52 
56  static const size_t DIGEST_SIZE = 32;
57 
61  Sha256();
62 
66  explicit
67  Sha256(std::istream& is);
68 
74  bool
75  empty() const
76  {
77  return m_isEmpty;
78  }
79 
83  void
84  reset();
85 
90  computeDigest();
91 
96  bool
97  operator==(Sha256& digest);
98 
103  bool
105  {
106  return !(*this == digest);
107  }
108 
118  Sha256&
119  operator<<(Sha256& src);
120 
125  Sha256&
126  operator<<(const std::string& str);
127 
132  Sha256&
133  operator<<(const Block& block);
134 
139  Sha256&
140  operator<<(uint64_t value);
141 
148  void
149  update(const uint8_t* buffer, size_t size);
150 
155  std::string
156  toString();
157 
164  static ConstBufferPtr
165  computeDigest(const uint8_t* buffer, size_t size);
166 
167 private:
168  unique_ptr<security::transform::StepSource> m_input;
169  unique_ptr<OBufferStream> m_output;
170  bool m_isEmpty;
171  bool m_isFinalized;
172 };
173 
174 std::ostream&
175 operator<<(std::ostream& os, Sha256& digest);
176 
177 } // namespace util
178 } // namespace ndn
179 
180 #endif // NDN_CXX_UTIL_SHA256_HPP
Represents a TLV element of the NDN packet format.
Definition: block.hpp:45
Provides stateful SHA-256 digest calculation.
Definition: sha256.hpp:45
ConstBufferPtr computeDigest()
Finalize and return the digest based on all previously supplied inputs.
Definition: sha256.cpp:63
bool empty() const
Check if digest is empty.
Definition: sha256.hpp:75
Sha256()
Create an empty SHA-256 digest.
Definition: sha256.cpp:34
static const size_t DIGEST_SIZE
Length in bytes of a SHA-256 digest.
Definition: sha256.hpp:56
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 equal to this digest.
Definition: sha256.cpp:75
Sha256 & operator<<(Sha256 &src)
Add existing digest to the digest calculation.
Definition: sha256.cpp:89
bool operator!=(Sha256 &digest)
Check if the supplied digest is not equal to this digest.
Definition: sha256.hpp:104
std::string toString()
Convert digest to std::string.
Definition: sha256.cpp:129
void reset()
Discard the current state and start a new digest calculation.
Definition: sha256.cpp:50
std::ostream & operator<<(std::ostream &os, LogLevel level)
Output LogLevel as a string.
Definition: logger.cpp:31
Definition: data.cpp:25
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:139