util: deprecate crypto::computeSha256Digest()
Use Sha256::computeDigest() instead.
Change-Id: I9db5b4839559c9c7930cdc24c78f35ca76b25b52
diff --git a/src/data.cpp b/src/data.cpp
index 8261449..36fdfd4 100644
--- a/src/data.cpp
+++ b/src/data.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2013-2016 Regents of the University of California.
+/*
+ * Copyright (c) 2013-2017 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -21,7 +21,7 @@
#include "data.hpp"
#include "encoding/block-helpers.hpp"
-#include "util/crypto.hpp"
+#include "util/digest.hpp"
namespace ndn {
@@ -184,7 +184,7 @@
"(e.g., not signed)"));
}
m_fullName = m_name;
- m_fullName.appendImplicitSha256Digest(crypto::computeSha256Digest(m_wire.wire(), m_wire.size()));
+ m_fullName.appendImplicitSha256Digest(util::Sha256::computeDigest(m_wire.wire(), m_wire.size()));
}
return m_fullName;
diff --git a/src/interest.cpp b/src/interest.cpp
index 2f54dcb..b35814c 100644
--- a/src/interest.cpp
+++ b/src/interest.cpp
@@ -21,7 +21,6 @@
#include "interest.hpp"
#include "util/random.hpp"
-#include "util/crypto.hpp"
#include "data.hpp"
#include <cstring>
diff --git a/src/name-component.cpp b/src/name-component.cpp
index 746f69a..34a3470 100644
--- a/src/name-component.cpp
+++ b/src/name-component.cpp
@@ -1,5 +1,5 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
+/*
* Copyright (c) 2013-2017 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
@@ -27,8 +27,8 @@
#include "encoding/block-helpers.hpp"
#include "encoding/encoding-buffer.hpp"
+#include "util/digest.hpp"
#include "util/string-helper.hpp"
-#include "util/crypto.hpp"
#include <boost/algorithm/string/trim.hpp>
@@ -87,7 +87,6 @@
{
}
-
Component
Component::fromEscapedString(const char* escapedString, size_t beginOffset, size_t endOffset)
{
@@ -96,7 +95,7 @@
if (trimmedString.compare(0, getSha256DigestUriPrefix().size(),
getSha256DigestUriPrefix()) == 0) {
- if (trimmedString.size() != getSha256DigestUriPrefix().size() + crypto::SHA256_DIGEST_SIZE * 2)
+ if (trimmedString.size() != getSha256DigestUriPrefix().size() + util::Sha256::DIGEST_SIZE * 2)
BOOST_THROW_EXCEPTION(Error("Cannot convert to ImplicitSha256DigestComponent"
"(expected sha256 in hex encoding)"));
@@ -357,15 +356,15 @@
Component::isImplicitSha256Digest() const
{
return (type() == tlv::ImplicitSha256DigestComponent &&
- value_size() == crypto::SHA256_DIGEST_SIZE);
+ value_size() == util::Sha256::DIGEST_SIZE);
}
Component
Component::fromImplicitSha256Digest(const ConstBufferPtr& digest)
{
- if (digest->size() != crypto::SHA256_DIGEST_SIZE)
+ if (digest->size() != util::Sha256::DIGEST_SIZE)
BOOST_THROW_EXCEPTION(Error("Cannot create ImplicitSha256DigestComponent (input digest must be " +
- to_string(crypto::SHA256_DIGEST_SIZE) + " octets)"));
+ to_string(util::Sha256::DIGEST_SIZE) + " octets)"));
return Block(tlv::ImplicitSha256DigestComponent, digest);
}
@@ -373,9 +372,9 @@
Component
Component::fromImplicitSha256Digest(const uint8_t* digest, size_t digestSize)
{
- if (digestSize != crypto::SHA256_DIGEST_SIZE)
+ if (digestSize != util::Sha256::DIGEST_SIZE)
BOOST_THROW_EXCEPTION(Error("Cannot create ImplicitSha256DigestComponent (input digest must be " +
- to_string(crypto::SHA256_DIGEST_SIZE) + " octets)"));
+ to_string(util::Sha256::DIGEST_SIZE) + " octets)"));
return makeBinaryBlock(tlv::ImplicitSha256DigestComponent, digest, digestSize);
}
diff --git a/src/security/v1/key-chain.cpp b/src/security/v1/key-chain.cpp
index 1916532..9efcb4a 100644
--- a/src/security/v1/key-chain.cpp
+++ b/src/security/v1/key-chain.cpp
@@ -1,5 +1,5 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
+/*
* Copyright (c) 2013-2017 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
@@ -24,8 +24,8 @@
#include "key-chain.hpp"
#include "../signing-helpers.hpp"
-#include "../../util/random.hpp"
#include "../../util/config-file.hpp"
+#include "../../util/digest.hpp"
#include "sec-public-info-sqlite3.hpp"
@@ -755,7 +755,7 @@
const Name& keyName, DigestAlgorithm digestAlgorithm) const
{
if (keyName == SigningInfo::getDigestSha256Identity())
- return Block(tlv::SignatureValue, crypto::computeSha256Digest(buf, size));
+ return Block(tlv::SignatureValue, util::Sha256::computeDigest(buf, size));
return m_tpm->signInTpm(buf, size, keyName, digestAlgorithm);
}
@@ -790,7 +790,7 @@
.append(sig.getInfo()); // signatureInfo
Block sigValue(tlv::SignatureValue,
- crypto::computeSha256Digest(signedName.wireEncode().value(),
+ util::Sha256::computeDigest(signedName.wireEncode().value(),
signedName.wireEncode().value_size()));
sigValue.encode();
diff --git a/src/security/v1/key-chain.hpp b/src/security/v1/key-chain.hpp
index 65821fa..3a84a47 100644
--- a/src/security/v1/key-chain.hpp
+++ b/src/security/v1/key-chain.hpp
@@ -1,5 +1,5 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
+/*
* Copyright (c) 2013-2017 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
@@ -32,9 +32,7 @@
#include "../signature-sha256-with-ecdsa.hpp"
#include "../digest-sha256.hpp"
#include "../signing-info.hpp"
-
#include "../../interest.hpp"
-#include "../../util/crypto.hpp"
#include "../../util/random.hpp"
#include <initializer_list>
@@ -132,7 +130,7 @@
/**
* @brief KeyChain constructor
*
- * @sa http://redmine.named-data.net/issues/2260
+ * @sa https://redmine.named-data.net/issues/2260
*
* @param pibLocator PIB locator
* @param tpmLocator TPM locator
diff --git a/src/security/v1/public-key.cpp b/src/security/v1/public-key.cpp
index cef7295..7ada249 100644
--- a/src/security/v1/public-key.cpp
+++ b/src/security/v1/public-key.cpp
@@ -1,5 +1,5 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
+/*
* Copyright (c) 2013-2017 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
@@ -25,7 +25,7 @@
#include "public-key.hpp"
#include "../../encoding/oid.hpp"
-#include "../../util/crypto.hpp"
+#include "../../util/digest.hpp"
#include "cryptopp.hpp"
namespace ndn {
@@ -53,7 +53,7 @@
if (m_digest.hasWire())
return m_digest;
else {
- m_digest = Block(tlv::KeyDigest, crypto::computeSha256Digest(m_key.buf(), m_key.size()));
+ m_digest = Block(tlv::KeyDigest, util::Sha256::computeDigest(m_key.buf(), m_key.size()));
m_digest.encode();
return m_digest;
}
@@ -119,7 +119,7 @@
m_key.assign(out.begin(), out.end());
}
- catch (CryptoPP::BERDecodeErr& err)
+ catch (const CryptoPP::BERDecodeErr& err)
{
m_type = KeyType::NONE;
BOOST_THROW_EXCEPTION(Error("PublicKey decoding error"));
diff --git a/src/security/v2/key-chain.cpp b/src/security/v2/key-chain.cpp
index 45d9277..c6e044c 100644
--- a/src/security/v2/key-chain.cpp
+++ b/src/security/v2/key-chain.cpp
@@ -1,5 +1,5 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
+/*
* Copyright (c) 2013-2017 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
@@ -22,6 +22,7 @@
#include "key-chain.hpp"
#include "../../util/config-file.hpp"
+#include "../../util/digest.hpp"
#include "../../util/logger.hpp"
#include "../pib/pib-sqlite3.hpp"
@@ -39,7 +40,6 @@
#include "../transform/private-key.hpp"
#include "../transform/verifier-filter.hpp"
#include "../../encoding/buffer-stream.hpp"
-#include "../../util/crypto.hpp"
#include <boost/lexical_cast.hpp>
@@ -685,7 +685,7 @@
const Name& keyName, DigestAlgorithm digestAlgorithm) const
{
if (keyName == SigningInfo::getDigestSha256Identity())
- return Block(tlv::SignatureValue, crypto::computeSha256Digest(buf, size));
+ return Block(tlv::SignatureValue, util::Sha256::computeDigest(buf, size));
return Block(tlv::SignatureValue, m_tpm->sign(buf, size, keyName, digestAlgorithm));
}
diff --git a/src/security/validator.cpp b/src/security/validator.cpp
index f0f87b7..4862d76 100644
--- a/src/security/validator.cpp
+++ b/src/security/validator.cpp
@@ -1,5 +1,5 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
+/*
* Copyright (c) 2013-2017 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
@@ -20,7 +20,7 @@
*/
#include "validator.hpp"
-#include "../util/crypto.hpp"
+#include "../util/digest.hpp"
#include "../lp/tags.hpp"
#include "v1/cryptopp.hpp"
@@ -234,21 +234,20 @@
Validator::verifySignature(const uint8_t* buf, const size_t size, const DigestSha256& sig)
{
try {
- ConstBufferPtr buffer = crypto::computeSha256Digest(buf, size);
+ ConstBufferPtr buffer = util::Sha256::computeDigest(buf, size);
const Block& sigValue = sig.getValue();
- if (buffer != nullptr &&
- buffer->size() == sigValue.value_size() &&
- buffer->size() == crypto::SHA256_DIGEST_SIZE) {
+ if (buffer->size() == sigValue.value_size() &&
+ buffer->size() == util::Sha256::DIGEST_SIZE) {
const uint8_t* p1 = buffer->buf();
const uint8_t* p2 = sigValue.value();
- return 0 == memcmp(p1, p2, crypto::SHA256_DIGEST_SIZE);
+ return 0 == memcmp(p1, p2, util::Sha256::DIGEST_SIZE);
}
else
return false;
}
- catch (const CryptoPP::Exception& e) {
+ catch (const util::Sha256::Error&) {
return false;
}
}
diff --git a/src/util/crypto.cpp b/src/util/crypto.cpp
deleted file mode 100644
index 3083e40..0000000
--- a/src/util/crypto.cpp
+++ /dev/null
@@ -1,47 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2013-2017 Regents of the University of California.
- *
- * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
- *
- * ndn-cxx library is free software: you can redistribute it and/or modify it under the
- * terms of the GNU Lesser General Public License as published by the Free Software
- * Foundation, either version 3 of the License, or (at your option) any later version.
- *
- * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
- * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
- * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
- *
- * You should have received copies of the GNU General Public License and GNU Lesser
- * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
- * <http://www.gnu.org/licenses/>.
- *
- * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
- */
-
-#include "crypto.hpp"
-#include "../encoding/buffer-stream.hpp"
-#include "../security/transform/buffer-source.hpp"
-#include "../security/transform/digest-filter.hpp"
-#include "../security/transform/stream-sink.hpp"
-
-namespace ndn {
-namespace crypto {
-
-ConstBufferPtr
-computeSha256Digest(const uint8_t* data, size_t dataLength)
-{
- namespace tr = security::transform;
- try {
- OBufferStream os;
- tr::bufferSource(data, dataLength) >> tr::digestFilter(DigestAlgorithm::SHA256)
- >> tr::streamSink(os);
- return os.buf();
- }
- catch (const tr::Error&) {
- return nullptr;
- }
-}
-
-} // namespace crypto
-} // namespace ndn
diff --git a/src/util/crypto.hpp b/src/util/crypto.hpp
index 860b17d..281e2f2 100644
--- a/src/util/crypto.hpp
+++ b/src/util/crypto.hpp
@@ -1,5 +1,5 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
+/*
* Copyright (c) 2013-2017 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
@@ -22,14 +22,21 @@
#ifndef NDN_UTIL_CRYPTO_HPP
#define NDN_UTIL_CRYPTO_HPP
-#include "../common.hpp"
-#include "../encoding/buffer.hpp"
+/**
+ * @file
+ * @deprecated use <ndn-cxx/util/digest.hpp>
+ */
+
+#include "digest.hpp"
namespace ndn {
namespace crypto {
-/// @brief number of octets in a SHA256 digest
-const size_t SHA256_DIGEST_SIZE = 32;
+/**
+ * @brief number of octets in a SHA256 digest
+ * @deprecated use ndn::util::Sha256::DIGEST_SIZE
+ */
+DEPRECATED(const size_t SHA256_DIGEST_SIZE) = util::Sha256::DIGEST_SIZE;
/**
* @brief Compute the SHA-256 digest of data.
@@ -37,9 +44,15 @@
* @param data Pointer to the input byte array.
* @param dataLength The length of data.
* @return A pointer to a buffer of SHA256_DIGEST_SIZE bytes.
+ *
+ * @deprecated use ndn::util::Sha256::computeDigest()
*/
-ConstBufferPtr
-computeSha256Digest(const uint8_t* data, size_t dataLength);
+DEPRECATED()
+inline ConstBufferPtr
+computeSha256Digest(const uint8_t* data, size_t dataLength)
+{
+ return util::Sha256::computeDigest(data, dataLength);
+}
} // namespace crypto
} // namespace ndn
diff --git a/src/util/digest.cpp b/src/util/digest.cpp
index 55eb459..8053fdb 100644
--- a/src/util/digest.cpp
+++ b/src/util/digest.cpp
@@ -29,6 +29,8 @@
namespace ndn {
namespace util {
+const size_t Sha256::DIGEST_SIZE;
+
Sha256::Sha256()
{
reset();
@@ -130,6 +132,14 @@
return toHex(*buf);
}
+ConstBufferPtr
+Sha256::computeDigest(const uint8_t* buffer, size_t size)
+{
+ Sha256 sha256;
+ sha256.update(buffer, size);
+ return sha256.computeDigest();
+}
+
std::ostream&
operator<<(std::ostream& os, Sha256& digest)
{
diff --git a/src/util/digest.hpp b/src/util/digest.hpp
index 1c3c857..12dbd96 100644
--- a/src/util/digest.hpp
+++ b/src/util/digest.hpp
@@ -22,7 +22,6 @@
#ifndef NDN_UTIL_DIGEST_HPP
#define NDN_UTIL_DIGEST_HPP
-#include "crypto.hpp"
#include "../encoding/block.hpp"
#include "../encoding/buffer-stream.hpp"
#include "../security/transform/step-source.hpp"
@@ -56,6 +55,11 @@
};
/**
+ * @brief Length in bytes of a SHA-256 digest.
+ */
+ static const size_t DIGEST_SIZE = 32;
+
+ /**
* @brief Create an empty SHA-256 digest.
*/
Sha256();
@@ -156,16 +160,13 @@
toString();
/**
- * @brief Compute a one-time SHA-256 digest.
+ * @brief Stateless SHA-256 digest calculation.
* @param buffer the input buffer
* @param size the size of the input buffer
* @return SHA-256 digest of the input buffer
*/
static ConstBufferPtr
- computeDigest(const uint8_t* buffer, size_t size)
- {
- return crypto::computeSha256Digest(buffer, size);
- }
+ computeDigest(const uint8_t* buffer, size_t size);
private:
unique_ptr<security::transform::StepSource> m_input;
diff --git a/src/util/in-memory-storage.cpp b/src/util/in-memory-storage.cpp
index 69f2b5a..bc1beac 100644
--- a/src/util/in-memory-storage.cpp
+++ b/src/util/in-memory-storage.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2013-2016 Regents of the University of California.
+/*
+ * Copyright (c) 2013-2017 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -22,8 +22,6 @@
#include "in-memory-storage.hpp"
#include "in-memory-storage-entry.hpp"
-#include "crypto.hpp"
-
#include "../security/signature-sha256-with-rsa.hpp"
namespace ndn {
@@ -423,9 +421,7 @@
{
Cache::index<byFullName>::type::iterator it = m_cache.get<byFullName>().end();
- const Data* ptr = NULL;
-
- return const_iterator(ptr, &m_cache, it);
+ return const_iterator(nullptr, &m_cache, it);
}
void
diff --git a/tests/unit-tests/exclude.t.cpp b/tests/unit-tests/exclude.t.cpp
index 1e074d5..a6823c6 100644
--- a/tests/unit-tests/exclude.t.cpp
+++ b/tests/unit-tests/exclude.t.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2013-2016 Regents of the University of California.
+/*
+ * Copyright (c) 2013-2017 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -20,7 +20,7 @@
*/
#include "exclude.hpp"
-#include "util/crypto.hpp"
+#include "util/digest.hpp"
#include "boost-test.hpp"
@@ -86,7 +86,7 @@
BOOST_AUTO_TEST_CASE(Before)
{
- // based on http://redmine.named-data.net/issues/1158
+ // based on https://redmine.named-data.net/issues/1158
ndn::Exclude e;
BOOST_REQUIRE_NO_THROW(e.excludeBefore(name::Component("PuQxMaf91")));
@@ -237,7 +237,7 @@
BOOST_AUTO_TEST_SUITE(ImplicitDigest) // exclude ImplicitSha256DigestComponent
-/** \brief make a name::Component with an octet repeated crypto::SHA256_DIGEST_SIZE times
+/** \brief make a name::Component with an octet repeated util::Sha256::DIGEST_SIZE times
* \param octet the octet to fill the component
* \param isDigest whether to make an ImplicitSha256DigestComponent or a generic NameComponent
* \param lastOctet if non-negative, set the last octet to a different value
@@ -245,10 +245,10 @@
static name::Component
makeComponent(uint8_t octet, bool isDigest, int lastOctet = -1)
{
- uint8_t wire[crypto::SHA256_DIGEST_SIZE];
+ uint8_t wire[util::Sha256::DIGEST_SIZE];
std::memset(wire, octet, sizeof(wire));
if (lastOctet >= 0) {
- wire[crypto::SHA256_DIGEST_SIZE - 1] = static_cast<uint8_t>(lastOctet);
+ wire[util::Sha256::DIGEST_SIZE - 1] = static_cast<uint8_t>(lastOctet);
}
if (isDigest) {
diff --git a/tests/unit-tests/security/digest-sha256.t.cpp b/tests/unit-tests/security/digest-sha256.t.cpp
index 732efe6..6facb98 100644
--- a/tests/unit-tests/security/digest-sha256.t.cpp
+++ b/tests/unit-tests/security/digest-sha256.t.cpp
@@ -1,5 +1,5 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
+/*
* Copyright (c) 2013-2017 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
@@ -21,6 +21,7 @@
#include "security/digest-sha256.hpp"
#include "security/validator.hpp"
+#include "util/digest.hpp"
#include "util/string-helper.hpp"
#include "identity-management-fixture.hpp"
@@ -35,7 +36,7 @@
BOOST_AUTO_TEST_CASE(Sha256)
{
char content[6] = "1234\n";
- ConstBufferPtr buf = crypto::computeSha256Digest(reinterpret_cast<uint8_t*>(content), 5);
+ ConstBufferPtr buf = util::Sha256::computeDigest(reinterpret_cast<uint8_t*>(content), 5);
BOOST_CHECK_EQUAL(toHex(buf->buf(), buf->size(), false),
"a883dafc480d466ee04e0d6da986bd78eb1fdd2178d04693723da3a8f95d42f4");
diff --git a/tests/unit-tests/util/crypto.t.cpp b/tests/unit-tests/util/crypto.t.cpp
deleted file mode 100644
index 7770245..0000000
--- a/tests/unit-tests/util/crypto.t.cpp
+++ /dev/null
@@ -1,54 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2013-2017 Regents of the University of California.
- *
- * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
- *
- * ndn-cxx library is free software: you can redistribute it and/or modify it under the
- * terms of the GNU Lesser General Public License as published by the Free Software
- * Foundation, either version 3 of the License, or (at your option) any later version.
- *
- * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
- * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
- * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
- *
- * You should have received copies of the GNU General Public License and GNU Lesser
- * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
- * <http://www.gnu.org/licenses/>.
- *
- * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
- */
-
-#include "util/crypto.hpp"
-
-#include "boost-test.hpp"
-
-namespace ndn {
-namespace crypto {
-namespace tests {
-
-BOOST_AUTO_TEST_SUITE(Util)
-BOOST_AUTO_TEST_SUITE(TestCrypto)
-
-BOOST_AUTO_TEST_CASE(Basic)
-{
- const std::string testString = "Hello, world!";
- auto result = computeSha256Digest(reinterpret_cast<const uint8_t*>(testString.data()),
- testString.size());
-
- BOOST_CHECK_EQUAL(result->size(), SHA256_DIGEST_SIZE);
-
- const uint8_t expectedSha256[] = {0x31, 0x5f, 0x5b, 0xdb, 0x76, 0xd0, 0x78, 0xc4,
- 0x3b, 0x8a, 0xc0, 0x06, 0x4e, 0x4a, 0x01, 0x64,
- 0x61, 0x2b, 0x1f, 0xce, 0x77, 0xc8, 0x69, 0x34,
- 0x5b, 0xfc, 0x94, 0xc7, 0x58, 0x94, 0xed, 0xd3};
- BOOST_CHECK_EQUAL_COLLECTIONS(result->begin(), result->end(),
- expectedSha256, expectedSha256 + sizeof(expectedSha256));
-}
-
-BOOST_AUTO_TEST_SUITE_END() // TestCrypto
-BOOST_AUTO_TEST_SUITE_END() // Util
-
-} // namespace tests
-} // namespace crypto
-} // namespace ndn
diff --git a/tests/unit-tests/util/digest.t.cpp b/tests/unit-tests/util/digest.t.cpp
index 36a72e2..d8614c1 100644
--- a/tests/unit-tests/util/digest.t.cpp
+++ b/tests/unit-tests/util/digest.t.cpp
@@ -21,6 +21,7 @@
#include "util/digest.hpp"
#include "util/string-helper.hpp"
+#include "encoding/endian.hpp"
#include "boost-test.hpp"
@@ -33,44 +34,44 @@
BOOST_AUTO_TEST_CASE(Basic)
{
- const uint8_t origin[] = {0x01, 0x02, 0x03, 0x04};
- ConstBufferPtr digest1 = crypto::computeSha256Digest(origin, sizeof(origin));
+ const uint8_t input[] = {0x01, 0x02, 0x03, 0x04};
+ auto expected = fromHex("9f64a747e1b97f131fabb6b447296c9b6f0201e79fb3c5356e6c77e89b6a806a");
Sha256 statefulSha256;
BOOST_CHECK_EQUAL(statefulSha256.empty(), true);
- statefulSha256.update(origin, 1);
- statefulSha256.update(origin + 1, 1);
- statefulSha256.update(origin + 2, 1);
- statefulSha256.update(origin + 3, 1);
- ConstBufferPtr digest2 = statefulSha256.computeDigest();
- BOOST_CHECK_EQUAL_COLLECTIONS(digest1->buf(), digest1->buf() + digest1->size(),
- digest2->buf(), digest2->buf() + digest2->size());
+ statefulSha256.update(input, 1);
+ statefulSha256.update(input + 1, 1);
+ statefulSha256.update(input + 2, 1);
+ statefulSha256.update(input + 3, 1);
+ ConstBufferPtr digest = statefulSha256.computeDigest();
+ BOOST_CHECK_EQUAL(digest->size(), Sha256::DIGEST_SIZE);
+ BOOST_CHECK_EQUAL_COLLECTIONS(expected->buf(), expected->buf() + expected->size(),
+ digest->buf(), digest->buf() + digest->size());
}
BOOST_AUTO_TEST_CASE(ConstructFromStream)
{
- const std::string input = "Hello, World!";
- ConstBufferPtr digest1 = crypto::computeSha256Digest(reinterpret_cast<const uint8_t*>(input.data()),
- input.size());
+ const std::string input = "Hello, world!";
+ auto expected = fromHex("315f5bdb76d078c43b8ac0064e4a0164612b1fce77c869345bfc94c75894edd3");
std::istringstream is(input);
Sha256 sha(is);
BOOST_CHECK_EQUAL(sha.empty(), false);
- BOOST_CHECK_EQUAL(sha.toString(), "DFFD6021BB2BD5B0AF676290809EC3A53191DD81C7F70A4B28688A362182986F");
+ BOOST_CHECK_EQUAL(sha.toString(), "315F5BDB76D078C43B8AC0064E4A0164612B1FCE77C869345BFC94C75894EDD3");
- ConstBufferPtr digest2 = sha.computeDigest();
- BOOST_CHECK_EQUAL_COLLECTIONS(digest1->buf(), digest1->buf() + digest1->size(),
- digest2->buf(), digest2->buf() + digest2->size());
+ ConstBufferPtr digest = sha.computeDigest();
+ BOOST_CHECK_EQUAL_COLLECTIONS(expected->buf(), expected->buf() + expected->size(),
+ digest->buf(), digest->buf() + digest->size());
}
BOOST_AUTO_TEST_CASE(Compare)
{
const uint8_t origin[] = {0x01, 0x02, 0x03, 0x04};
- Sha256 digest;
- digest.update(origin, sizeof(origin));
- digest.computeDigest();
+ Sha256 digest1;
+ digest1.update(origin, sizeof(origin));
+ digest1.computeDigest();
Sha256 digest2;
digest2.update(origin, 1);
@@ -79,46 +80,43 @@
digest2.update(origin + 3, 1);
digest2.computeDigest();
- BOOST_CHECK_EQUAL(digest == digest2, true);
- BOOST_CHECK_EQUAL(digest != digest2, false);
+ BOOST_CHECK_EQUAL(digest1 == digest2, true);
+ BOOST_CHECK_EQUAL(digest1 != digest2, false);
}
BOOST_AUTO_TEST_CASE(InsertionOperatorSha256)
{
- const uint8_t origin[] = {0x94, 0xEE, 0x05, 0x93, 0x35, 0xE5, 0x87, 0xE5,
- 0x01, 0xCC, 0x4B, 0xF9, 0x06, 0x13, 0xE0, 0x81,
- 0x4F, 0x00, 0xA7, 0xB0, 0x8B, 0xC7, 0xC6, 0x48,
- 0xFD, 0x86, 0x5A, 0x2A, 0xF6, 0xA2, 0x2C, 0xC2};
- ConstBufferPtr digest1 = crypto::computeSha256Digest(origin, sizeof(origin));
+ auto expected = fromHex("d7bd34bfe44a18d2aa755a344fe3e6b06ed0473772e6dfce16ac71ba0b0a241c");
Sha256 innerDigest;
innerDigest << "TEST";
Sha256 statefulSha256;
statefulSha256 << innerDigest;
- ConstBufferPtr digest2 = statefulSha256.computeDigest();
+ ConstBufferPtr digest = statefulSha256.computeDigest();
- BOOST_CHECK_EQUAL_COLLECTIONS(digest1->buf(), digest1->buf() + digest1->size(),
- digest2->buf(), digest2->buf() + digest2->size());
+ BOOST_CHECK_EQUAL(statefulSha256.empty(), false);
+ BOOST_CHECK_EQUAL_COLLECTIONS(expected->buf(), expected->buf() + expected->size(),
+ digest->buf(), digest->buf() + digest->size());
}
BOOST_AUTO_TEST_CASE(InsertionOperatorString)
{
- const std::string str = "Hello, World!";
- ConstBufferPtr digest1 = crypto::computeSha256Digest(reinterpret_cast<const uint8_t*>(str.data()),
- str.size());
+ const std::string input = "Hello, world!";
+ auto expected = fromHex("315f5bdb76d078c43b8ac0064e4a0164612b1fce77c869345bfc94c75894edd3");
Sha256 statefulSha256;
- statefulSha256 << str;
- ConstBufferPtr digest2 = statefulSha256.computeDigest();
+ statefulSha256 << input;
+ ConstBufferPtr digest = statefulSha256.computeDigest();
- BOOST_CHECK_EQUAL_COLLECTIONS(digest1->buf(), digest1->buf() + digest1->size(),
- digest2->buf(), digest2->buf() + digest2->size());
+ BOOST_CHECK_EQUAL(statefulSha256.empty(), false);
+ BOOST_CHECK_EQUAL_COLLECTIONS(expected->buf(), expected->buf() + expected->size(),
+ digest->buf(), digest->buf() + digest->size());
}
BOOST_AUTO_TEST_CASE(InsertionOperatorBlock)
{
- const uint8_t origin[] = {
+ const uint8_t input[] = {
0x16, 0x1b, // SignatureInfo
0x1b, 0x01, // SignatureType
0x01, // Sha256WithRsa
@@ -131,32 +129,31 @@
0x08, 0x07,
0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72
};
- ConstBufferPtr digest1 = crypto::computeSha256Digest(origin, sizeof(origin));
+ auto expected = fromHex("b372edfd4d6a4db2cfeaeead6c34fdee9b9e759f7b8d799cf8067e39e7f2886c");
Sha256 statefulSha256;
- Block block(origin, sizeof(origin));
- statefulSha256 << block;
- ConstBufferPtr digest2 = statefulSha256.computeDigest();
+ statefulSha256 << Block{input, sizeof(input)};
+ ConstBufferPtr digest = statefulSha256.computeDigest();
- BOOST_CHECK_EQUAL_COLLECTIONS(digest1->buf(), digest1->buf() + digest1->size(),
- digest2->buf(), digest2->buf() + digest2->size());
+ BOOST_CHECK_EQUAL(statefulSha256.empty(), false);
+ BOOST_CHECK_EQUAL_COLLECTIONS(expected->buf(), expected->buf() + expected->size(),
+ digest->buf(), digest->buf() + digest->size());
}
BOOST_AUTO_TEST_CASE(InsertionOperatorUint64t)
{
- const uint64_t origin[] = {1, 2, 3, 4};
- ConstBufferPtr digest1 = crypto::computeSha256Digest(reinterpret_cast<const uint8_t*>(origin),
- sizeof(origin));
+ const uint64_t input[] = {1, 2, 3, 4};
+ auto expected = fromHex("7236c00c170036c6de133a878210ddd58567aa1d0619a0f70f69e38ae6f916e9");
Sha256 statefulSha256;
- statefulSha256 << origin[0];
- statefulSha256 << origin[1];
- statefulSha256 << origin[2];
- statefulSha256 << origin[3];
- ConstBufferPtr digest2 = statefulSha256.computeDigest();
+ for (size_t i = 0; i < sizeof(input) / sizeof(uint64_t); ++i) {
+ statefulSha256 << htobe64(input[i]);
+ }
+ ConstBufferPtr digest = statefulSha256.computeDigest();
- BOOST_CHECK_EQUAL_COLLECTIONS(digest1->buf(), digest1->buf() + digest1->size(),
- digest2->buf(), digest2->buf() + digest2->size());
+ BOOST_CHECK_EQUAL(statefulSha256.empty(), false);
+ BOOST_CHECK_EQUAL_COLLECTIONS(expected->buf(), expected->buf() + expected->size(),
+ digest->buf(), digest->buf() + digest->size());
}
BOOST_AUTO_TEST_CASE(Reset)
@@ -183,11 +180,12 @@
BOOST_AUTO_TEST_CASE(StaticComputeDigest)
{
- const uint8_t origin[] = {0x01, 0x02, 0x03, 0x04};
- ConstBufferPtr digest1 = crypto::computeSha256Digest(origin, sizeof(origin));
- ConstBufferPtr digest2 = Sha256::computeDigest(origin, sizeof(origin));
- BOOST_CHECK_EQUAL_COLLECTIONS(digest1->buf(), digest1->buf() + digest1->size(),
- digest2->buf(), digest2->buf() + digest2->size());
+ const uint8_t input[] = {0x01, 0x02, 0x03, 0x04};
+ auto expected = fromHex("9f64a747e1b97f131fabb6b447296c9b6f0201e79fb3c5356e6c77e89b6a806a");
+
+ ConstBufferPtr digest = Sha256::computeDigest(input, sizeof(input));
+ BOOST_CHECK_EQUAL_COLLECTIONS(expected->buf(), expected->buf() + expected->size(),
+ digest->buf(), digest->buf() + digest->size());
}
BOOST_AUTO_TEST_CASE(Print)
@@ -196,14 +194,14 @@
0x01, 0xCC, 0x4B, 0xF9, 0x06, 0x13, 0xE0, 0x81,
0x4F, 0x00, 0xA7, 0xB0, 0x8B, 0xC7, 0xC6, 0x48,
0xFD, 0x86, 0x5A, 0x2A, 0xF6, 0xA2, 0x2C, 0xC2};
- std::string hexString = toHex(origin, sizeof(origin));
+ std::string expected = toHex(origin, sizeof(origin));
Sha256 digest;
digest << "TEST";
std::ostringstream os;
os << digest;
- BOOST_CHECK_EQUAL(os.str(), hexString);
- BOOST_CHECK_EQUAL(digest.toString(), hexString);
+ BOOST_CHECK_EQUAL(os.str(), expected);
+ BOOST_CHECK_EQUAL(digest.toString(), expected);
}
BOOST_AUTO_TEST_SUITE_END() // TestSha256
diff --git a/tests/unit-tests/util/in-memory-storage-common.t.cpp b/tests/unit-tests/util/in-memory-storage-common.t.cpp
index a4a992b..5c83391 100644
--- a/tests/unit-tests/util/in-memory-storage-common.t.cpp
+++ b/tests/unit-tests/util/in-memory-storage-common.t.cpp
@@ -1,5 +1,5 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
+/*
* Copyright (c) 2013-2017 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
@@ -23,7 +23,7 @@
#include "util/in-memory-storage-fifo.hpp"
#include "util/in-memory-storage-lfu.hpp"
#include "util/in-memory-storage-lru.hpp"
-#include "util/crypto.hpp"
+#include "util/digest.hpp"
#include "security/signature-sha256-with-rsa.hpp"
#include "boost-test.hpp"
@@ -274,7 +274,7 @@
{
shared_ptr<Data> data = makeData("/digest/compute");
- ConstBufferPtr digest1 = crypto::computeSha256Digest(data->wireEncode().wire(), data->wireEncode().size());
+ ConstBufferPtr digest1 = Sha256::computeDigest(data->wireEncode().wire(), data->wireEncode().size());
BOOST_CHECK_EQUAL(digest1->size(), 32);
InMemoryStorageEntry entry;
@@ -372,7 +372,7 @@
shared_ptr<Data> data7 = makeData("/c/c/1");
ims.insert(*data7);
- ConstBufferPtr digest1 = crypto::computeSha256Digest(data->wireEncode().wire(), data->wireEncode().size());
+ ConstBufferPtr digest1 = Sha256::computeDigest(data->wireEncode().wire(), data->wireEncode().size());
Name name("/a");
ims.erase(name);
@@ -393,7 +393,7 @@
shared_ptr<Data> data3 = makeData("/z/z/z");
ims.insert(*data3);
- ConstBufferPtr digest1 = crypto::computeSha256Digest(data->wireEncode().wire(), data->wireEncode().size());
+ ConstBufferPtr digest1 = Sha256::computeDigest(data->wireEncode().wire(), data->wireEncode().size());
shared_ptr<Interest> interest = makeInterest("");
interest->setName(Name(name).appendImplicitSha256Digest(digest1->buf(), digest1->size()));
@@ -928,9 +928,9 @@
Name n2 = insert(2, "ndn:/A");
insert(3, "ndn:/A/B");
- uint8_t digest00[crypto::SHA256_DIGEST_SIZE];
+ uint8_t digest00[Sha256::DIGEST_SIZE];
std::fill_n(digest00, sizeof(digest00), 0x00);
- uint8_t digestFF[crypto::SHA256_DIGEST_SIZE];
+ uint8_t digestFF[Sha256::DIGEST_SIZE];
std::fill_n(digestFF, sizeof(digestFF), 0xFF);
Exclude excludeDigest;