wipeable_string: add hex_to_pod function

This commit is contained in:
moneromooo-monero 2018-08-27 08:43:56 +00:00
parent 91c7d68b2d
commit 07ec748c82
No known key found for this signature in database
GPG key ID: 686F07454D6CEFC3
4 changed files with 28 additions and 2 deletions

View file

@ -44,6 +44,7 @@ namespace epee
static std::string string(const span<const std::uint8_t> src); static std::string string(const span<const std::uint8_t> src);
//! \return A epee::wipeable_string containing hex of `src`. //! \return A epee::wipeable_string containing hex of `src`.
static epee::wipeable_string wipeable_string(const span<const std::uint8_t> src); static epee::wipeable_string wipeable_string(const span<const std::uint8_t> src);
template<typename T> static epee::wipeable_string wipeable_string(const T &pod) { return wipeable_string(span<const uint8_t>((const uint8_t*)&pod, sizeof(pod))); }
//! \return An array containing hex of `src`. //! \return An array containing hex of `src`.
template<std::size_t N> template<std::size_t N>

View file

@ -28,10 +28,11 @@
#pragma once #pragma once
#include <boost/optional/optional_fwd.hpp> #include <boost/optional/optional.hpp>
#include <stddef.h> #include <stddef.h>
#include <vector> #include <vector>
#include <string> #include <string>
#include "memwipe.h"
#include "fnv1.h" #include "fnv1.h"
namespace epee namespace epee
@ -65,6 +66,8 @@ namespace epee
void trim(); void trim();
void split(std::vector<wipeable_string> &fields) const; void split(std::vector<wipeable_string> &fields) const;
boost::optional<wipeable_string> parse_hexstr() const; boost::optional<wipeable_string> parse_hexstr() const;
template<typename T> inline bool hex_to_pod(T &pod) const;
template<typename T> inline bool hex_to_pod(tools::scrubbed<T> &pod) const { return hex_to_pod(unwrap(pod)); }
void resize(size_t sz); void resize(size_t sz);
void reserve(size_t sz); void reserve(size_t sz);
void clear(); void clear();
@ -79,6 +82,20 @@ namespace epee
private: private:
std::vector<char> buffer; std::vector<char> buffer;
}; };
template<typename T> inline bool wipeable_string::hex_to_pod(T &pod) const
{
static_assert(std::is_pod<T>::value, "expected pod type");
if (size() != sizeof(T) * 2)
return false;
boost::optional<epee::wipeable_string> blob = parse_hexstr();
if (!blob)
return false;
if (blob->size() != sizeof(T))
return false;
pod = *(const T*)blob->data();
return true;
}
} }
namespace std namespace std

View file

@ -32,6 +32,8 @@
#include "misc_log_ex.h" #include "misc_log_ex.h"
#include "wipeable_string.h" #include "wipeable_string.h"
static constexpr const char hex[] = u8"0123456789abcdef";
namespace namespace
{ {
int atolower(int c) int atolower(int c)
@ -197,7 +199,6 @@ boost::optional<epee::wipeable_string> wipeable_string::parse_hexstr() const
const size_t len = size(); const size_t len = size();
const char *d = data(); const char *d = data();
res->grow(0, len / 2); res->grow(0, len / 2);
static constexpr const char hex[] = u8"0123456789abcdef";
for (size_t i = 0; i < len; i += 2) for (size_t i = 0; i < len; i += 2)
{ {
char c = atolower(d[i]); char c = atolower(d[i]);

View file

@ -32,6 +32,7 @@
#include "misc_log_ex.h" #include "misc_log_ex.h"
#include "wipeable_string.h" #include "wipeable_string.h"
#include "hex.h"
TEST(wipeable_string, ctor) TEST(wipeable_string, ctor)
{ {
@ -202,3 +203,9 @@ TEST(wipeable_string, parse_hexstr)
ASSERT_TRUE((s = epee::wipeable_string("414243").parse_hexstr())); ASSERT_TRUE((s = epee::wipeable_string("414243").parse_hexstr()));
ASSERT_EQ(*s, epee::wipeable_string("ABC")); ASSERT_EQ(*s, epee::wipeable_string("ABC"));
} }
TEST(wipeable_string, to_hex)
{
ASSERT_TRUE(epee::to_hex::wipeable_string(epee::span<const uint8_t>((const uint8_t*)"", 0)) == epee::wipeable_string(""));
ASSERT_TRUE(epee::to_hex::wipeable_string(epee::span<const uint8_t>((const uint8_t*)"abc", 3)) == epee::wipeable_string("616263"));
}