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  sha512
22 };
23 
24 enum class hmac_algorithm
25 {
26  sha256
27 };
28 
29 class buffer;
30 
32 class FZ_PUBLIC_SYMBOL hash_accumulator final
33 {
34 public:
37  hash_accumulator(hmac_algorithm algorithm, std::vector<uint8_t> const& key);
39 
40  hash_accumulator(hash_accumulator const&) = delete;
41  hash_accumulator& operator=(hash_accumulator const&) = delete;
42 
43  size_t digest_size() const;
44 
45  void reinit();
46 
47  void update(std::string_view const& data);
48  void update(std::basic_string_view<uint8_t> const& data);
49  void update(std::vector<uint8_t> const& data);
50  void update(uint8_t const* data, size_t size);
51  void update(buffer const& data);
52  void update(uint8_t in) {
53  update(&in, 1);
54  }
55 
57  std::vector<uint8_t> digest();
58  void digest(uint8_t* out, size_t s);
59 
60  operator std::vector<uint8_t>() {
61  return digest();
62  }
63 
64  bool is_digest(std::string_view const& ref);
65  bool is_digest(uint8_t const* ref, size_t s);
66 
67  template<typename T>
68  hash_accumulator& operator<<(T && in) {
69  update(std::forward<T>(in));
70  return *this;
71  }
72 
74  std::vector<std::uint8_t> export_state();
75  bool import_state(std::vector<std::uint8_t> const& state);
76 
77  class impl;
78 private:
79  impl* impl_;
80 };
81 
86 std::vector<uint8_t> FZ_PUBLIC_SYMBOL md5(std::string_view const& data);
87 std::vector<uint8_t> FZ_PUBLIC_SYMBOL md5(std::vector<uint8_t> const& data);
88 
90 std::vector<uint8_t> FZ_PUBLIC_SYMBOL sha256(std::string_view const& data);
91 std::vector<uint8_t> FZ_PUBLIC_SYMBOL sha256(std::vector<uint8_t> const& data);
92 
97 std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha1(std::string_view const& key, std::string_view const& data);
98 std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha1(std::vector<uint8_t> const& key, std::vector<uint8_t> const& data);
99 std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha1(std::vector<uint8_t> const& key, std::string_view const& data);
100 std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha1(std::string_view const& key, std::vector<uint8_t> const& data);
101 
103 std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha256(std::string_view const& key, std::string_view const& data);
104 std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha256(std::vector<uint8_t> const& key, std::vector<uint8_t> const& data);
105 std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha256(std::vector<uint8_t> const& key, std::string_view const& data);
106 std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha256(std::string_view const& key, std::vector<uint8_t> const& data);
107 
108 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);
109 
110 template <typename PasswordContainer, typename SaltContainer,
111  std::enable_if_t<sizeof(typename PasswordContainer::value_type) == sizeof(uint8_t) &&
112  sizeof(typename SaltContainer::value_type) == sizeof(uint8_t)>* = nullptr>
113 std::vector<uint8_t> pbkdf2_hmac_sha256(PasswordContainer const& password, SaltContainer const& salt, size_t length, unsigned int iterations)
114 {
115  return pbkdf2_hmac_sha256(std::basic_string_view<uint8_t>(reinterpret_cast<uint8_t const*>(password.data()), password.size()),
116  std::basic_string_view<uint8_t>(reinterpret_cast<uint8_t const*>(salt.data()), salt.size()),
117  length, iterations);
118 }
119 }
120 
121 #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 > 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:32
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.