A toolkit for working with phylogenetic data.
v0.20.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
hashing.cpp
Go to the documentation of this file.
1 /*
2  Genesis - A toolkit for working with phylogenetic data.
3  Copyright (C) 2014-2018 Lucas Czech and HITS gGmbH
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  Contact:
19  Lucas Czech <lucas.czech@h-its.org>
20  Exelixis Lab, Heidelberg Institute for Theoretical Studies
21  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
22 */
23 
32 
36 
37 #include <iostream>
38 #include <stdexcept>
39 
40 namespace genesis {
41 namespace utils {
42 
43 // ================================================================================================
44 // Hashing
45 // ================================================================================================
46 
47 std::string hash_from_file_hex( std::string const& filename, HashingFunctions hash_fct )
48 {
49  switch( hash_fct ) {
51  return MD5::from_file_hex( filename );
52  }
54  return SHA1::from_file_hex( filename );
55  }
57  return SHA256::from_file_hex( filename );
58  }
59  }
60 
61  throw std::invalid_argument( "Invalid hashing function selected." );
62 }
63 
64 std::string hash_from_string_hex( std::string const& input, HashingFunctions hash_fct )
65 {
66  switch( hash_fct ) {
68  return MD5::from_string_hex( input );
69  }
71  return SHA1::from_string_hex( input );
72  }
74  return SHA256::from_string_hex( input );
75  }
76  }
77 
78  throw std::invalid_argument( "Invalid hashing function selected." );
79 }
80 
81 std::string hash_from_stream_hex( std::istream& is, HashingFunctions hash_fct )
82 {
83  switch( hash_fct ) {
85  return MD5::from_stream_hex( is );
86  }
88  return SHA1::from_stream_hex( is );
89  }
91  return SHA256::from_stream_hex( is );
92  }
93  }
94 
95  throw std::invalid_argument( "Invalid hashing function selected." );
96 }
97 
98 } // namespace utils
99 } // namespace genesis
static std::string from_file_hex(std::string const &filename)
Calculate the checksum for the content of a file, given its path.
Definition: md5.cpp:164
std::string hash_from_stream_hex(std::istream &is, HashingFunctions hash_fct)
Calcualte the hash of an input stream, using a given hashing function, and return its hex representat...
Definition: hashing.cpp:81
static std::string from_stream_hex(std::istream &is)
Calculate the checksum for the content of a stream.
Definition: sha1.cpp:156
static std::string from_stream_hex(std::istream &is)
Calculate the checksum for the content of a stream.
Definition: md5.cpp:194
static std::string from_stream_hex(std::istream &is)
Calculate the checksum for the content of a stream.
Definition: sha256.cpp:214
static std::string from_string_hex(std::string const &input)
Calculate the checksum for the content of a string.
Definition: sha1.cpp:142
static std::string from_file_hex(std::string const &filename)
Calculate the checksum for the content of a file, given its path.
Definition: sha1.cpp:126
static std::string from_file_hex(std::string const &filename)
Calculate the checksum for the content of a file, given its path.
Definition: sha256.cpp:184
std::string hash_from_string_hex(std::string const &input, HashingFunctions hash_fct)
Calcualte the hash of a string, using a given hashing function, and return its hex representation as ...
Definition: hashing.cpp:64
static std::string from_string_hex(std::string const &input)
Calculate the checksum for the content of a string.
Definition: sha256.cpp:200
std::string hash_from_file_hex(std::string const &filename, HashingFunctions hash_fct)
Calcualte the hash of a file, using a given hashing function, and return its hex representation as a ...
Definition: hashing.cpp:47
HashingFunctions
List of the currently implemented hashing functions.
Definition: hashing.hpp:53
static std::string from_string_hex(std::string const &input)
Calculate the checksum for the content of a string.
Definition: md5.cpp:180