libfilezilla
Loading...
Searching...
No Matches
hash.hpp
Go to the documentation of this file.
1#ifndef LIBFILEZILLA_HASH_HEADER
2#define LIBFILEZILLA_HASH_HEADER
3
7
8#include "libfilezilla.hpp"
9
10#include <vector>
11#include <string>
12
13namespace fz {
14
17{
18 md5, // insecure
19 sha1, // insecure
20 sha256,
21 sha384,
22 sha512,
23 sha3_256,
24 sha3_384,
25 sha3_512
26};
27
28enum class hmac_algorithm
29{
30 sha1, // insecure
31 sha256,
32 sha512
33};
34
36size_t FZ_PUBLIC_SYMBOL get_digest_size(hash_algorithm);
37
38class buffer;
39class secure_buffer;
40
42class FZ_PUBLIC_SYMBOL hash_accumulator final
43{
44public:
47 hash_accumulator(hmac_algorithm algorithm, std::vector<uint8_t> const& key);
48 hash_accumulator(hmac_algorithm algorithm, std::string_view const& key);
50
51 hash_accumulator(hash_accumulator const&) = delete;
52 hash_accumulator& operator=(hash_accumulator const&) = delete;
53
54 size_t digest_size() const;
55
56 void reinit();
57
58 void update(std::string_view const& data);
59 void update(std::basic_string_view<uint8_t> const& data);
60 void update(std::vector<uint8_t> const& data);
61 void update(uint8_t const* data, size_t size);
62 void update(buffer const& data);
63 void update(secure_buffer const& data);
64 void update(uint8_t in) {
65 update(&in, 1);
66 }
67
69 void update_uint32_be(uint32_t v);
70
72 void update_with_length(std::string_view const& data);
73
75 std::vector<uint8_t> digest();
76 void digest(uint8_t* out, size_t s);
77
78 operator std::vector<uint8_t>() {
79 return digest();
80 }
81
82 bool is_digest(std::string_view const& ref);
83 bool is_digest(uint8_t const* ref, size_t s);
84
85 template<typename T>
86 hash_accumulator& operator<<(T && in) {
87 update(std::forward<T>(in));
88 return *this;
89 }
90
92 std::vector<std::uint8_t> export_state();
93 bool import_state(std::vector<std::uint8_t> const& state);
94
95 class impl;
96private:
97 impl* impl_;
98};
99
104std::vector<uint8_t> FZ_PUBLIC_SYMBOL md5(std::string_view const& data);
105std::vector<uint8_t> FZ_PUBLIC_SYMBOL md5(std::vector<uint8_t> const& data);
106
108std::vector<uint8_t> FZ_PUBLIC_SYMBOL sha1(std::string_view const& data);
109std::vector<uint8_t> FZ_PUBLIC_SYMBOL sha1(std::vector<uint8_t> const& data);
110
112std::vector<uint8_t> FZ_PUBLIC_SYMBOL sha256(std::string_view const& data);
113std::vector<uint8_t> FZ_PUBLIC_SYMBOL sha256(std::vector<uint8_t> const& data);
114
116std::vector<uint8_t> FZ_PUBLIC_SYMBOL sha384(std::string_view const& data);
117std::vector<uint8_t> FZ_PUBLIC_SYMBOL sha384(std::vector<uint8_t> const& data);
118
120std::vector<uint8_t> FZ_PUBLIC_SYMBOL sha512(std::string_view const& data);
121std::vector<uint8_t> FZ_PUBLIC_SYMBOL sha512(std::vector<uint8_t> const& data);
122
127std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha1(std::string_view const& key, std::string_view const& data);
128std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha1(std::vector<uint8_t> const& key, std::vector<uint8_t> const& data);
129std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha1(std::vector<uint8_t> const& key, std::string_view const& data);
130std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha1(std::string_view const& key, std::vector<uint8_t> const& data);
131
133std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha256(std::string_view const& key, std::string_view const& data);
134std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha256(std::vector<uint8_t> const& key, std::vector<uint8_t> const& data);
135std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha256(std::vector<uint8_t> const& key, std::string_view const& data);
136std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha256(std::string_view const& key, std::vector<uint8_t> const& data);
137
138std::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);
139
140template <typename PasswordContainer, typename SaltContainer,
141 std::enable_if_t<sizeof(typename PasswordContainer::value_type) == sizeof(uint8_t) &&
142 sizeof(typename SaltContainer::value_type) == sizeof(uint8_t)>* = nullptr>
143std::vector<uint8_t> pbkdf2_hmac_sha256(PasswordContainer const& password, SaltContainer const& salt, size_t length, unsigned int iterations)
144{
145 return pbkdf2_hmac_sha256(std::basic_string_view<uint8_t>(reinterpret_cast<uint8_t const*>(password.data()), password.size()),
146 std::basic_string_view<uint8_t>(reinterpret_cast<uint8_t const*>(salt.data()), salt.size()),
147 length, iterations);
148}
149
150secure_buffer FZ_PUBLIC_SYMBOL pbkdf2(hmac_algorithm alg, std::string_view const& password, std::string_view const& salt, size_t length, unsigned int iterations);
151
152}
153
154#endif
Definition buffer.hpp:200
Accumulator for hashing large amounts of data.
Definition hash.hpp:43
std::vector< std::uint8_t > export_state()
Currently only implemented for SHA1.
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.
void update_with_length(std::string_view const &data)
Also feeds length, as uint32 in network byte order, to the hash.
void update_uint32_be(uint32_t v)
If platform is little-endian, converts to big-endian, aka network byte order, before feeding the hash...
Definition buffer.hpp:209
Sets some global macros and further includes string.hpp.
The namespace used by libfilezilla.
Definition apply.hpp:17
std::vector< uint8_t > sha1(std::string_view const &data)
Standard SHA1.
hash_algorithm
List of supported hashing algorithms.
Definition hash.hpp:17
std::vector< uint8_t > md5(std::string_view const &data)
Standard MD5.
size_t get_digest_size(hash_algorithm)
Returns digest size in bytes.
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 > sha384(std::string_view const &data)
Standard SHA384.
std::vector< uint8_t > sha256(std::string_view const &data)
Standard SHA256.
std::vector< uint8_t > hmac_sha256(std::string_view const &key, std::string_view const &data)
Standard HMAC using SHA256.
@ password
Definition impersonation.hpp:152