blob: c48040195861a09cacaa9e4344eb2b1f5b72a557 [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/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 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
Yingdi Yu21157162014-02-28 13:02:34 -080022#include "../common.hpp"
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080023
24#include "crypto.hpp"
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070025#include "../encoding/buffer-stream.hpp"
Junxiao Shi482ccc52014-03-31 13:05:24 -070026#include "../security/cryptopp.hpp"
Alexander Afanasyevd409d592014-01-28 18:36:38 -080027
28namespace ndn {
Jeff Thompson3af7e792013-08-23 14:22:11 -070029
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070030void ndn_digestSha256(const uint8_t* data, size_t dataLength, uint8_t* digest)
Jeff Thompson3af7e792013-08-23 14:22:11 -070031{
Yingdi Yu21157162014-02-28 13:02:34 -080032 try
33 {
34 using namespace CryptoPP;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070035
Yingdi Yu21157162014-02-28 13:02:34 -080036 CryptoPP::SHA256 hash;
37 OBufferStream os;
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070038 StringSource(data, dataLength, true,
39 new HashFilter(hash, new ArraySink(digest, crypto::SHA256_DIGEST_SIZE)));
Yingdi Yu21157162014-02-28 13:02:34 -080040 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070041 catch (CryptoPP::Exception& e)
Yingdi Yu21157162014-02-28 13:02:34 -080042 {
43 return;
44 }
45
Jeff Thompson3af7e792013-08-23 14:22:11 -070046}
Alexander Afanasyevd409d592014-01-28 18:36:38 -080047
Yingdi Yu21157162014-02-28 13:02:34 -080048namespace crypto {
49
50ConstBufferPtr
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070051sha256(const uint8_t* data, size_t dataLength)
Yingdi Yu21157162014-02-28 13:02:34 -080052{
53 try
54 {
55 using namespace CryptoPP;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070056
Yingdi Yu21157162014-02-28 13:02:34 -080057 SHA256 hash;
58 OBufferStream os;
59 StringSource(data, dataLength, true, new HashFilter(hash, new FileSink(os)));
60 return os.buf();
61 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070062 catch (CryptoPP::Exception& e)
Yingdi Yu21157162014-02-28 13:02:34 -080063 {
64 return ConstBufferPtr();
65 }
66}
67
68} // namespace crypto
69
Alexander Afanasyevd409d592014-01-28 18:36:38 -080070} // namespace ndn