blob: 29cbe9d6fe1546d041a5ae039b5ff07123e38ecf [file] [log] [blame]
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Jeff Thompson3af7e792013-08-23 14:22:11 -07002/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
Jeff Thompson3af7e792013-08-23 14:22:11 -070011 */
12
Yingdi Yu21157162014-02-28 13:02:34 -080013#include "../common.hpp"
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080014
15#include "crypto.hpp"
Junxiao Shi482ccc52014-03-31 13:05:24 -070016#include "../security/cryptopp.hpp"
Alexander Afanasyevd409d592014-01-28 18:36:38 -080017
18namespace ndn {
Jeff Thompson3af7e792013-08-23 14:22:11 -070019
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070020void ndn_digestSha256(const uint8_t* data, size_t dataLength, uint8_t* digest)
Jeff Thompson3af7e792013-08-23 14:22:11 -070021{
Yingdi Yu21157162014-02-28 13:02:34 -080022 try
23 {
24 using namespace CryptoPP;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070025
Yingdi Yu21157162014-02-28 13:02:34 -080026 CryptoPP::SHA256 hash;
27 OBufferStream os;
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070028 StringSource(data, dataLength, true,
29 new HashFilter(hash, new ArraySink(digest, crypto::SHA256_DIGEST_SIZE)));
Yingdi Yu21157162014-02-28 13:02:34 -080030 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070031 catch (CryptoPP::Exception& e)
Yingdi Yu21157162014-02-28 13:02:34 -080032 {
33 return;
34 }
35
Jeff Thompson3af7e792013-08-23 14:22:11 -070036}
Alexander Afanasyevd409d592014-01-28 18:36:38 -080037
Yingdi Yu21157162014-02-28 13:02:34 -080038namespace crypto {
39
40ConstBufferPtr
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070041sha256(const uint8_t* data, size_t dataLength)
Yingdi Yu21157162014-02-28 13:02:34 -080042{
43 try
44 {
45 using namespace CryptoPP;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070046
Yingdi Yu21157162014-02-28 13:02:34 -080047 SHA256 hash;
48 OBufferStream os;
49 StringSource(data, dataLength, true, new HashFilter(hash, new FileSink(os)));
50 return os.buf();
51 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070052 catch (CryptoPP::Exception& e)
Yingdi Yu21157162014-02-28 13:02:34 -080053 {
54 return ConstBufferPtr();
55 }
56}
57
58} // namespace crypto
59
Alexander Afanasyevd409d592014-01-28 18:36:38 -080060} // namespace ndn