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 
25 class FZ_PUBLIC_SYMBOL hash_accumulator final
26 {
27 public:
31 
32  hash_accumulator(hash_accumulator const&) = delete;
33  hash_accumulator& operator=(hash_accumulator const&) = delete;
34 
35  void reinit();
36 
37  void update(std::string_view const& data);
38  void update(std::basic_string_view<uint8_t> const& data);
39  void update(std::vector<uint8_t> const& data);
40  void update(uint8_t const* data, size_t size);
41  void update(uint8_t in) {
42  update(&in, 1);
43  }
44 
46  std::vector<uint8_t> digest();
47 
48  operator std::vector<uint8_t>() {
49  return digest();
50  }
51 
52  template<typename T>
53  hash_accumulator& operator<<(T && in) {
54  update(std::forward<T>(in));
55  return *this;
56  }
57 
58  class impl;
59 private:
60  impl* impl_;
61 };
62 
67 std::vector<uint8_t> FZ_PUBLIC_SYMBOL md5(std::string_view const& data);
68 std::vector<uint8_t> FZ_PUBLIC_SYMBOL md5(std::vector<uint8_t> const& data);
69 
71 std::vector<uint8_t> FZ_PUBLIC_SYMBOL sha256(std::string_view const& data);
72 std::vector<uint8_t> FZ_PUBLIC_SYMBOL sha256(std::vector<uint8_t> const& data);
73 
78 std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha1(std::string_view const& key, std::string_view const& data);
79 std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha1(std::vector<uint8_t> const& key, std::vector<uint8_t> const& data);
80 std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha1(std::vector<uint8_t> const& key, std::string_view const& data);
81 std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha1(std::string_view const& key, std::vector<uint8_t> const& data);
82 
84 std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha256(std::string_view const& key, std::string_view const& data);
85 std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha256(std::vector<uint8_t> const& key, std::vector<uint8_t> const& data);
86 std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha256(std::vector<uint8_t> const& key, std::string_view const& data);
87 std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha256(std::string_view const& key, std::vector<uint8_t> const& data);
88 
89 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);
90 
91 template <typename PasswordContainer, typename SaltContainer,
92  std::enable_if_t<sizeof(typename PasswordContainer::value_type) == sizeof(uint8_t) &&
93  sizeof(typename SaltContainer::value_type) == sizeof(uint8_t)>* = nullptr>
94 std::vector<uint8_t> pbkdf2_hmac_sha256(PasswordContainer const& password, SaltContainer const& salt, size_t length, unsigned int iterations)
95 {
96  return pbkdf2_hmac_sha256(std::basic_string_view<uint8_t>(reinterpret_cast<uint8_t const*>(password.data()), password.size()),
97  std::basic_string_view<uint8_t>(reinterpret_cast<uint8_t const*>(salt.data()), salt.size()),
98  length, iterations);
99 }
100 }
101 
102 #endif
Accumulator for hashing large amounts of data.
Definition: hash.hpp:26
std::vector< uint8_t > digest()
Returns the raw digest and reinitializes the accumulator.
hash_accumulator(hash_algorithm algorithm)
Creates an initialized accumulator for the passed algorithm.
Sets some global macros and further includes string.hpp.
The namespace used by libfilezilla.
Definition: apply.hpp:17
hash_algorithm
List of supported hashing algorithms.
Definition: hash.hpp:17
std::vector< uint8_t > hmac_sha256(std::string_view const &key, std::string_view const &data)
Standard HMAC using SHA256.
std::vector< uint8_t > hmac_sha1(std::string_view const &key, std::string_view const &data)
Standard HMAC using SHA1.
std::vector< uint8_t > sha256(std::string_view const &data)
Standard SHA256.
std::vector< uint8_t > md5(std::string_view const &data)
Standard MD5.