util: rewrite toHex using security::transform instead of printHex
Change-Id: If07c434419f44decc866c97adf083051597e7ba6
Refs: #3006
diff --git a/src/util/string-helper.cpp b/src/util/string-helper.cpp
index ed3c961..44083b3 100644
--- a/src/util/string-helper.cpp
+++ b/src/util/string-helper.cpp
@@ -23,6 +23,7 @@
#include "../encoding/buffer-stream.hpp"
#include "../security/transform/buffer-source.hpp"
#include "../security/transform/hex-decode.hpp"
+#include "../security/transform/hex-encode.hpp"
#include "../security/transform/stream-sink.hpp"
#include <boost/algorithm/string/trim.hpp>
@@ -33,13 +34,13 @@
namespace ndn {
void
-printHex(std::ostream& os, const uint8_t* buffer, size_t length, bool isUpperCase/* = true*/)
+printHex(std::ostream& os, const uint8_t* buffer, size_t length, bool wantUpperCase)
{
if (buffer == nullptr || length == 0)
return;
auto newFlags = std::ios::hex;
- if (isUpperCase) {
+ if (wantUpperCase) {
newFlags |= std::ios::uppercase;
}
auto oldFlags = os.flags(newFlags);
@@ -52,26 +53,31 @@
}
void
-printHex(std::ostream& os, const Buffer& buffer, bool isUpperCase/* = true*/)
+printHex(std::ostream& os, const Buffer& buffer, bool wantUpperCase)
{
- return printHex(os, buffer.buf(), buffer.size(), isUpperCase);
+ return printHex(os, buffer.buf(), buffer.size(), wantUpperCase);
}
std::string
-toHex(const uint8_t* buffer, size_t length, bool isUpperCase/* = true*/)
+toHex(const uint8_t* buffer, size_t length, bool wantUpperCase)
{
- if (buffer == nullptr || length == 0)
- return "";
+ BOOST_ASSERT(buffer != nullptr || length == 0);
+
+ namespace tr = security::transform;
std::ostringstream result;
- printHex(result, buffer, length, isUpperCase);
+ tr::bufferSource(buffer, length) >> tr::hexEncode(wantUpperCase) >> tr::streamSink(result);
return result.str();
}
std::string
-toHex(const Buffer& buffer, bool isUpperCase/* = true*/)
+toHex(const Buffer& buffer, bool wantUpperCase)
{
- return toHex(buffer.buf(), buffer.size(), isUpperCase);
+ namespace tr = security::transform;
+
+ std::ostringstream result;
+ tr::bufferSource(buffer) >> tr::hexEncode(wantUpperCase) >> tr::streamSink(result);
+ return result.str();
}
int