blob: d8e803ac5cd6ea7d37ef64d4dd16efc4287483fe [file] [log] [blame]
Jeff Thompson3af7e792013-08-23 14:22:11 -07001/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07002 * Copyright (C) 2013 Regents of the University of California.
Jeff Thompson3af7e792013-08-23 14:22:11 -07003 * See COPYING for copyright and distribution information.
4 */
5
Yingdi Yu21157162014-02-28 13:02:34 -08006#include "../common.hpp"
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -08007
8#include "crypto.hpp"
Junxiao Shi482ccc52014-03-31 13:05:24 -07009#include "../security/cryptopp.hpp"
Alexander Afanasyevd409d592014-01-28 18:36:38 -080010
11namespace ndn {
Jeff Thompson3af7e792013-08-23 14:22:11 -070012
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070013void ndn_digestSha256(const uint8_t* data, size_t dataLength, uint8_t* digest)
Jeff Thompson3af7e792013-08-23 14:22:11 -070014{
Yingdi Yu21157162014-02-28 13:02:34 -080015 try
16 {
17 using namespace CryptoPP;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070018
Yingdi Yu21157162014-02-28 13:02:34 -080019 CryptoPP::SHA256 hash;
20 OBufferStream os;
Yingdi Yu110881d2014-03-04 18:27:29 -080021 StringSource(data, dataLength, true, new HashFilter(hash, new ArraySink(digest, crypto::SHA256_DIGEST_SIZE)));
Yingdi Yu21157162014-02-28 13:02:34 -080022 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070023 catch (CryptoPP::Exception& e)
Yingdi Yu21157162014-02-28 13:02:34 -080024 {
25 return;
26 }
27
Jeff Thompson3af7e792013-08-23 14:22:11 -070028}
Alexander Afanasyevd409d592014-01-28 18:36:38 -080029
Yingdi Yu21157162014-02-28 13:02:34 -080030namespace crypto {
31
32ConstBufferPtr
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070033sha256(const uint8_t* data, size_t dataLength)
Yingdi Yu21157162014-02-28 13:02:34 -080034{
35 try
36 {
37 using namespace CryptoPP;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070038
Yingdi Yu21157162014-02-28 13:02:34 -080039 SHA256 hash;
40 OBufferStream os;
41 StringSource(data, dataLength, true, new HashFilter(hash, new FileSink(os)));
42 return os.buf();
43 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070044 catch (CryptoPP::Exception& e)
Yingdi Yu21157162014-02-28 13:02:34 -080045 {
46 return ConstBufferPtr();
47 }
48}
49
50} // namespace crypto
51
Alexander Afanasyevd409d592014-01-28 18:36:38 -080052} // namespace ndn