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#include <cstdint>
13
14namespace fz {
15
18{
19 md5, // insecure
20 sha1, // insecure
21 sha256,
22 sha384,
23 sha512,
24 sha3_256,
25 sha3_384,
26 sha3_512
27};
28
29enum class hmac_algorithm
30{
31 sha1, // insecure
32 sha256,
33 sha512
34};
35
37size_t FZ_PUBLIC_SYMBOL get_digest_size(hash_algorithm);
38
39class buffer;
40class secure_buffer;
41
43class FZ_PUBLIC_SYMBOL hash_accumulator final
44{
45public:
48 hash_accumulator(hmac_algorithm algorithm, std::vector<uint8_t> const& key);
49 hash_accumulator(hmac_algorithm algorithm, std::string_view const& key);
51
52 hash_accumulator(hash_accumulator const&) = delete;
53 hash_accumulator& operator=(hash_accumulator const&) = delete;
54
55 size_t digest_size() const;
56
57 void reinit();
58
59 void update(std::string_view const& data);
60 void update(std::basic_string_view<uint8_t> const& data);
61 void update(std::vector<uint8_t> const& data);
62 void update(uint8_t const* data, size_t size);
63 void update(buffer const& data);
64 void update(secure_buffer const& data);
65 void update(uint8_t in) {
66 update(&in, 1);
67 }
68
70 void update_uint32_be(uint32_t v);
71
73 void update_with_length(std::string_view const& data);
74
76 std::vector<uint8_t> digest();
77 void digest(uint8_t* out, size_t s);
78
79 operator std::vector<uint8_t>() {
80 return digest();
81 }
82
83 bool is_digest(std::string_view const& ref);
84 bool is_digest(uint8_t const* ref, size_t s);
85
86 template<typename T>
87 hash_accumulator& operator<<(T && in) {
88 update(std::forward<T>(in));
89 return *this;
90 }
91
93 std::vector<std::uint8_t> export_state();
94 bool import_state(std::vector<std::uint8_t> const& state);
95
96 class impl;
97private:
98 impl* impl_;
99};
100
105std::vector<uint8_t> FZ_PUBLIC_SYMBOL md5(std::string_view const& data);
106std::vector<uint8_t> FZ_PUBLIC_SYMBOL md5(std::vector<uint8_t> const& data);
107
109std::vector<uint8_t> FZ_PUBLIC_SYMBOL sha1(std::string_view const& data);
110std::vector<uint8_t> FZ_PUBLIC_SYMBOL sha1(std::vector<uint8_t> const& data);
111
113std::vector<uint8_t> FZ_PUBLIC_SYMBOL sha256(std::string_view const& data);
114std::vector<uint8_t> FZ_PUBLIC_SYMBOL sha256(std::vector<uint8_t> const& data);
115
117std::vector<uint8_t> FZ_PUBLIC_SYMBOL sha384(std::string_view const& data);
118std::vector<uint8_t> FZ_PUBLIC_SYMBOL sha384(std::vector<uint8_t> const& data);
119
121std::vector<uint8_t> FZ_PUBLIC_SYMBOL sha512(std::string_view const& data);
122std::vector<uint8_t> FZ_PUBLIC_SYMBOL sha512(std::vector<uint8_t> const& data);
123
128std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha1(std::string_view const& key, std::string_view const& data);
129std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha1(std::vector<uint8_t> const& key, std::vector<uint8_t> const& data);
130std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha1(std::vector<uint8_t> const& key, std::string_view const& data);
131std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha1(std::string_view const& key, std::vector<uint8_t> const& data);
132
134std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha256(std::string_view const& key, std::string_view const& data);
135std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha256(std::vector<uint8_t> const& key, std::vector<uint8_t> const& data);
136std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha256(std::vector<uint8_t> const& key, std::string_view const& data);
137std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha256(std::string_view const& key, std::vector<uint8_t> const& data);
138
139std::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);
140
141template <typename PasswordContainer, typename SaltContainer,
142 std::enable_if_t<sizeof(typename PasswordContainer::value_type) == sizeof(uint8_t) &&
143 sizeof(typename SaltContainer::value_type) == sizeof(uint8_t)>* = nullptr>
144std::vector<uint8_t> pbkdf2_hmac_sha256(PasswordContainer const& password, SaltContainer const& salt, size_t length, unsigned int iterations)
145{
146 return pbkdf2_hmac_sha256(std::basic_string_view<uint8_t>(reinterpret_cast<uint8_t const*>(password.data()), password.size()),
147 std::basic_string_view<uint8_t>(reinterpret_cast<uint8_t const*>(salt.data()), salt.size()),
148 length, iterations);
149}
150
151secure_buffer FZ_PUBLIC_SYMBOL pbkdf2(hmac_algorithm alg, std::string_view const& password, std::string_view const& salt, size_t length, unsigned int iterations);
152
153}
154
155#endif
Definition buffer.hpp:201
Accumulator for hashing large amounts of data.
Definition hash.hpp:44
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:210
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:18
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