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-2023 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 
27 
28 namespace ndn::util {
29 
42 class Sha256
43 {
44 public:
45  class Error : public std::runtime_error
46  {
47  public:
48  using std::runtime_error::runtime_error;
49  };
50 
54  static constexpr size_t DIGEST_SIZE = 32;
55 
59  Sha256();
60 
64  explicit
65  Sha256(std::istream& is);
66 
72  bool
73  empty() const
74  {
75  return m_isEmpty;
76  }
77 
81  void
82  reset();
83 
88  computeDigest();
89 
94  bool
95  operator==(Sha256& digest);
96 
101  bool
103  {
104  return !(*this == digest);
105  }
106 
116  Sha256&
117  operator<<(Sha256& src);
118 
123  Sha256&
124  operator<<(std::string_view str);
125 
130  Sha256&
131  operator<<(uint64_t value);
132 
137  Sha256&
138  operator<<(span<const uint8_t> bytes);
139 
144  void
145  update(span<const uint8_t> buffer);
146 
151  std::string
152  toString();
153 
158  static ConstBufferPtr
159  computeDigest(span<const uint8_t> buffer);
160 
161 private:
162  unique_ptr<security::transform::StepSource> m_input;
163  unique_ptr<OBufferStream> m_output;
164  bool m_isEmpty;
165  bool m_isFinalized;
166 };
167 
168 std::ostream&
169 operator<<(std::ostream& os, Sha256& digest);
170 
171 } // namespace ndn::util
172 
173 #endif // NDN_CXX_UTIL_SHA256_HPP
Provides stateful SHA-256 digest calculation.
Definition: sha256.hpp:43
ConstBufferPtr computeDigest()
Finalize and return the digest based on all previously supplied inputs.
Definition: sha256.cpp:61
bool empty() const
Check if digest is empty.
Definition: sha256.hpp:73
Sha256()
Create an empty SHA-256 digest.
Definition: sha256.cpp:32
bool operator==(Sha256 &digest)
Check if the supplied digest is equal to this digest.
Definition: sha256.cpp:73
Sha256 & operator<<(Sha256 &src)
Add existing digest to the digest calculation.
Definition: sha256.cpp:87
bool operator!=(Sha256 &digest)
Check if the supplied digest is not equal to this digest.
Definition: sha256.hpp:102
std::string toString()
Convert digest to std::string.
Definition: sha256.cpp:126
void reset()
Discard the current state and start a new digest calculation.
Definition: sha256.cpp:48
static constexpr size_t DIGEST_SIZE
Length in bytes of a SHA-256 digest.
Definition: sha256.hpp:54
void update(span< const uint8_t > buffer)
Add a byte buffer to the digest calculation.
Definition: sha256.cpp:115
std::ostream & operator<<(std::ostream &os, LogLevel level)
Output LogLevel as a string.
Definition: logger.cpp:28
std::shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:140