blob: 55eb459f399538eb431a281832c10f0e7318cf55 [file] [log] [blame]
Yingdi Yude222c72014-08-15 16:06:52 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento94368312017-07-08 22:25:03 -04002/*
Alexander Afanasyev574aa862017-01-10 19:53:28 -08003 * Copyright (c) 2013-2017 Regents of the University of California.
Yingdi Yude222c72014-08-15 16:06:52 -07004 *
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 "digest.hpp"
Alexander Afanasyev8828ca62015-07-02 13:40:09 -070023#include "string-helper.hpp"
Davide Pesavento94368312017-07-08 22:25:03 -040024#include "../security/detail/openssl.hpp"
25#include "../security/transform/digest-filter.hpp"
26#include "../security/transform/stream-sink.hpp"
27#include "../security/transform/stream-source.hpp"
Yingdi Yude222c72014-08-15 16:06:52 -070028
29namespace ndn {
30namespace util {
31
Davide Pesavento94368312017-07-08 22:25:03 -040032Sha256::Sha256()
33{
34 reset();
35}
36
37Sha256::Sha256(std::istream& is)
38 : m_output(make_unique<OBufferStream>())
39 , m_isEmpty(false)
40 , m_isFinalized(true)
41{
42 namespace tr = security::transform;
43
44 tr::streamSource(is) >> tr::digestFilter(DigestAlgorithm::SHA256) >> tr::streamSink(*m_output);
45}
46
47void
48Sha256::reset()
49{
50 namespace tr = security::transform;
51
52 m_input = make_unique<tr::StepSource>();
53 m_output = make_unique<OBufferStream>();
54 m_isEmpty = true;
55 m_isFinalized = false;
56
57 *m_input >> tr::digestFilter(DigestAlgorithm::SHA256) >> tr::streamSink(*m_output);
58}
59
60ConstBufferPtr
61Sha256::computeDigest()
62{
63 if (!m_isFinalized) {
64 BOOST_ASSERT(m_input != nullptr);
65 m_input->end();
66 m_isFinalized = true;
67 }
68
69 return m_output->buf();
70}
71
72bool
73Sha256::operator==(Sha256& digest)
74{
75 const Buffer& lhs = *computeDigest();
76 const Buffer& rhs = *digest.computeDigest();
77
78 if (lhs.size() != rhs.size()) {
79 return false;
80 }
81
82 // constant-time buffer comparison to mitigate timing attacks
83 return CRYPTO_memcmp(lhs.get(), rhs.get(), lhs.size()) == 0;
84}
85
86Sha256&
87Sha256::operator<<(Sha256& src)
88{
89 auto buf = src.computeDigest();
90 update(buf->get(), buf->size());
91 return *this;
92}
93
94Sha256&
95Sha256::operator<<(const std::string& str)
96{
97 update(reinterpret_cast<const uint8_t*>(str.data()), str.size());
98 return *this;
99}
100
101Sha256&
102Sha256::operator<<(const Block& block)
103{
104 update(block.wire(), block.size());
105 return *this;
106}
107
108Sha256&
109Sha256::operator<<(uint64_t value)
110{
111 update(reinterpret_cast<const uint8_t*>(&value), sizeof(uint64_t));
112 return *this;
113}
114
115void
116Sha256::update(const uint8_t* buffer, size_t size)
117{
118 if (m_isFinalized)
119 BOOST_THROW_EXCEPTION(Error("Digest has been already finalized"));
120
121 BOOST_ASSERT(m_input != nullptr);
122 m_input->write(buffer, size);
123 m_isEmpty = false;
124}
125
126std::string
127Sha256::toString()
128{
129 auto buf = computeDigest();
130 return toHex(*buf);
131}
132
Yingdi Yu9ad2d722014-08-30 16:13:57 -0700133std::ostream&
Davide Pesavento94368312017-07-08 22:25:03 -0400134operator<<(std::ostream& os, Sha256& digest)
135{
136 auto buf = digest.computeDigest();
137 printHex(os, *buf);
138 return os;
139}
Yingdi Yude222c72014-08-15 16:06:52 -0700140
141} // namespace util
142} // namespace ndn