Jeff Thompson | 3af7e79 | 2013-08-23 14:22:11 -0700 | [diff] [blame] | 1 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 2 | * Copyright (C) 2013 Regents of the University of California. |
Jeff Thompson | 3af7e79 | 2013-08-23 14:22:11 -0700 | [diff] [blame] | 3 | * See COPYING for copyright and distribution information. |
| 4 | */ |
| 5 | |
Yingdi Yu | 2115716 | 2014-02-28 13:02:34 -0800 | [diff] [blame] | 6 | #include "../common.hpp" |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 7 | |
| 8 | #include "crypto.hpp" |
Yingdi Yu | 2115716 | 2014-02-28 13:02:34 -0800 | [diff] [blame] | 9 | #include <cryptopp/sha.h> |
| 10 | #include <cryptopp/filters.h> |
| 11 | #include <cryptopp/files.h> |
Alexander Afanasyev | d409d59 | 2014-01-28 18:36:38 -0800 | [diff] [blame] | 12 | |
| 13 | namespace ndn { |
Jeff Thompson | 3af7e79 | 2013-08-23 14:22:11 -0700 | [diff] [blame] | 14 | |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 15 | void ndn_digestSha256(const uint8_t *data, size_t dataLength, uint8_t *digest) |
Jeff Thompson | 3af7e79 | 2013-08-23 14:22:11 -0700 | [diff] [blame] | 16 | { |
Yingdi Yu | 2115716 | 2014-02-28 13:02:34 -0800 | [diff] [blame] | 17 | try |
| 18 | { |
| 19 | using namespace CryptoPP; |
| 20 | |
| 21 | CryptoPP::SHA256 hash; |
| 22 | OBufferStream os; |
| 23 | StringSource(data, dataLength, true, new HashFilter(hash, new ArraySink(digest, crypto::SHA256_DIGEST_LENGTH))); |
| 24 | } |
| 25 | catch(CryptoPP::Exception& e) |
| 26 | { |
| 27 | return; |
| 28 | } |
| 29 | |
Jeff Thompson | 3af7e79 | 2013-08-23 14:22:11 -0700 | [diff] [blame] | 30 | } |
Alexander Afanasyev | d409d59 | 2014-01-28 18:36:38 -0800 | [diff] [blame] | 31 | |
Yingdi Yu | 2115716 | 2014-02-28 13:02:34 -0800 | [diff] [blame] | 32 | namespace crypto { |
| 33 | |
| 34 | ConstBufferPtr |
| 35 | sha256(const uint8_t *data, size_t dataLength) |
| 36 | { |
| 37 | try |
| 38 | { |
| 39 | using namespace CryptoPP; |
| 40 | |
| 41 | SHA256 hash; |
| 42 | OBufferStream os; |
| 43 | StringSource(data, dataLength, true, new HashFilter(hash, new FileSink(os))); |
| 44 | return os.buf(); |
| 45 | } |
| 46 | catch(CryptoPP::Exception& e) |
| 47 | { |
| 48 | return ConstBufferPtr(); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | } // namespace crypto |
| 53 | |
Alexander Afanasyev | d409d59 | 2014-01-28 18:36:38 -0800 | [diff] [blame] | 54 | } // namespace ndn |