diff --git a/doc/html/hash2.html b/doc/html/hash2.html index b36b314..df49964 100644 --- a/doc/html/hash2.html +++ b/doc/html/hash2.html @@ -712,6 +712,7 @@ pre.rouge {
  • hash2sum
  • Compile Time Hashing
  • Use with Unordered Containers
  • +
  • Result Extension
  • Implementation Features @@ -2556,6 +2557,64 @@ In real code, you might want to omit the default constructor, to avoid the possi

    To keep the case where we only pass one hash algorithm working, we default the second template parameter to the first one, so that if only one hash algorithm is passed, it will always be used.

    +
    +

    Result Extension

    +
    +

    Some of our hash algorithms, such as xxhash_64 and siphash_64, have more than 64 bits of internal state, but only produce a 64 bit result.

    +
    +
    +

    If we’re using one of these algorithms to produce file or content checksums, do not tolerate collisions, and operate on a large number of files or items (many millions), it might be better to use a 128 bit digest instead.

    +
    +
    +

    Since the algorithms maintain more than 64 bits of state, we can call result() twice and obtain a meaningful 128 bit result.

    +
    +
    +

    The following example demonstrates how. It defines an algorithm xxhash_128 which is implemented by wrapping xxhash_64 and redefining its result_type and result members appropriately:

    +
    +
    +
    +
    #include <boost/hash2/xxhash.hpp>
    +#include <boost/hash2/digest.hpp>
    +#include <boost/endian/conversion.hpp>
    +
    +class xxhash_128: private boost::hash2::xxhash_64
    +{
    +public:
    +
    +    using result_type = boost::hash2::digest<16>;
    +
    +    using xxhash_64::xxhash_64;
    +    using xxhash_64::update;
    +
    +    result_type result()
    +    {
    +        std::uint64_t r1 = xxhash_64::result();
    +        std::uint64_t r2 = xxhash_64::result();
    +
    +        result_type r = {};
    +
    +        boost::endian::store_little_u64( r.data() + 0, r1 );
    +        boost::endian::store_little_u64( r.data() + 8, r2 );
    +
    +        return r;
    +    }
    +};
    +
    +#include <string>
    +#include <iostream>
    +
    +int main()
    +{
    +    std::string tv( "The quick brown fox jumps over the lazy dog" );
    +
    +    xxhash_128 hash( 43 );
    +    hash.update( tv.data(), tv.size() );
    +
    +    std::cout << hash.result() << std::endl;
    +}
    +
    +
    +