A toolkit for working with phylogenetic data.
v0.19.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 
85  SHA1();
86  ~SHA1() = default;
87 
88  SHA1( SHA1 const& ) = default;
89  SHA1( SHA1&& ) = default;
90 
91  SHA1& operator= ( SHA1 const& ) = default;
92  SHA1& operator= ( SHA1&& ) = default;
93 
94  // -------------------------------------------------------------------------
95  // Member Functions
96  // -------------------------------------------------------------------------
97 
98  void update( std::string const& s );
99  void update( std::istream& is );
100 
101  std::string final_hex();
103 
104  static std::string from_file_hex( std::string const& filename );
105  static DigestType from_file_digest( std::string const& filename );
106 
107  static std::string from_string_hex( std::string const& input );
108  static DigestType from_string_digest( std::string const& input );
109 
110  static std::string digest_to_hex( DigestType const& digest );
111  static DigestType hex_to_digest( std::string const& hex );
112 
113  // -------------------------------------------------------------------------
114  // Internal Functions
115  // -------------------------------------------------------------------------
116 
117 private:
118 
119  void reset_();
120 
121  uint32_t rol_(const uint32_t value, const size_t bits);
122  uint32_t blk_(const uint32_t block[SHA1::BlockInts], const size_t i);
123 
124  void R0_(
125  const uint32_t block[SHA1::BlockInts], const uint32_t v, uint32_t& w, const uint32_t x,
126  const uint32_t y, uint32_t& z, const size_t i
127  );
128  void R1_(
129  uint32_t block[SHA1::BlockInts], const uint32_t v, uint32_t& w, const uint32_t x,
130  const uint32_t y, uint32_t& z, const size_t i
131  );
132  void R2_(
133  uint32_t block[SHA1::BlockInts], const uint32_t v, uint32_t& w, const uint32_t x,
134  const uint32_t y, uint32_t& z, const size_t i
135  );
136  void R3_(
137  uint32_t block[SHA1::BlockInts], const uint32_t v, uint32_t& w, const uint32_t x,
138  const uint32_t y, uint32_t& z, const size_t i
139  );
140  void R4_(
141  uint32_t block[SHA1::BlockInts], const uint32_t v, uint32_t& w, const uint32_t x,
142  const uint32_t y, uint32_t& z, const size_t i
143  );
144 
145  void transform_( uint32_t block[SHA1::BlockInts] );
146  void buffer_to_block_(const std::string& buffer, uint32_t block[SHA1::BlockInts]);
147 
148  // -------------------------------------------------------------------------
149  // Data Members
150  // -------------------------------------------------------------------------
151 
152 private:
153 
154  DigestType digest_;
155  std::string buffer_;
156  size_t transforms_;
157 
158 };
159 
160 } // namespace utils
161 } // namespace genesis
162 
163 // ================================================================================================
164 // Standard Hash Function
165 // ================================================================================================
166 
167 namespace std
168 {
175  template<>
176  struct hash<genesis::utils::SHA1::DigestType>
177  {
179  using result_type = std::size_t;
180 
182  result_type hash = 0;
183  hash ^= s[0] ^ ( static_cast<result_type>( s[1] ) << 32 );
184  hash ^= s[2] ^ ( static_cast<result_type>( s[3] ) << 32 );
185  hash ^= s[4];
186  return hash;
187  }
188  };
189 }
190 
191 #endif // include guard
DigestType final_digest()
Finish the calculation, prepare the object for next use, and return the digest.
Definition: sha1.cpp:101
genesis::utils::SHA1::DigestType argument_type
Definition: sha1.hpp:178
std::string final_hex()
Finish the calculation, prepare the object for next use, and return the hash.
Definition: sha1.cpp:92
static DigestType from_string_digest(std::string const &input)
Calculate the hash digest for the content of a string.
Definition: sha1.cpp:171
SHA1 & operator=(SHA1 const &)=default
result_type operator()(argument_type const &s) const
Definition: sha1.hpp:181
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:161
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:150
static const size_t BlockBytes
Definition: sha1.hpp:70
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:139
static std::string digest_to_hex(DigestType const &digest)
Definition: sha1.cpp:178
void update(std::string const &s)
Add the contents of a string to the hash digest.
Definition: sha1.cpp:64
Calculate SHA1 hashes for strings and files.
Definition: sha1.hpp:60
SHA1()
Initialize the object for use.
Definition: sha1.cpp:52
static DigestType hex_to_digest(std::string const &hex)
Definition: sha1.cpp:190