blob: 3083e401a3420b55e917355bc4baa80894ee965b [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Jeff Thompson3af7e792013-08-23 14:22:11 -07002/**
Davide Pesavento791badb2017-02-03 02:03:18 -05003 * Copyright (c) 2013-2017 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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.
Jeff Thompson3af7e792013-08-23 14:22:11 -070020 */
21
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080022#include "crypto.hpp"
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070023#include "../encoding/buffer-stream.hpp"
Davide Pesavento791badb2017-02-03 02:03:18 -050024#include "../security/transform/buffer-source.hpp"
25#include "../security/transform/digest-filter.hpp"
26#include "../security/transform/stream-sink.hpp"
Alexander Afanasyevd409d592014-01-28 18:36:38 -080027
28namespace ndn {
Yingdi Yu21157162014-02-28 13:02:34 -080029namespace crypto {
30
31ConstBufferPtr
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070032computeSha256Digest(const uint8_t* data, size_t dataLength)
Yingdi Yu21157162014-02-28 13:02:34 -080033{
Davide Pesavento791badb2017-02-03 02:03:18 -050034 namespace tr = security::transform;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070035 try {
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070036 OBufferStream os;
Davide Pesavento791badb2017-02-03 02:03:18 -050037 tr::bufferSource(data, dataLength) >> tr::digestFilter(DigestAlgorithm::SHA256)
38 >> tr::streamSink(os);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070039 return os.buf();
40 }
Davide Pesavento791badb2017-02-03 02:03:18 -050041 catch (const tr::Error&) {
42 return nullptr;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070043 }
Yingdi Yu21157162014-02-28 13:02:34 -080044}
45
46} // namespace crypto
Alexander Afanasyevd409d592014-01-28 18:36:38 -080047} // namespace ndn