util: Improvements of string helpers
Improvements include:
- test cases for every string helper
- a new `printHex` helper to output hex conversion directly to std::ostream
- new `toHex` and `printHex` helpers that accept Buffer as input parameter
- a new `fromHex` helper to convert the supplied string to shared_ptr<const Buffer>
- replaced uses of CryptoPP routines with `toHex` and `fromHex` helpers where applicable
Change-Id: I092c9fa8fd21c413b53ea5624b82f769287bb42c
Refs: #3006
diff --git a/src/name-component.cpp b/src/name-component.cpp
index 576fac4..dbe9998 100644
--- a/src/name-component.cpp
+++ b/src/name-component.cpp
@@ -102,16 +102,10 @@
"(expected sha256 in hex encoding)");
try {
- std::string value;
- CryptoPP::StringSource(reinterpret_cast<const uint8_t*>(trimmedString.c_str()) +
- getSha256DigestUriPrefix().size(),
- trimmedString.size () - getSha256DigestUriPrefix().size(), true,
- new CryptoPP::HexDecoder(new CryptoPP::StringSink(value)));
-
- return fromImplicitSha256Digest(reinterpret_cast<const uint8_t*>(value.c_str()),
- value.size());
+ trimmedString.erase(0, getSha256DigestUriPrefix().size());
+ return fromImplicitSha256Digest(fromHex(trimmedString));
}
- catch (CryptoPP::Exception& e) {
+ catch (StringHelperError& e) {
throw Error("Cannot convert to a ImplicitSha256DigestComponent (invalid hex encoding)");
}
}
@@ -139,8 +133,7 @@
if (type() == tlv::ImplicitSha256DigestComponent) {
result << getSha256DigestUriPrefix();
- CryptoPP::StringSource(value(), value_size(), true,
- new CryptoPP::HexEncoder(new CryptoPP::FileSink(result), false));
+ printHex(result, value(), value_size(), false);
}
else {
const uint8_t* value = this->value();