25 #ifndef CRYPTO3_DIGEST_HPP
26 #define CRYPTO3_DIGEST_HPP
30 #include <boost/static_assert.hpp>
31 #include <boost/assert.hpp>
33 #include <boost/container/small_vector.hpp>
35 #include <nil/crypto3/detail/pack.hpp>
36 #include <nil/crypto3/detail/octet.hpp>
70 template<std::
size_t DigestBits>
71 using digest = boost::container::small_vector<
octet_type, DigestBits / octet_bits>;
74 template<std::
size_t DigestBits,
typename OutputIterator>
75 OutputIterator
to_ascii(
const boost::container::small_vector<octet_type, DigestBits / octet_bits> &d,
77 for (std::size_t j = 0; j < d.size(); ++j) {
79 *it++ =
"0123456789abcdef"[(b >> 4) & 0xF];
80 *it++ =
"0123456789abcdef"[(b >> 0) & 0xF];
85 template<std::
size_t DigestBits>
87 c_str(
const boost::container::small_vector<octet_type, DigestBits / octet_bits> &d) {
88 digest<DigestBits / 4 + 1> s;
89 to_ascii<DigestBits>(d, std::back_inserter(s));
103 template<
unsigned NewBits,
unsigned OldBits>
106 unsigned bytes =
sizeof(
octet_type) * (NewBits < OldBits ? NewBits : OldBits) / octet_bits;
107 std::memcpy(nd.data(), od.data(), bytes);
121 template<
unsigned NewBits,
unsigned OldBits>
122 digest<NewBits>
truncate(
const boost::container::small_vector<octet_type, OldBits / octet_bits> &od) {
123 BOOST_STATIC_ASSERT(NewBits <= OldBits);
124 return resize<NewBits>(od);
127 template<
unsigned DB1,
unsigned DB2>
128 bool operator==(
const digest<DB1> &a,
const digest<DB2> &b) {
129 unsigned const DB = DB1 < DB2 ? DB2 : DB1;
130 return resize<DB>(a).base_array() == resize<DB>(b).base_array();
133 template<
unsigned DB1,
unsigned DB2>
134 bool operator!=(
const digest<DB1> &a,
const digest<DB2> &b) {
138 template<
unsigned DB1,
unsigned DB2>
139 bool operator<(
const digest<DB1> &a,
const digest<DB2> &b) {
140 unsigned const DB = DB1 < DB2 ? DB2 : DB1;
141 return resize<DB>(a).base_array() < resize<DB>(b).base_array();
144 template<
unsigned DB1,
unsigned DB2>
145 bool operator>(
const digest<DB1> &a,
const digest<DB2> &b) {
149 template<
unsigned DB1,
unsigned DB2>
150 bool operator<=(
const digest<DB1> &a,
const digest<DB2> &b) {
154 template<
unsigned DB1,
unsigned DB2>
155 bool operator>=(
const digest<DB1> &a,
const digest<DB2> &b) {
159 template<
unsigned DB>
160 bool operator!=(digest<DB>
const &a,
char const *b) {
161 BOOST_ASSERT(std::strlen(b) == DB / 4);
162 return static_cast<bool>(std::strcmp(a.cstring().data(), b));
165 template<
unsigned DB>
166 bool operator==(digest<DB>
const &a,
char const *b) {
170 template<
unsigned DB>
171 bool operator!=(
char const *b, digest<DB>
const &a) {
175 template<
unsigned DB>
176 bool operator==(
char const *b, digest<DB>
const &a) {
180 template<
unsigned DB>
181 std::ostream &
operator<<(std::ostream &sink, digest<DB>
const &d) {
182 d.to_ascii(std::ostream_iterator<char>(sink));
186 template<
unsigned DB>
187 std::istream &
operator>>(std::istream &source, digest<DB> &d) {
188 std::array<char, DB / 4> a = {{}};
189 for (
unsigned i = 0; i < a.size(); ++i) {
191 if (!source.get(c)) {
192 source.setstate(std::ios::failbit);
195 if (!std::isxdigit(c, source.getloc())) {
197 source.setstate(std::ios::failbit);
201 if (std::isdigit(c, source.getloc())) {
204 a[i] = std::toupper(c, source.getloc()) -
'A' + 0xA;
207 detail::pack<stream_endian::big_bit, 4, 8>(a, d);
214 template<std::
size_t DigestBits>
217 return std::string(cstr.begin(), cstr.begin() + cstr.size() - 1);
std::string to_string(const nil::crypto3::digest< DigestBits > &d)
Definition: block/include/nil/crypto3/detail/digest.hpp:248
OutputIterator to_ascii(const digest< DigestBits > &d, OutputIterator it)
Definition: block/include/nil/crypto3/detail/digest.hpp:86
digest< DigestBits/4+1 > c_str(const digest< DigestBits > &d)
Definition: block/include/nil/crypto3/detail/digest.hpp:98
boost::uint_t< octet_bits >::least octet_type
Definition: algebra/include/nil/crypto3/detail/octet.hpp:33
std::istream & operator>>(std::istream &source, digest< DB > &d)
Definition: block/include/nil/crypto3/detail/digest.hpp:220
bool operator>(const digest< DB1 > &a, const digest< DB2 > &b)
Definition: block/include/nil/crypto3/detail/digest.hpp:178
bool operator<(const digest< DB1 > &a, const digest< DB2 > &b)
Definition: block/include/nil/crypto3/detail/digest.hpp:172
bool operator!=(const secure_allocator< T > &, const secure_allocator< U > &)
Definition: secure_allocator.hpp:98
std::ostream & operator<<(std::ostream &sink, digest< DB > const &d)
Definition: block/include/nil/crypto3/detail/digest.hpp:214
bool operator==(const secure_allocator< T > &, const secure_allocator< U > &)
Definition: secure_allocator.hpp:93
digest< DigestBits > resize(const digest< DigestBits > &od, std::size_t new_size)
Definition: block/include/nil/crypto3/detail/digest.hpp:131
boost::container::small_vector< octet_type, DigestBits/octet_bits > digest
Definition: codec/include/nil/crypto3/detail/digest.hpp:71
digest< NewBits > truncate(const digest< OldBits > &od)
Definition: block/include/nil/crypto3/detail/digest.hpp:155
bool operator<=(const digest< DB1 > &a, const digest< DB2 > &b)
Definition: block/include/nil/crypto3/detail/digest.hpp:183
bool operator>=(const digest< DB1 > &a, const digest< DB2 > &b)
Definition: block/include/nil/crypto3/detail/digest.hpp:188
Definition: block/include/nil/crypto3/detail/digest.hpp:72