A toolkit for working with phylogenetic data.
v0.20.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sha256.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_UTILS_TOOLS_SHA256_H_
2 #define GENESIS_UTILS_TOOLS_SHA256_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 // SHA256
44 // ================================================================================================
45 
96 class SHA256
97 {
98 public:
99 
100  // -------------------------------------------------------------------------
101  // Typedefs and Constants
102  // -------------------------------------------------------------------------
103 
104  static const size_t BlockSize = ( 512 / 8 );
105  static const size_t DigestSize = ( 256 / 8);
106 
114  using DigestType = std::array< uint32_t, 8 >;
115 
116  // -------------------------------------------------------------------------
117  // Constructors and Rule of Five
118  // -------------------------------------------------------------------------
119 
123  SHA256();
124  ~SHA256() = default;
125 
126  SHA256( SHA256 const& ) = default;
127  SHA256( SHA256&& ) = default;
128 
129  SHA256& operator= ( SHA256 const& ) = default;
130  SHA256& operator= ( SHA256&& ) = default;
131 
132  // -------------------------------------------------------------------------
133  // Member Functions
134  // -------------------------------------------------------------------------
135 
139  void clear();
140 
144  void update( std::string const& s );
145  void update( std::istream& is );
146  void update( char const* input, size_t length );
147 
151  std::string final_hex();
152 
157 
161  static std::string from_file_hex( std::string const& filename );
162 
166  static DigestType from_file_digest( std::string const& filename );
167 
171  static std::string from_string_hex( std::string const& input );
172 
176  static DigestType from_string_digest( std::string const& input );
177 
181  static std::string from_stream_hex( std::istream& is );
182 
186  static DigestType from_stream_digest( std::istream& is );
187 
188 
189  static std::string digest_to_hex( DigestType const& digest );
190  static DigestType hex_to_digest( std::string const& hex );
191 
192  // -------------------------------------------------------------------------
193  // Internal Functions
194  // -------------------------------------------------------------------------
195 
196 private:
197 
198  void reset_();
199 
200  // SHA256 block update operation. Continues an SHA256 message-digest
201  // operation, processing another message block
202  void update_( unsigned char const* message, size_t len );
203 
204  void transform_( unsigned char const* message, unsigned int block_nb );
205 
206  static inline uint32_t SHA2_SHFR( uint32_t x, uint32_t n )
207  {
208  return x >> n;
209  }
210 
211  static inline uint32_t SHA2_ROTR( uint32_t x, uint32_t n )
212  {
213  return ((x >> n) | (x << ((sizeof(x) << 3) - n)));
214  }
215 
216  static inline uint32_t SHA2_ROTL( uint32_t x, uint32_t n )
217  {
218  return ((x << n) | (x >> ((sizeof(x) << 3) - n)));
219  }
220 
221  static inline uint32_t SHA2_CH( uint32_t x, uint32_t y, uint32_t z )
222  {
223  return ((x & y) ^ (~x & z));
224  }
225 
226  static inline uint32_t SHA2_MAJ( uint32_t x, uint32_t y, uint32_t z )
227  {
228  return ((x & y) ^ (x & z) ^ (y & z));
229  }
230 
231  static inline uint32_t SHA256_F1( uint32_t x )
232  {
233  return (SHA2_ROTR(x, 2) ^ SHA2_ROTR(x, 13) ^ SHA2_ROTR(x, 22));
234  }
235 
236  static inline uint32_t SHA256_F2( uint32_t x )
237  {
238  return (SHA2_ROTR(x, 6) ^ SHA2_ROTR(x, 11) ^ SHA2_ROTR(x, 25));
239  }
240 
241  static inline uint32_t SHA256_F3( uint32_t x )
242  {
243  return (SHA2_ROTR(x, 7) ^ SHA2_ROTR(x, 18) ^ SHA2_SHFR(x, 3));
244  }
245 
246  static inline uint32_t SHA256_F4( uint32_t x )
247  {
248  return (SHA2_ROTR(x, 17) ^ SHA2_ROTR(x, 19) ^ SHA2_SHFR(x, 10));
249  }
250 
251  static inline uint32_t SHA2_PACK32( unsigned char const* str )
252  {
253  return ((uint32_t) *((str) + 3) )
254  | ((uint32_t) *((str) + 2) << 8)
255  | ((uint32_t) *((str) + 1) << 16)
256  | ((uint32_t) *((str) + 0) << 24)
257  ;
258  }
259 
260  static inline void SHA2_UNPACK32( uint32_t x, unsigned char* str)
261  {
262  *((str) + 3) = (uint8_t) ((x) );
263  *((str) + 2) = (uint8_t) ((x) >> 8);
264  *((str) + 1) = (uint8_t) ((x) >> 16);
265  *((str) + 0) = (uint8_t) ((x) >> 24);
266  }
267 
268  // -------------------------------------------------------------------------
269  // Data Members
270  // -------------------------------------------------------------------------
271 
272 private:
273 
274  const static uint32_t sha256_k[];
275 
276  unsigned int tot_len_;
277  unsigned int len_;
278  unsigned char block_[ 2 * BlockSize ];
279 
280  DigestType digest_;
281 
282 };
283 
284 } // namespace utils
285 } // namespace genesis
286 
287 // ================================================================================================
288 // Standard Hash Function
289 // ================================================================================================
290 
291 namespace std
292 {
299  template<>
300  struct hash<genesis::utils::SHA256::DigestType>
301  {
303  using result_type = std::size_t;
304 
306  result_type hash = 0;
307  hash ^= s[0] ^ ( static_cast<result_type>( s[1] ) << 32 );
308  hash ^= s[2] ^ ( static_cast<result_type>( s[3] ) << 32 );
309  hash ^= s[4] ^ ( static_cast<result_type>( s[5] ) << 32 );
310  hash ^= s[6] ^ ( static_cast<result_type>( s[7] ) << 32 );
311  return hash;
312  }
313  };
314 }
315 
316 #endif // include guard
std::array< uint32_t, 8 > DigestType
Store a SHA256 digest.
Definition: sha256.hpp:114
std::string final_hex()
Finish the calculation, prepare the object for next use, and return the hash.
Definition: sha256.cpp:153
Calculate SHA256 hashes for strings and files.
Definition: sha256.hpp:96
void clear()
Reset to initial state, that is, delete any intermediate input from update() calls.
Definition: sha256.cpp:119
static DigestType hex_to_digest(std::string const &hex)
Definition: sha256.cpp:247
SHA256 & operator=(SHA256 const &)=default
DigestType final_digest()
Finish the calculation, prepare the object for next use, and return the digest.
Definition: sha256.cpp:159
static DigestType from_file_digest(std::string const &filename)
Calculate the hash digest for the content of a file, given its path.
Definition: sha256.cpp:192
static std::string from_stream_hex(std::istream &is)
Calculate the checksum for the content of a stream.
Definition: sha256.cpp:214
genesis::utils::SHA256::DigestType argument_type
Definition: sha256.hpp:302
static DigestType from_stream_digest(std::istream &is)
Calculate the hash digest for the content of a stream.
Definition: sha256.cpp:221
SHA256()
Initialize the object for use.
Definition: sha256.cpp:110
void update(std::string const &s)
Add the contents of a string to the hash digest.
Definition: sha256.cpp:124
static std::string digest_to_hex(DigestType const &digest)
Definition: sha256.cpp:228
static const size_t DigestSize
Definition: sha256.hpp:105
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
static DigestType from_string_digest(std::string const &input)
Calculate the hash digest for the content of a string.
Definition: sha256.cpp:207
static const size_t BlockSize
Definition: sha256.hpp:104
result_type operator()(argument_type const &s) const
Definition: sha256.hpp:305
static std::string from_string_hex(std::string const &input)
Calculate the checksum for the content of a string.
Definition: sha256.cpp:200
double length(Tree const &tree)
Get the length of the tree, i.e., the sum of all branch lengths.