26 #ifndef CRYPTO3_DIGEST_HPP
27 #define CRYPTO3_DIGEST_HPP
31 #include <boost/static_assert.hpp>
32 #include <boost/assert.hpp>
34 #include <boost/container/small_vector.hpp>
36 #include <nil/crypto3/detail/pack.hpp>
37 #include <nil/crypto3/detail/octet.hpp>
71 template<std::
size_t DigestBits>
72 struct digest :
public boost::container::small_vector<octet_type, DigestBits / octet_bits> {
85 template<std::
size_t DigestBits,
typename OutputIterator>
86 OutputIterator
to_ascii(
const digest<DigestBits> &d,
88 for (std::size_t j = 0; j < d.size(); ++j) {
90 *it++ =
"0123456789abcdef"[(b >> 4) & 0xF];
91 *it++ =
"0123456789abcdef"[(b >> 0) & 0xF];
96 template<std::
size_t DigestBits>
98 c_str(
const digest<DigestBits> &d) {
99 digest<DigestBits / 4 + 1> s;
100 to_ascii<DigestBits>(d, std::back_inserter(s));
114 template<
unsigned NewBits,
unsigned OldBits>
115 digest<NewBits>
reserve(
const digest< OldBits > &od) {
117 unsigned bytes =
sizeof(
octet_type) * (NewBits < OldBits ? NewBits : OldBits) / octet_bits;
118 std::memcpy(nd.data(), od.data(), bytes);
130 template<std::
size_t DigestBits>
131 digest<DigestBits>
resize(
const digest<DigestBits> &od, std::size_t new_size) {
133 std::size_t old_size = od.size();
135 if (new_size == old_size)
138 digest<DigestBits> nd(new_size,
octet_type());
139 std::size_t bytes =
sizeof(
octet_type) * (old_size < new_size ? old_size : new_size);
140 std::memcpy(nd.data(), od.data(), bytes);
154 template<
unsigned NewBits,
unsigned OldBits>
155 digest<NewBits>
truncate(
const digest< OldBits > &od) {
156 BOOST_STATIC_ASSERT(NewBits <= OldBits);
157 return resize<NewBits>(od);
160 template<
unsigned DB1,
unsigned DB2>
161 bool operator==(
const digest<DB1> &a,
const digest<DB2> &b) {
162 unsigned const DB = DB1 < DB2 ? DB2 : DB1;
163 return resize<DB>(a).base_array() == resize<DB>(b).base_array();
166 template<
unsigned DB1,
unsigned DB2>
167 bool operator!=(
const digest<DB1> &a,
const digest<DB2> &b) {
171 template<
unsigned DB1,
unsigned DB2>
172 bool operator<(
const digest<DB1> &a,
const digest<DB2> &b) {
173 unsigned const DB = DB1 < DB2 ? DB2 : DB1;
174 return resize<DB>(a).base_array() < resize<DB>(b).base_array();
177 template<
unsigned DB1,
unsigned DB2>
178 bool operator>(
const digest<DB1> &a,
const digest<DB2> &b) {
182 template<
unsigned DB1,
unsigned DB2>
183 bool operator<=(
const digest<DB1> &a,
const digest<DB2> &b) {
187 template<
unsigned DB1,
unsigned DB2>
188 bool operator>=(
const digest<DB1> &a,
const digest<DB2> &b) {
192 template<
unsigned DB>
193 bool operator!=(digest<DB>
const &a,
char const *b) {
194 BOOST_ASSERT(std::strlen(b) == DB / 4);
195 return static_cast<bool>(std::strcmp(a.cstring().data(), b));
198 template<
unsigned DB>
199 bool operator==(digest<DB>
const &a,
char const *b) {
203 template<
unsigned DB>
204 bool operator!=(
char const *b, digest<DB>
const &a) {
208 template<
unsigned DB>
209 bool operator==(
char const *b, digest<DB>
const &a) {
213 template<
unsigned DB>
214 std::ostream &
operator<<(std::ostream &sink, digest<DB>
const &d) {
215 d.to_ascii(std::ostream_iterator<char>(sink));
219 template<
unsigned DB>
220 std::istream &
operator>>(std::istream &source, digest<DB> &d) {
221 std::array<char, DB / 4> a = {{}};
222 for (
unsigned i = 0; i < a.size(); ++i) {
224 if (!source.get(c)) {
225 source.setstate(std::ios::failbit);
228 if (!std::isxdigit(c, source.getloc())) {
230 source.setstate(std::ios::failbit);
234 if (std::isdigit(c, source.getloc())) {
237 a[i] = std::toupper(c, source.getloc()) -
'A' + 0xA;
240 detail::pack<stream_endian::big_bit, stream_endian::big_bit, 4, 8>(a.begin(), a.end(), d.begin());
247 template<std::
size_t DigestBits>
250 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
digest< NewBits > reserve(const digest< OldBits > &od)
Definition: block/include/nil/crypto3/detail/digest.hpp:115
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
digest()
Definition: modes/include/nil/crypto3/detail/digest.hpp:74
digest(std::size_t sz, octet_type ot)
Definition: modes/include/nil/crypto3/detail/digest.hpp:76