diff --git a/include/opendht/infohash.h b/include/opendht/infohash.h index e6d23b78..cd24f638 100644 --- a/include/opendht/infohash.h +++ b/include/opendht/infohash.h @@ -42,6 +42,7 @@ typedef uint16_t in_port_t; #include #include #include +#include #include @@ -88,10 +89,46 @@ public: std::copy_n(o.via.bin.ptr, N, data_.data()); } + template + OPENDHT_PUBLIC friend std::ostream& operator<< (std::ostream& s, const Hash& h); + + template + OPENDHT_PUBLIC friend std::istream& operator>> (std::istream& s, Hash& h); + + std::string toString() const { + std::stringstream ss; + ss << *this; + return ss.str(); + } + private: std::array data_; }; +template +std::ostream& operator<< (std::ostream& s, const Hash& h) +{ + s << std::hex; + for (unsigned i=0; i +std::istream& operator>> (std::istream& s, Hash& h) +{ + std::array dat; + s.exceptions(std::istream::eofbit | std::istream::failbit); + s.read(&(*dat.begin()), dat.size()); + for (size_t i = 0; i < h.size(); i++) { + unsigned res = 0; + sscanf(dat.data() + 2*i, "%02x", &res); + h[i] = res; + } + return s; +} + using h256 = Hash<32>; using PkId = h256;