A library for working with phylogenetic and population genetic data.
v0.32.0
MD5 Class Reference

#include <genesis/utils/tools/hash/md5.hpp>

Detailed Description

Calculate MD5 hashes for strings and files.

After creating an object of this type, call update() with your input data as argument, as often as needed. Then, call final_hex() or final_digest() to obtain the hash as either a hex string or a DigestType object and reset the object for reuse.

If you simply need the hash for a string or content of a file, use read_hex() or read_digest(), which are static shortcuts for the above using an input source. Use functions such as utils::from_file() and utils::from_string() to conveniently get an input source that can be used here.

The implementation is based on http://www.zedwood.com/article/cpp-md5-function, which itself was converted to C++ class by Frank Thilo (thilo.nosp@m.@uni.nosp@m.x-ag..nosp@m.org) for bzflag (http://www.bzflag.org), and is based on the reference implementation of RFC 1321:

License

Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All rights reserved.

License to copy and use this software is granted provided that it is identified as the "RSA Data Security, Inc. MD5 Message-Digest Algorithm" in all material mentioning or referencing this software or this function.

License is also granted to make and use derivative works provided that such works are identified as "derived from the RSA Data Security, Inc. MD5 Message-Digest Algorithm" in all material mentioning or referencing the derived work.

RSA Data Security, Inc. makes no representations concerning either the merchantability of this software or the suitability of this software for any particular purpose. It is provided "as is" without express or implied warranty of any kind.

These notices must be retained in any copies of any part of this documentation and/or software.

See also Acknowledgements.

Definition at line 88 of file md5.hpp.

Public Member Functions

 MD5 ()
 Initialize the object for use. More...
 
 MD5 (MD5 &&)=default
 
 MD5 (MD5 const &)=default
 
 ~MD5 ()=default
 
void clear ()
 Reset to initial state, that is, delete any intermediate input from update() calls. More...
 
DigestType final_digest ()
 Finish the calculation, prepare the object for next use, and return the digest. More...
 
std::string final_hex ()
 Finish the calculation, prepare the object for next use, and return the hash. More...
 
MD5operator= (MD5 &&)=default
 
MD5operator= (MD5 const &)=default
 
void update (char const *input, size_type length)
 
void update (std::istream &is)
 
void update (std::shared_ptr< BaseInputSource > source)
 
void update (std::string const &s)
 

Static Public Member Functions

static std::string digest_to_hex (DigestType const &digest)
 
static DigestType hex_to_digest (std::string const &hex)
 
static DigestType read_digest (std::shared_ptr< BaseInputSource > source)
 Calculate the hash digest for the content of an input source. More...
 
static std::string read_hex (std::shared_ptr< BaseInputSource > source)
 Calculate the checksum for the content of an input source. More...
 

Public Types

using DigestType = std::array< uint8_t, 16 >
 Store an MD5 digest. More...
 
using size_type = uint32_t
 

Static Public Attributes

static const size_t BlockSize = 64
 

Constructor & Destructor Documentation

◆ MD5() [1/3]

MD5 ( )

Initialize the object for use.

Definition at line 87 of file md5.cpp.

◆ ~MD5()

~MD5 ( )
default

◆ MD5() [2/3]

MD5 ( MD5 const &  )
default

◆ MD5() [3/3]

MD5 ( MD5 &&  )
default

Member Function Documentation

◆ clear()

void clear ( )

Reset to initial state, that is, delete any intermediate input from update() calls.

Definition at line 155 of file md5.cpp.

◆ digest_to_hex()

std::string digest_to_hex ( MD5::DigestType const &  digest)
static

Definition at line 110 of file md5.cpp.

◆ final_digest()

MD5::DigestType final_digest ( )

Finish the calculation, prepare the object for next use, and return the digest.

Definition at line 213 of file md5.cpp.

◆ final_hex()

std::string final_hex ( )

Finish the calculation, prepare the object for next use, and return the hash.

Definition at line 207 of file md5.cpp.

◆ hex_to_digest()

MD5::DigestType hex_to_digest ( std::string const &  hex)
static

Definition at line 122 of file md5.cpp.

◆ operator=() [1/2]

MD5& operator= ( MD5 &&  )
default

◆ operator=() [2/2]

MD5& operator= ( MD5 const &  )
default

◆ read_digest()

MD5::DigestType read_digest ( std::shared_ptr< BaseInputSource source)
static

Calculate the hash digest for the content of an input source.

Definition at line 103 of file md5.cpp.

◆ read_hex()

std::string read_hex ( std::shared_ptr< BaseInputSource source)
static

Calculate the checksum for the content of an input source.

Definition at line 96 of file md5.cpp.

◆ update() [1/4]

void update ( char const *  input,
MD5::size_type  length 
)

Definition at line 200 of file md5.cpp.

◆ update() [2/4]

void update ( std::istream &  is)

Definition at line 183 of file md5.cpp.

◆ update() [3/4]

void update ( std::shared_ptr< BaseInputSource source)

Definition at line 160 of file md5.cpp.

◆ update() [4/4]

void update ( std::string const &  s)

Definition at line 178 of file md5.cpp.

Member Typedef Documentation

◆ DigestType

using DigestType = std::array< uint8_t, 16 >

Store an MD5 digest.

This type can be used for storing hashes instead of the hex representation. It only needs 16 bytes instead of 32 bytes for the hex. As it is a std::array, this type can be compared using normal ==.

Definition at line 107 of file md5.hpp.

◆ size_type

using size_type = uint32_t

Definition at line 96 of file md5.hpp.

Member Data Documentation

◆ BlockSize

const size_t BlockSize = 64
static

Definition at line 98 of file md5.hpp.


The documentation for this class was generated from the following files: