Back to Index

## The MaybeBoost Hash Library: Functions

### The `compute_digest` Family

Defined in `<boost/hash/compute_digest.hpp>`

These are convenience functions for calculating the message digest of a single range of data. All versions take a *HashAlgorithm* model as an explicit template parameter and return the corresponding *HashAlgorithm*::digest_type. All the bits in the input range values are used (including the sign bit, if present).

There are multiple ways to provide the range:

- As a pair of input iterators
`compute_digest<`*HashAlgorithm*>(*InIter* b, *InIter* e)

- As an input iterator and a length
`compute_digest_n<`*HashAlgorithm*>(*InIter* b, size_t n)

- As a single-pass range of readable iterators
`compute_digest<`*HashAlgorithm*>(*Range* const &r)

equivalent to `compute_digest<`*HashAlgorithm*>(boost::begin(r), boost::end(r))

(When the `Range` is a `std::basic_string`, `compute_digest` and `compute_digest_n` delegate to `compute_digest_data` to allow pointer-dependant optimizations.)
- As an object providing
`begin()` and `size()` member functions
`compute_digest_n<`*HashAlgorithm*>(*T* const &x)

equivalent to `compute_digest_n<`*HashAlgorithm*>(x.begin(), x.size())

- As an object providing
`data()` and `size()` member functions
`compute_digest_data<`*HashAlgorithm*>(*T* const &x)

equivalent to `compute_digest_n<`*HashAlgorithm*>(x.data(), x.size())

**Portability Note:** Hashing values with `compute_digest` is portable if you ensure that the size in bits of the range iterator's value_type is the same on all machines. It's expected that most users will be hashing bytes on POSIX-compatible machines (where bytes are always 8 bits), and thus will have no portability issues.

**Limitation:** It is not possible to explicitly specify the number of bits to use from each value in the range when using `compute_digest`. The alternative is to use the `StreamHash` directly. This can still be done as an expression using code along the lines of the following:

`typedef `*HashAlgorithm*::stream_hash<*N*>::type shn;

shn::digest_type d = shn().update(*b*, *e*).end_message();

Copyright Scott McMurray 2010

Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt).