blob: 6e559ddfa7e5487dfcb6430c2c5f3a04f9b5f29a [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"
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070016#include "../encoding/buffer-stream.hpp"
Junxiao Shi482ccc52014-03-31 13:05:24 -070017#include "../security/cryptopp.hpp"
Alexander Afanasyevd409d592014-01-28 18:36:38 -080018
19namespace ndn {
Jeff Thompson3af7e792013-08-23 14:22:11 -070020
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070021void ndn_digestSha256(const uint8_t* data, size_t dataLength, uint8_t* digest)
Jeff Thompson3af7e792013-08-23 14:22:11 -070022{
Yingdi Yu21157162014-02-28 13:02:34 -080023 try
24 {
25 using namespace CryptoPP;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070026
Yingdi Yu21157162014-02-28 13:02:34 -080027 CryptoPP::SHA256 hash;
28 OBufferStream os;
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070029 StringSource(data, dataLength, true,
30 new HashFilter(hash, new ArraySink(digest, crypto::SHA256_DIGEST_SIZE)));
Yingdi Yu21157162014-02-28 13:02:34 -080031 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070032 catch (CryptoPP::Exception& e)
Yingdi Yu21157162014-02-28 13:02:34 -080033 {
34 return;
35 }
36
Jeff Thompson3af7e792013-08-23 14:22:11 -070037}
Alexander Afanasyevd409d592014-01-28 18:36:38 -080038
Yingdi Yu21157162014-02-28 13:02:34 -080039namespace crypto {
40
41ConstBufferPtr
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070042sha256(const uint8_t* data, size_t dataLength)
Yingdi Yu21157162014-02-28 13:02:34 -080043{
44 try
45 {
46 using namespace CryptoPP;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070047
Yingdi Yu21157162014-02-28 13:02:34 -080048 SHA256 hash;
49 OBufferStream os;
50 StringSource(data, dataLength, true, new HashFilter(hash, new FileSink(os)));
51 return os.buf();
52 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070053 catch (CryptoPP::Exception& e)
Yingdi Yu21157162014-02-28 13:02:34 -080054 {
55 return ConstBufferPtr();
56 }
57}
58
59} // namespace crypto
60
Alexander Afanasyevd409d592014-01-28 18:36:38 -080061} // namespace ndn