Back to Index

The MaybeBoost Hash Library: Quickstart

Want to get started without worry about the details? Here's how.

  1. Include the boost/hash.hpp header. No linking is required.
  2. Choose the hash algorithm you with to use. The following are provided:
    Name Digest Bits HashAlgorithm Model (in boost::hash) Reference Design Type Notes
    Adler-h (h in {8, 16, 24, ..., 64}) h adler<h> RFC 1950 Checksum Only supports octets as input values
    CRC-32/PNG 32 crc32_png PNG Specification, Section 3.4 Checksum Also used by cksfv; Only supports bytes as input values
    CubeHash-h (h in {8, 16, 24, ..., 512}) h cubehash<h> CubeHash NIST Submission Cryptographic SHA-3 candidate; Not yet mature.
    MD4 128 md4 RFC 1320 Cryptographic Practical attacks exist
    MD5 128 md5 RFC 1321
    SHA 160 sha FIPS 180 Cryptographic Collisions known; Repealed
    SHA-1 160 sha1 FIPS 180-3 Some concern
    SHA-224 224 sha2<224> As of 2010, considered secure by NIST
    SHA-256 256 sha2<256>
    SHA-384 384 sha2<384>
    SHA-512 512 sha2<512>
    If you need security against a malicious adversary, sha2<h> is, as of 2010, the recommended choice. If not, then two popular choices are crc<h> for checksums and md5 for fingerprinting.
  3. With the chosen HashAlgorithm, call the boost::hash::compute_digest<HashAlgorithm> function, passing it an input range (such as a container). That will return the digest as an object of type HashAlgorithm::digest_type.
  4. The resulting digest may then be output to a std::ostream or compared against other message digests (such as one read from a std::istream).

Here's a sample complete program, example/quickstart.cpp:

#include <boost/hash.hpp>

#include <iostream>
#include <string>

int main() {
    std::string s = "Hello World!";

int main() {
    using namespace boost::hash;
    std::string s = "Hello World!";
    typedef boost::hash::sha2<256> HashAlgorithm;
    HashAlgorithm::digest_type digest = compute_digest<HashAlgorithm>(s);
    std::cout << digest << "\n";

The distribution also includes a "hash this file" example, example/hashsum.cpp, along the lines of the coreutils md5sum and sha1sum programs.

Previous: Introduction Next: Functions

Copyright Scott McMurray 2010

Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at