Boost logo

Boost :

From: Vinnie Falco (vinnie.falco_at_[hidden])
Date: 2024-12-09 18:09:58


On Mon, Dec 9, 2024 at 9:41 AM Vinnie Falco <vinnie.falco_at_[hidden]> wrote:

> I think it would be preferable if Hash2 made this the default
> implementation, and allowed more sophisticated algorithms (and authors) to
> opt-in to a better implementation. Otherwise, we are forcing ordinary users
> who just want to adapt an external library to meet the HashAlgorithm
> requirements to become experts.
>

Peter, would you please comment on the following sketch:

    /** HashAlgorithm exemplar
    */
    struct HashAlgorithm
    {
        using result_type = /* integral or array-like */;

        HashAlgorithm();
        HashAlgorithm( HashAlgorithm const& r );
        HashAlgorithm& operator=( HashAlgorithm const& r );

        /** Append data to the message

            The behavior of subsequent calls to update() after
            finalize() has been called at least once is undefined
            unless specified by the HashAlgorithm.
        */
        void update( void const* data, std::size_t n );

        /** Return the digest

            This function shall return the final hash value of the input
            message, where the input message is defined by the ordered
            sequence of bytes provided in all prior calls to update().
            The behavior of subsequent calls to finalize() is undefined
            unless specified by the HashAlgorithm.
        */
        result_type finalize();

        static constexpr int block_size = /*...*/; // optional
        explicit HashAlgorithm( std::uint64_t seed ); // optional
        HashAlgorithm( unsigned char const* seed, std::size_t n ); //
optional
    };

    /** Return a seeded HashAlgorithm
    */
    template< class HashAlgorithm >
    HashAlgorithm make_seeded( std::uint64_t seed )
    {
        if constexpr(std::is_constructible<
                HashAlgorithm, std::uint64_t>)
            return HashAlgorithm(seed);
        else
        {
            HashAlgorithm h;
            hash_append(h, seed);
            return h;
        }
    }

    /** Return a seeded HashAlgorithm
    */
    template< class HashAlgorithm >
    HashAlgorithm make_seeded(
        unsigned char const* seed, std::size_t n )
    {
        if constexpr(std::is_constructible<HashAlgorithm,
                unsigned char const*, std::size_t>)
            return HashAlgorithm(seed);
        else
        {
            HashAlgorithm h;
            hash_append(h, seed);
            return h;
        }
    }

Thanks


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk