NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
sha256.cpp
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 #include "ndn-cxx/util/sha256.hpp"
28 
29 namespace ndn {
30 namespace util {
31 
32 const size_t Sha256::DIGEST_SIZE;
33 
35 {
36  reset();
37 }
38 
39 Sha256::Sha256(std::istream& is)
40  : m_output(make_unique<OBufferStream>())
41  , m_isEmpty(false)
42  , m_isFinalized(true)
43 {
44  namespace tr = security::transform;
45 
47 }
48 
49 void
51 {
52  namespace tr = security::transform;
53 
54  m_input = make_unique<tr::StepSource>();
55  m_output = make_unique<OBufferStream>();
56  m_isEmpty = true;
57  m_isFinalized = false;
58 
59  *m_input >> tr::digestFilter(DigestAlgorithm::SHA256) >> tr::streamSink(*m_output);
60 }
61 
64 {
65  if (!m_isFinalized) {
66  BOOST_ASSERT(m_input != nullptr);
67  m_input->end();
68  m_isFinalized = true;
69  }
70 
71  return m_output->buf();
72 }
73 
74 bool
76 {
77  const Buffer& lhs = *computeDigest();
78  const Buffer& rhs = *digest.computeDigest();
79 
80  if (lhs.size() != rhs.size()) {
81  return false;
82  }
83 
84  // constant-time buffer comparison to mitigate timing attacks
85  return CRYPTO_memcmp(lhs.data(), rhs.data(), lhs.size()) == 0;
86 }
87 
88 Sha256&
90 {
91  update(*src.computeDigest());
92  return *this;
93 }
94 
95 Sha256&
96 Sha256::operator<<(const std::string& str)
97 {
98  update({reinterpret_cast<const uint8_t*>(str.data()), str.size()});
99  return *this;
100 }
101 
102 Sha256&
104 {
105  update({reinterpret_cast<const uint8_t*>(&value), sizeof(uint64_t)});
106  return *this;
107 }
108 
109 Sha256&
110 Sha256::operator<<(span<const uint8_t> bytes)
111 {
112  update(bytes);
113  return *this;
114 }
115 
116 void
117 Sha256::update(span<const uint8_t> buffer)
118 {
119  if (m_isFinalized)
120  NDN_THROW(Error("Digest has been already finalized"));
121 
122  BOOST_ASSERT(m_input != nullptr);
123  m_input->write(buffer);
124  m_isEmpty = false;
125 }
126 
127 std::string
129 {
130  auto buf = computeDigest();
131  return toHex(*buf);
132 }
133 
135 Sha256::computeDigest(span<const uint8_t> buffer)
136 {
137  Sha256 sha256;
138  sha256.update(buffer);
139  return sha256.computeDigest();
140 }
141 
142 std::ostream&
143 operator<<(std::ostream& os, Sha256& digest)
144 {
145  auto buf = digest.computeDigest();
146  printHex(os, *buf);
147  return os;
148 }
149 
150 } // namespace util
151 } // namespace ndn
Copyright (c) 2011-2015 Regents of the University of California.
std::string toHex(span< const uint8_t > buffer, bool wantUpperCase)
Return a string containing the hex representation of the bytes in buffer.
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:56
bool operator==(Sha256 &digest)
Check if the supplied digest is equal to this digest.
Definition: sha256.cpp:75
void update(span< const uint8_t > buffer)
Add a byte buffer to the digest calculation.
Definition: sha256.cpp:117
std::string toString()
Convert digest to std::string.
Definition: sha256.cpp:128
Sha256()
Create an empty SHA-256 digest.
Definition: sha256.cpp:34
#define NDN_THROW(e)
Definition: exception.hpp:61
void reset()
Discard the current state and start a new digest calculation.
Definition: sha256.cpp:50
unique_ptr< Sink > streamSink(std::ostream &os)
Definition: stream-sink.cpp:53
unique_ptr< Transform > digestFilter(DigestAlgorithm algo)
Use the SHA-256 hash of the public key as key id.
ConstBufferPtr computeDigest()
Finalize and return the digest based on all previously supplied inputs.
Definition: sha256.cpp:63
void printHex(std::ostream &os, uint64_t num, bool wantUpperCase)
Output the hex representation of num to the output stream os.
implements an output stream that constructs ndn::Buffer
Sha256 & operator<<(Sha256 &src)
Add existing digest to the digest calculation.
Definition: sha256.cpp:89
General-purpose automatically managed/resized buffer.
Definition: buffer.hpp:41
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:139