A toolkit for working with phylogenetic data.
v0.24.0
sha1.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_UTILS_TOOLS_HASH_SHA1_H_
2 #define GENESIS_UTILS_TOOLS_HASH_SHA1_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2019 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 
35 
36 #include <array>
37 #include <cstdint>
38 #include <iosfwd>
39 #include <string>
40 
41 namespace genesis {
42 namespace utils {
43 
44 // ================================================================================================
45 // SHA1
46 // ================================================================================================
47 
64 class SHA1
65 {
66 public:
67 
68  // -------------------------------------------------------------------------
69  // Typedefs and Constants
70  // -------------------------------------------------------------------------
71 
72  // Number of 32bit integers per SHA1 block.
73  static const size_t BlockInts = 16;
74  static const size_t BlockBytes = BlockInts * 4;
75 
83  using DigestType = std::array< uint32_t, 5 >;
84 
85  // -------------------------------------------------------------------------
86  // Constructors and Rule of Five
87  // -------------------------------------------------------------------------
88 
92  SHA1();
93  ~SHA1() = default;
94 
95  SHA1( SHA1 const& ) = default;
96  SHA1( SHA1&& ) = default;
97 
98  SHA1& operator= ( SHA1 const& ) = default;
99  SHA1& operator= ( SHA1&& ) = default;
100 
101  // -------------------------------------------------------------------------
102  // Full Hashing
103  // -------------------------------------------------------------------------
104 
108  static std::string read_hex( std::shared_ptr<BaseInputSource> source );
109 
113  static DigestType read_digest( std::shared_ptr<BaseInputSource> source );
114 
115  static std::string digest_to_hex( DigestType const& digest );
116  static DigestType hex_to_digest( std::string const& hex );
117 
118  // -------------------------------------------------------------------------
119  // Iterative Hashing
120  // -------------------------------------------------------------------------
121 
125  void clear();
126 
127  void update( std::shared_ptr<BaseInputSource> source );
128 
132  void update( std::string const& s );
133 
137  void update( std::istream& is );
138 
142  std::string final_hex();
143 
148 
149  // -------------------------------------------------------------------------
150  // Internal Functions
151  // -------------------------------------------------------------------------
152 
153 private:
154 
155  void reset_();
156 
157  uint32_t rol_(const uint32_t value, const size_t bits);
158  uint32_t blk_(const uint32_t block[SHA1::BlockInts], const size_t i);
159 
160  void R0_(
161  const uint32_t block[SHA1::BlockInts], const uint32_t v, uint32_t& w, const uint32_t x,
162  const uint32_t y, uint32_t& z, const size_t i
163  );
164  void R1_(
165  uint32_t block[SHA1::BlockInts], const uint32_t v, uint32_t& w, const uint32_t x,
166  const uint32_t y, uint32_t& z, const size_t i
167  );
168  void R2_(
169  uint32_t block[SHA1::BlockInts], const uint32_t v, uint32_t& w, const uint32_t x,
170  const uint32_t y, uint32_t& z, const size_t i
171  );
172  void R3_(
173  uint32_t block[SHA1::BlockInts], const uint32_t v, uint32_t& w, const uint32_t x,
174  const uint32_t y, uint32_t& z, const size_t i
175  );
176  void R4_(
177  uint32_t block[SHA1::BlockInts], const uint32_t v, uint32_t& w, const uint32_t x,
178  const uint32_t y, uint32_t& z, const size_t i
179  );
180 
184  void transform_( uint32_t block[SHA1::BlockInts] );
185 
189  void buffer_to_block_(const std::string& buffer, uint32_t block[SHA1::BlockInts]);
190 
191  // -------------------------------------------------------------------------
192  // Data Members
193  // -------------------------------------------------------------------------
194 
195 private:
196 
197  DigestType digest_;
198  std::string buffer_;
199  size_t transforms_;
200 
201 };
202 
203 } // namespace utils
204 } // namespace genesis
205 
206 // ================================================================================================
207 // Standard Hash Function
208 // ================================================================================================
209 
210 namespace std
211 {
218  template<>
220  {
222  using result_type = std::size_t;
223 
225  result_type result = 0;
226  result ^= s[0] ^ ( static_cast<result_type>( s[1] ) << 32 );
227  result ^= s[2] ^ ( static_cast<result_type>( s[3] ) << 32 );
228  result ^= s[4];
229  return result;
230  }
231  };
232 }
233 
234 #endif // include guard
DigestType final_digest()
Finish the calculation, prepare the object for next use, and return the digest.
Definition: sha1.cpp:187
static DigestType read_digest(std::shared_ptr< BaseInputSource > source)
Calculate the hash digest for the content of an input source.
Definition: sha1.cpp:72
genesis::utils::SHA1::DigestType argument_type
Definition: sha1.hpp:221
std::string final_hex()
Finish the calculation, prepare the object for next use, and return the hash.
Definition: sha1.cpp:181
STL namespace.
result_type operator()(argument_type const &s) const
Definition: sha1.hpp:224
Container namespace for all symbols of genesis in order to keep them separate when used as a library...
SHA1 & operator=(SHA1 const &)=default
std::array< uint32_t, 5 > DigestType
Store a SHA1 digest.
Definition: sha1.hpp:83
static const size_t BlockInts
Definition: sha1.hpp:73
static const size_t BlockBytes
Definition: sha1.hpp:74
void clear()
Reset to initial state, that is, delete any intermediate input from update() calls.
Definition: sha1.cpp:129
static std::string read_hex(std::shared_ptr< BaseInputSource > source)
Calculate the checksum for the content of an input source.
Definition: sha1.cpp:65
static std::string digest_to_hex(DigestType const &digest)
Definition: sha1.cpp:79
Calculate SHA1 hashes for strings and files.
Definition: sha1.hpp:64
void update(std::shared_ptr< BaseInputSource > source)
Definition: sha1.cpp:134
SHA1()
Initialize the object for use.
Definition: sha1.cpp:56
static DigestType hex_to_digest(std::string const &hex)
Definition: sha1.cpp:91