stream/include/nil/crypto3/detail/digest.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2018-2020 Mikhail Komarov <nemo@nil.foundation>
3 //
4 // MIT License
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in all
14 // copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 // SOFTWARE.
23 //---------------------------------------------------------------------------//
24 
25 #ifndef CRYPTO3_DIGEST_HPP
26 #define CRYPTO3_DIGEST_HPP
27 
28 #include <boost/static_assert.hpp>
29 #include <boost/assert.hpp>
30 
31 #include <boost/container/small_vector.hpp>
32 
33 #include <nil/crypto3/detail/pack.hpp>
34 #include <nil/crypto3/detail/octet.hpp>
35 
36 namespace nil {
37  namespace crypto3 {
68  template<std::size_t DigestBits>
69  using digest = boost::container::small_vector<octet_type, DigestBits / octet_bits>;
70 
71  namespace detail {
72  template<std::size_t DigestBits, typename OutputIterator>
73  OutputIterator to_ascii(const boost::container::small_vector<octet_type, DigestBits / octet_bits> &d,
74  OutputIterator it) {
75  for (std::size_t j = 0; j < d.size(); ++j) {
76  octet_type b = d[j];
77  *it++ = "0123456789abcdef"[(b >> 4) & 0xF];
78  *it++ = "0123456789abcdef"[(b >> 0) & 0xF];
79  }
80  return it;
81  }
82 
83  template<std::size_t DigestBits>
84  digest<DigestBits / 4 + 1>
85  c_str(const boost::container::small_vector<octet_type, DigestBits / octet_bits> &d) {
86  digest<DigestBits / 4 + 1> s;
87  to_ascii<DigestBits>(d, std::back_inserter(s));
88  s.push_back('\0');
89  return s;
90  }
91  } // namespace detail
92 
101  template<unsigned NewBits, unsigned OldBits>
102  digest<NewBits> resize(const boost::container::small_vector<octet_type, OldBits / octet_bits> &od) {
103  digest<NewBits> nd;
104  unsigned bytes = sizeof(octet_type) * (NewBits < OldBits ? NewBits : OldBits) / octet_bits;
105  std::memcpy(nd.data(), od.data(), bytes);
106  return nd;
107  }
108 
119  template<unsigned NewBits, unsigned OldBits>
120  digest<NewBits> truncate(const boost::container::small_vector<octet_type, OldBits / octet_bits> &od) {
121  BOOST_STATIC_ASSERT(NewBits <= OldBits);
122  return resize<NewBits>(od);
123  }
124 
125  template<unsigned DB1, unsigned DB2>
126  bool operator==(const digest<DB1> &a, const digest<DB2> &b) {
127  unsigned const DB = DB1 < DB2 ? DB2 : DB1;
128  return resize<DB>(a).base_array() == resize<DB>(b).base_array();
129  }
130 
131  template<unsigned DB1, unsigned DB2>
132  bool operator!=(const digest<DB1> &a, const digest<DB2> &b) {
133  return !(a == b);
134  }
135 
136  template<unsigned DB1, unsigned DB2>
137  bool operator<(const digest<DB1> &a, const digest<DB2> &b) {
138  unsigned const DB = DB1 < DB2 ? DB2 : DB1;
139  return resize<DB>(a).base_array() < resize<DB>(b).base_array();
140  }
141 
142  template<unsigned DB1, unsigned DB2>
143  bool operator>(const digest<DB1> &a, const digest<DB2> &b) {
144  return b < a;
145  }
146 
147  template<unsigned DB1, unsigned DB2>
148  bool operator<=(const digest<DB1> &a, const digest<DB2> &b) {
149  return !(b < a);
150  }
151 
152  template<unsigned DB1, unsigned DB2>
153  bool operator>=(const digest<DB1> &a, const digest<DB2> &b) {
154  return !(b > a);
155  }
156 
157  template<unsigned DB>
158  bool operator!=(digest<DB> const &a, char const *b) {
159  BOOST_ASSERT(std::strlen(b) == DB / 4);
160  return static_cast<bool>(std::strcmp(a.cstring().data(), b));
161  }
162 
163  template<unsigned DB>
164  bool operator==(digest<DB> const &a, char const *b) {
165  return !(a != b);
166  }
167 
168  template<unsigned DB>
169  bool operator!=(char const *b, digest<DB> const &a) {
170  return a != b;
171  }
172 
173  template<unsigned DB>
174  bool operator==(char const *b, digest<DB> const &a) {
175  return a == b;
176  }
177 
178  template<unsigned DB>
179  std::ostream &operator<<(std::ostream &sink, digest<DB> const &d) {
180  d.to_ascii(std::ostream_iterator<char>(sink));
181  return sink;
182  }
183 
184  template<unsigned DB>
185  std::istream &operator>>(std::istream &source, digest<DB> &d) {
186  std::array<char, DB / 4> a = {{}};
187  for (unsigned i = 0; i < a.size(); ++i) {
188  char c;
189  if (!source.get(c)) {
190  source.setstate(std::ios::failbit);
191  break;
192  }
193  if (!std::isxdigit(c, source.getloc())) {
194  source.unget();
195  source.setstate(std::ios::failbit);
196  break;
197  }
198 
199  if (std::isdigit(c, source.getloc())) {
200  a[i] = (c - '0');
201  } else {
202  a[i] = std::toupper(c, source.getloc()) - 'A' + 0xA;
203  }
204  }
205  detail::pack<stream_endian::big_bit, 4, 8>(a, d);
206  return source;
207  }
208  } // namespace crypto3
209 } // namespace nil
210 
211 namespace std {
212  template<std::size_t DigestBits>
213  std::string to_string(const nil::crypto3::digest<DigestBits> &d) {
214  nil::crypto3::digest<DigestBits / 4 + 1> cstr = nil::crypto3::detail::c_str(d);
215  return std::string(cstr.begin(), cstr.begin() + cstr.size() - 1);
216  }
217 } // namespace std
218 
219 #endif // CRYPTO3_DIGEST_HPP
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: pair.hpp:31
Definition: block/include/nil/crypto3/detail/digest.hpp:72