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-2022 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<<(uint64_t value);
134 
139  Sha256&
140  operator<<(span<const uint8_t> bytes);
141 
146  void
147  update(span<const uint8_t> buffer);
148 
153  std::string
154  toString();
155 
160  static ConstBufferPtr
161  computeDigest(span<const uint8_t> buffer);
162 
163 private:
164  unique_ptr<security::transform::StepSource> m_input;
165  unique_ptr<OBufferStream> m_output;
166  bool m_isEmpty;
167  bool m_isFinalized;
168 };
169 
170 std::ostream&
171 operator<<(std::ostream& os, Sha256& digest);
172 
173 } // namespace util
174 } // namespace ndn
175 
176 #endif // NDN_CXX_UTIL_SHA256_HPP
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:64
bool empty() const
Check if digest is empty.
Definition: sha256.hpp:75
Sha256()
Create an empty SHA-256 digest.
Definition: sha256.cpp:35
static const size_t DIGEST_SIZE
Length in bytes of a SHA-256 digest.
Definition: sha256.hpp:56
bool operator==(Sha256 &digest)
Check if the supplied digest is equal to this digest.
Definition: sha256.cpp:76
Sha256 & operator<<(Sha256 &src)
Add existing digest to the digest calculation.
Definition: sha256.cpp:90
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:51
void update(span< const uint8_t > buffer)
Add a byte buffer to the digest calculation.
Definition: sha256.cpp:118
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