A toolkit for working with phylogenetic data.
v0.19.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
MD5 Class Reference

#include <genesis/utils/tools/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 and reset the object for reuse.

If you simply need the hash for a string or content of a file, use from_string() or from_file(), which are static shortcuts for the above.

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:

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 82 of file md5.hpp.

Public Member Functions

 MD5 ()
 Initialize the object for use. More...
 
 MD5 (MD5 const &)=default
 
 MD5 (MD5 &&)=default
 
 ~MD5 ()=default
 
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 const &)=default
 
MD5operator= (MD5 &&)=default
 
void update (std::string const &s)
 Add the contents of a string to the hash digest. More...
 
void update (std::istream &is)
 
void update (char const *input, size_type length)
 

Static Public Member Functions

static std::string digest_to_hex (DigestType const &digest)
 
static DigestType from_file_digest (std::string const &filename)
 Calculate the hash digest for the content of a file, given its path. More...
 
static std::string from_file_hex (std::string const &filename)
 Calculate the checksum for the content of a file, given its path. More...
 
static DigestType from_string_digest (std::string const &input)
 Calculate the hash digest for the content of a string. More...
 
static std::string from_string_hex (std::string const &input)
 Calculate the checksum for the content of a string. More...
 
static DigestType hex_to_digest (std::string const &hex)
 

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 ( )

Initialize the object for use.

Definition at line 87 of file md5.cpp.

~MD5 ( )
default
MD5 ( MD5 const &  )
default
MD5 ( MD5 &&  )
default

Member Function Documentation

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

Definition at line 213 of file md5.cpp.

MD5::DigestType final_digest ( )

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

Definition at line 140 of file md5.cpp.

std::string final_hex ( )

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

Definition at line 131 of file md5.cpp.

MD5::DigestType from_file_digest ( std::string const &  filename)
static

Calculate the hash digest for the content of a file, given its path.

Definition at line 185 of file md5.cpp.

std::string from_file_hex ( std::string const &  filename)
static

Calculate the checksum for the content of a file, given its path.

Definition at line 174 of file md5.cpp.

MD5::DigestType from_string_digest ( std::string const &  input)
static

Calculate the hash digest for the content of a string.

Definition at line 206 of file md5.cpp.

std::string from_string_hex ( std::string const &  input)
static

Calculate the checksum for the content of a string.

Definition at line 196 of file md5.cpp.

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

Definition at line 225 of file md5.cpp.

MD5& operator= ( MD5 const &  )
default
MD5& operator= ( MD5 &&  )
default
void update ( std::string const &  s)

Add the contents of a string to the hash digest.

Definition at line 99 of file md5.cpp.

void update ( std::istream &  is)

Definition at line 104 of file md5.cpp.

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

Definition at line 121 of file md5.cpp.

Member Typedef Documentation

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 101 of file md5.hpp.

using size_type = uint32_t

Definition at line 90 of file md5.hpp.

Member Data Documentation

const size_t BlockSize = 64
static

Definition at line 92 of file md5.hpp.


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