A toolkit for working with phylogenetic data.
v0.20.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sha1.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_UTILS_TOOLS_SHA1_H_
2 #define GENESIS_UTILS_TOOLS_SHA1_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2018 Lucas Czech and HITS gGmbH
7 
8  This program is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 
21  Contact:
22  Lucas Czech <lucas.czech@h-its.org>
23  Exelixis Lab, Heidelberg Institute for Theoretical Studies
24  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
25 */
26 
34 #include <array>
35 #include <cstdint>
36 #include <iosfwd>
37 #include <string>
38 
39 namespace genesis {
40 namespace utils {
41 
42 // ================================================================================================
43 // SHA1
44 // ================================================================================================
45 
60 class SHA1
61 {
62 public:
63 
64  // -------------------------------------------------------------------------
65  // Typedefs and Constants
66  // -------------------------------------------------------------------------
67 
68  // Number of 32bit integers per SHA1 block.
69  static const size_t BlockInts = 16;
70  static const size_t BlockBytes = BlockInts * 4;
71 
79  using DigestType = std::array< uint32_t, 5 >;
80 
81  // -------------------------------------------------------------------------
82  // Constructors and Rule of Five
83  // -------------------------------------------------------------------------
84 
88  SHA1();
89  ~SHA1() = default;
90 
91  SHA1( SHA1 const& ) = default;
92  SHA1( SHA1&& ) = default;
93 
94  SHA1& operator= ( SHA1 const& ) = default;
95  SHA1& operator= ( SHA1&& ) = default;
96 
97  // -------------------------------------------------------------------------
98  // Member Functions
99  // -------------------------------------------------------------------------
100 
104  void clear();
105 
109  void update( std::string const& s );
110 
114  void update( std::istream& is );
115 
119  std::string final_hex();
120 
125 
129  static std::string from_file_hex( std::string const& filename );
130 
134  static DigestType from_file_digest( std::string const& filename );
135 
139  static std::string from_string_hex( std::string const& input );
140 
144  static DigestType from_string_digest( std::string const& input );
145 
149  static std::string from_stream_hex( std::istream& is );
150 
154  static DigestType from_stream_digest( std::istream& is );
155 
156  static std::string digest_to_hex( DigestType const& digest );
157  static DigestType hex_to_digest( std::string const& hex );
158 
159  // -------------------------------------------------------------------------
160  // Internal Functions
161  // -------------------------------------------------------------------------
162 
163 private:
164 
165  void reset_();
166 
167  uint32_t rol_(const uint32_t value, const size_t bits);
168  uint32_t blk_(const uint32_t block[SHA1::BlockInts], const size_t i);
169 
170  void R0_(
171  const uint32_t block[SHA1::BlockInts], const uint32_t v, uint32_t& w, const uint32_t x,
172  const uint32_t y, uint32_t& z, const size_t i
173  );
174  void R1_(
175  uint32_t block[SHA1::BlockInts], const uint32_t v, uint32_t& w, const uint32_t x,
176  const uint32_t y, uint32_t& z, const size_t i
177  );
178  void R2_(
179  uint32_t block[SHA1::BlockInts], const uint32_t v, uint32_t& w, const uint32_t x,
180  const uint32_t y, uint32_t& z, const size_t i
181  );
182  void R3_(
183  uint32_t block[SHA1::BlockInts], const uint32_t v, uint32_t& w, const uint32_t x,
184  const uint32_t y, uint32_t& z, const size_t i
185  );
186  void R4_(
187  uint32_t block[SHA1::BlockInts], const uint32_t v, uint32_t& w, const uint32_t x,
188  const uint32_t y, uint32_t& z, const size_t i
189  );
190 
194  void transform_( uint32_t block[SHA1::BlockInts] );
195 
199  void buffer_to_block_(const std::string& buffer, uint32_t block[SHA1::BlockInts]);
200 
201  // -------------------------------------------------------------------------
202  // Data Members
203  // -------------------------------------------------------------------------
204 
205 private:
206 
207  DigestType digest_;
208  std::string buffer_;
209  size_t transforms_;
210 
211 };
212 
213 } // namespace utils
214 } // namespace genesis
215 
216 // ================================================================================================
217 // Standard Hash Function
218 // ================================================================================================
219 
220 namespace std
221 {
228  template<>
229  struct hash<genesis::utils::SHA1::DigestType>
230  {
232  using result_type = std::size_t;
233 
235  result_type hash = 0;
236  hash ^= s[0] ^ ( static_cast<result_type>( s[1] ) << 32 );
237  hash ^= s[2] ^ ( static_cast<result_type>( s[3] ) << 32 );
238  hash ^= s[4];
239  return hash;
240  }
241  };
242 }
243 
244 #endif // include guard
DigestType final_digest()
Finish the calculation, prepare the object for next use, and return the digest.
Definition: sha1.cpp:91
genesis::utils::SHA1::DigestType argument_type
Definition: sha1.hpp:231
std::string final_hex()
Finish the calculation, prepare the object for next use, and return the hash.
Definition: sha1.cpp:85
static std::string from_stream_hex(std::istream &is)
Calculate the checksum for the content of a stream.
Definition: sha1.cpp:156
static DigestType from_string_digest(std::string const &input)
Calculate the hash digest for the content of a string.
Definition: sha1.cpp:149
SHA1 & operator=(SHA1 const &)=default
static DigestType from_stream_digest(std::istream &is)
Calculate the hash digest for the content of a stream.
Definition: sha1.cpp:163
result_type operator()(argument_type const &s) const
Definition: sha1.hpp:234
std::array< uint32_t, 5 > DigestType
Store a SHA1 digest.
Definition: sha1.hpp:79
static std::string from_string_hex(std::string const &input)
Calculate the checksum for the content of a string.
Definition: sha1.cpp:142
static const size_t BlockInts
Definition: sha1.hpp:69
static DigestType from_file_digest(std::string const &filename)
Calculate the hash digest for the content of a file, given its path.
Definition: sha1.cpp:134
static const size_t BlockBytes
Definition: sha1.hpp:70
void clear()
Reset to initial state, that is, delete any intermediate input from update() calls.
Definition: sha1.cpp:58
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 digest_to_hex(DigestType const &digest)
Definition: sha1.cpp:170
void update(std::string const &s)
Add the contents of a string to the hash digest.
Definition: sha1.cpp:63
Calculate SHA1 hashes for strings and files.
Definition: sha1.hpp:60
SHA1()
Initialize the object for use.
Definition: sha1.cpp:49
static DigestType hex_to_digest(std::string const &hex)
Definition: sha1.cpp:182