libfilezilla
hash.hpp
Go to the documentation of this file.
1 #ifndef LIBFILEZILLA_HASH_HEADER
2 #define LIBFILEZILLA_HASH_HEADER
3 
8 #include "libfilezilla.hpp"
9 
10 #include <vector>
11 #include <string>
12 
13 namespace fz {
14 
16 enum class hash_algorithm
17 {
18  md5, // insecure
19  sha1, // insecure
20  sha256,
21  sha384,
22  sha512
23 };
24 
25 enum class hmac_algorithm
26 {
27  sha1, // insecure
28  sha256,
29  sha512
30 };
31 
33 size_t FZ_PUBLIC_SYMBOL get_digest_size(hash_algorithm);
34 
35 class buffer;
36 
38 class FZ_PUBLIC_SYMBOL hash_accumulator final
39 {
40 public:
43  hash_accumulator(hmac_algorithm algorithm, std::vector<uint8_t> const& key);
44  hash_accumulator(hmac_algorithm algorithm, std::string_view const& key);
46 
47  hash_accumulator(hash_accumulator const&) = delete;
48  hash_accumulator& operator=(hash_accumulator const&) = delete;
49 
50  size_t digest_size() const;
51 
52  void reinit();
53 
54  void update(std::string_view const& data);
55  void update(std::basic_string_view<uint8_t> const& data);
56  void update(std::vector<uint8_t> const& data);
57  void update(uint8_t const* data, size_t size);
58  void update(buffer const& data);
59  void update(uint8_t in) {
60  update(&in, 1);
61  }
62 
64  void update_uint32_be(uint32_t v);
65 
67  void update_with_length(std::string_view const& data);
68 
70  std::vector<uint8_t> digest();
71  void digest(uint8_t* out, size_t s);
72 
73  operator std::vector<uint8_t>() {
74  return digest();
75  }
76 
77  bool is_digest(std::string_view const& ref);
78  bool is_digest(uint8_t const* ref, size_t s);
79 
80  template<typename T>
81  hash_accumulator& operator<<(T && in) {
82  update(std::forward<T>(in));
83  return *this;
84  }
85 
87  std::vector<std::uint8_t> export_state();
88  bool import_state(std::vector<std::uint8_t> const& state);
89 
90  class impl;
91 private:
92  impl* impl_;
93 };
94 
99 std::vector<uint8_t> FZ_PUBLIC_SYMBOL md5(std::string_view const& data);
100 std::vector<uint8_t> FZ_PUBLIC_SYMBOL md5(std::vector<uint8_t> const& data);
101 
103 std::vector<uint8_t> FZ_PUBLIC_SYMBOL sha256(std::string_view const& data);
104 std::vector<uint8_t> FZ_PUBLIC_SYMBOL sha256(std::vector<uint8_t> const& data);
105 
107 std::vector<uint8_t> FZ_PUBLIC_SYMBOL sha512(std::string_view const& data);
108 std::vector<uint8_t> FZ_PUBLIC_SYMBOL sha512(std::vector<uint8_t> const& data);
109 
114 std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha1(std::string_view const& key, std::string_view const& data);
115 std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha1(std::vector<uint8_t> const& key, std::vector<uint8_t> const& data);
116 std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha1(std::vector<uint8_t> const& key, std::string_view const& data);
117 std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha1(std::string_view const& key, std::vector<uint8_t> const& data);
118 
120 std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha256(std::string_view const& key, std::string_view const& data);
121 std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha256(std::vector<uint8_t> const& key, std::vector<uint8_t> const& data);
122 std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha256(std::vector<uint8_t> const& key, std::string_view const& data);
123 std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha256(std::string_view const& key, std::vector<uint8_t> const& data);
124 
125 std::vector<uint8_t> FZ_PUBLIC_SYMBOL pbkdf2_hmac_sha256(std::basic_string_view<uint8_t> const& password, std::basic_string_view<uint8_t> const& salt, size_t length, unsigned int iterations);
126 
127 template <typename PasswordContainer, typename SaltContainer,
128  std::enable_if_t<sizeof(typename PasswordContainer::value_type) == sizeof(uint8_t) &&
129  sizeof(typename SaltContainer::value_type) == sizeof(uint8_t)>* = nullptr>
130 std::vector<uint8_t> pbkdf2_hmac_sha256(PasswordContainer const& password, SaltContainer const& salt, size_t length, unsigned int iterations)
131 {
132  return pbkdf2_hmac_sha256(std::basic_string_view<uint8_t>(reinterpret_cast<uint8_t const*>(password.data()), password.size()),
133  std::basic_string_view<uint8_t>(reinterpret_cast<uint8_t const*>(salt.data()), salt.size()),
134  length, iterations);
135 }
136 }
137 
138 #endif
std::vector< uint8_t > md5(std::string_view const &data)
Standard MD5.
std::vector< uint8_t > hmac_sha1(std::string_view const &key, std::string_view const &data)
Standard HMAC using SHA1.
std::vector< uint8_t > sha512(std::string_view const &data)
Standard SHA512.
std::vector< uint8_t > hmac_sha256(std::string_view const &key, std::string_view const &data)
Standard HMAC using SHA256.
hash_algorithm
List of supported hashing algorithms.
Definition: hash.hpp:16
Accumulator for hashing large amounts of data.
Definition: hash.hpp:38
size_t get_digest_size(hash_algorithm)
Returns digest size in bytes.
The namespace used by libfilezilla.
Definition: apply.hpp:17
Sets some global macros and further includes string.hpp.
The buffer class is a simple buffer where data can be appended at the end and consumed at the front...
Definition: buffer.hpp:26
std::vector< uint8_t > sha256(std::string_view const &data)
Standard SHA256.