A toolkit for working with phylogenetic data.
v0.18.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_MATH_SHA1_H_
2 #define GENESIS_UTILS_MATH_SHA1_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2017 Lucas Czech
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 
55 class SHA1
56 {
57 public:
58 
59  // -------------------------------------------------------------------------
60  // Typedefs and Constants
61  // -------------------------------------------------------------------------
62 
63  // Number of 32bit integers per SHA1 block.
64  static const size_t BlockInts = 16;
65  static const size_t BlockBytes = BlockInts * 4;
66 
74  using DigestType = std::array< uint32_t, 5 >;
75 
76  // -------------------------------------------------------------------------
77  // Constructors and Rule of Five
78  // -------------------------------------------------------------------------
79 
80  SHA1();
81  ~SHA1() = default;
82 
83  SHA1( SHA1 const& ) = default;
84  SHA1( SHA1&& ) = default;
85 
86  SHA1& operator= ( SHA1 const& ) = default;
87  SHA1& operator= ( SHA1&& ) = default;
88 
89  // -------------------------------------------------------------------------
90  // Member Functions
91  // -------------------------------------------------------------------------
92 
93  void update( std::string const& s );
94  void update( std::istream& is );
95 
96  std::string final_hex();
98 
99  static std::string from_file_hex( std::string const& filename );
100  static DigestType from_file_digest( std::string const& filename );
101 
102  static std::string from_string_hex( std::string const& input );
103  static DigestType from_string_digest( std::string const& input );
104 
105  // -------------------------------------------------------------------------
106  // Internal Functions
107  // -------------------------------------------------------------------------
108 
109 private:
110 
111  void reset_();
112 
113  uint32_t rol_(const uint32_t value, const size_t bits);
114  uint32_t blk_(const uint32_t block[SHA1::BlockInts], const size_t i);
115 
116  void R0_(
117  const uint32_t block[SHA1::BlockInts], const uint32_t v, uint32_t& w, const uint32_t x,
118  const uint32_t y, uint32_t& z, const size_t i
119  );
120  void R1_(
121  uint32_t block[SHA1::BlockInts], const uint32_t v, uint32_t& w, const uint32_t x,
122  const uint32_t y, uint32_t& z, const size_t i
123  );
124  void R2_(
125  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 R3_(
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 R4_(
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 
137  void transform_( uint32_t block[SHA1::BlockInts] );
138  void buffer_to_block_(const std::string& buffer, uint32_t block[SHA1::BlockInts]);
139 
140  // -------------------------------------------------------------------------
141  // Data Members
142  // -------------------------------------------------------------------------
143 
144 private:
145 
146  DigestType digest_;
147  std::string buffer_;
148  size_t transforms_;
149 
150 };
151 
152 } // namespace utils
153 } // namespace genesis
154 
155 #endif // include guard
DigestType final_digest()
Finish the calculation, prepare the object for next use, and return the digest.
Definition: sha1.cpp:106
std::string final_hex()
Finish the calculation, prepare the object for next use, and return the hash.
Definition: sha1.cpp:88
static DigestType from_string_digest(std::string const &input)
Calculate the hash digest for the content of a string.
Definition: sha1.cpp:176
SHA1 & operator=(SHA1 const &)=default
std::array< uint32_t, 5 > DigestType
Store a SHA1 digest.
Definition: sha1.hpp:74
static std::string from_string_hex(std::string const &input)
Calculate the checksum for the content of a string.
Definition: sha1.cpp:166
static const size_t BlockInts
Definition: sha1.hpp:64
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:155
static const size_t BlockBytes
Definition: sha1.hpp:65
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:144
void update(std::string const &s)
Add the contents of a string to the hash digest.
Definition: sha1.cpp:60
Calculate SHA1 hashes for strings and files.
Definition: sha1.hpp:55
SHA1()
Initialize the object for use.
Definition: sha1.cpp:48