NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
digest-filter.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2019 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 
24 
25 #include <boost/lexical_cast.hpp>
26 
27 namespace ndn {
28 namespace security {
29 namespace transform {
30 
32 {
33 public:
35 };
36 
37 
39  : m_impl(make_unique<Impl>())
40 {
41  const EVP_MD* md = detail::digestAlgorithmToEvpMd(algo);
42  if (md == nullptr)
43  NDN_THROW(Error(getIndex(), "Unsupported digest algorithm " + boost::lexical_cast<std::string>(algo)));
44 
45  if (EVP_DigestInit_ex(m_impl->ctx, md, nullptr) == 0)
46  NDN_THROW(Error(getIndex(), "Cannot initialize digest " + boost::lexical_cast<std::string>(algo)));
47 }
48 
49 DigestFilter::~DigestFilter() = default;
50 
51 size_t
52 DigestFilter::convert(const uint8_t* buf, size_t size)
53 {
54  if (EVP_DigestUpdate(m_impl->ctx, buf, size) == 0)
55  NDN_THROW(Error(getIndex(), "Failed to accept more input"));
56 
57  return size;
58 }
59 
60 void
61 DigestFilter::finalize()
62 {
63  auto buffer = make_unique<OBuffer>(EVP_MAX_MD_SIZE);
64  unsigned int mdLen = 0;
65 
66  if (EVP_DigestFinal_ex(m_impl->ctx, buffer->data(), &mdLen) == 0)
67  NDN_THROW(Error(getIndex(), "Failed to finalize digest"));
68 
69  buffer->erase(buffer->begin() + mdLen, buffer->end());
70  setOutputBuffer(std::move(buffer));
71 
73 }
74 
75 unique_ptr<Transform>
77 {
78  return make_unique<DigestFilter>(algo);
79 }
80 
81 } // namespace transform
82 } // namespace security
83 } // namespace ndn
buf
const uint8_t * buf
Definition: verification-helpers.cpp:47
ndn::security::transform::DigestFilter::Impl::ctx
detail::EvpMdCtx ctx
Definition: digest-filter.cpp:34
transform
nonstd::optional_lite::std11::move
T & move(T &t)
Definition: optional.hpp:421
ndn::security::transform::Transform::flushAllOutput
void flushAllOutput()
Read the all the content from output buffer and write it into next module.
Definition: transform-base.cpp:96
ndn::security::transform::DigestFilter::Impl
Definition: digest-filter.cpp:32
ndn::security::transform::DigestFilter::~DigestFilter
~DigestFilter()
ndn::DigestAlgorithm
DigestAlgorithm
Definition: security-common.hpp:96
ndn::security::transform::Error
Base class of transformation error.
Definition: transform-base.hpp:49
digest-filter.hpp
ndn::security::detail::digestAlgorithmToEvpMd
const EVP_MD * digestAlgorithmToEvpMd(DigestAlgorithm algo)
Definition: openssl-helper.cpp:29
NDN_THROW
#define NDN_THROW(e)
Definition: exception.hpp:61
openssl-helper.hpp
ndn::security::transform::Transform::setOutputBuffer
void setOutputBuffer(unique_ptr< OBuffer > buffer)
Set output buffer to buffer.
Definition: transform-base.cpp:104
ndn::security::transform::digestFilter
unique_ptr< Transform > digestFilter(DigestAlgorithm algo)
Definition: digest-filter.cpp:76
ndn::security::transform::Downstream::getIndex
size_t getIndex() const
Get the module index.
Definition: transform-base.hpp:126
ndn::security::transform::DigestFilter::DigestFilter
DigestFilter(DigestAlgorithm algo)
Create a digest module with algorithm algo.
Definition: digest-filter.cpp:38
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34
ndn::security::detail::EvpMdCtx
Definition: openssl-helper.hpp:39