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};
25
26enum class hmac_algorithm
27{
28 sha1, // insecure
29 sha256,
30 sha512
31};
32
34size_t FZ_PUBLIC_SYMBOL get_digest_size(hash_algorithm);
35
36class buffer;
37
39class FZ_PUBLIC_SYMBOL hash_accumulator final
40{
41public:
44 hash_accumulator(hmac_algorithm algorithm, std::vector<uint8_t> const& key);
45 hash_accumulator(hmac_algorithm algorithm, std::string_view const& key);
47
48 hash_accumulator(hash_accumulator const&) = delete;
49 hash_accumulator& operator=(hash_accumulator const&) = delete;
50
51 size_t digest_size() const;
52
53 void reinit();
54
55 void update(std::string_view const& data);
56 void update(std::basic_string_view<uint8_t> const& data);
57 void update(std::vector<uint8_t> const& data);
58 void update(uint8_t const* data, size_t size);
59 void update(buffer const& data);
60 void update(uint8_t in) {
61 update(&in, 1);
62 }
63
65 void update_uint32_be(uint32_t v);
66
68 void update_with_length(std::string_view const& data);
69
71 std::vector<uint8_t> digest();
72 void digest(uint8_t* out, size_t s);
73
74 operator std::vector<uint8_t>() {
75 return digest();
76 }
77
78 bool is_digest(std::string_view const& ref);
79 bool is_digest(uint8_t const* ref, size_t s);
80
81 template<typename T>
82 hash_accumulator& operator<<(T && in) {
83 update(std::forward<T>(in));
84 return *this;
85 }
86
88 std::vector<std::uint8_t> export_state();
89 bool import_state(std::vector<std::uint8_t> const& state);
90
91 class impl;
92private:
93 impl* impl_;
94};
95
100std::vector<uint8_t> FZ_PUBLIC_SYMBOL md5(std::string_view const& data);
101std::vector<uint8_t> FZ_PUBLIC_SYMBOL md5(std::vector<uint8_t> const& data);
102
104std::vector<uint8_t> FZ_PUBLIC_SYMBOL sha256(std::string_view const& data);
105std::vector<uint8_t> FZ_PUBLIC_SYMBOL sha256(std::vector<uint8_t> const& data);
106
108std::vector<uint8_t> FZ_PUBLIC_SYMBOL sha512(std::string_view const& data);
109std::vector<uint8_t> FZ_PUBLIC_SYMBOL sha512(std::vector<uint8_t> const& data);
110
115std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha1(std::string_view const& key, std::string_view const& data);
116std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha1(std::vector<uint8_t> const& key, std::vector<uint8_t> const& data);
117std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha1(std::vector<uint8_t> const& key, std::string_view const& data);
118std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha1(std::string_view const& key, std::vector<uint8_t> const& data);
119
121std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha256(std::string_view const& key, std::string_view const& data);
122std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha256(std::vector<uint8_t> const& key, std::vector<uint8_t> const& data);
123std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha256(std::vector<uint8_t> const& key, std::string_view const& data);
124std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha256(std::string_view const& key, std::vector<uint8_t> const& data);
125
126std::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);
127
128template <typename PasswordContainer, typename SaltContainer,
129 std::enable_if_t<sizeof(typename PasswordContainer::value_type) == sizeof(uint8_t) &&
130 sizeof(typename SaltContainer::value_type) == sizeof(uint8_t)>* = nullptr>
131std::vector<uint8_t> pbkdf2_hmac_sha256(PasswordContainer const& password, SaltContainer const& salt, size_t length, unsigned int iterations)
132{
133 return pbkdf2_hmac_sha256(std::basic_string_view<uint8_t>(reinterpret_cast<uint8_t const*>(password.data()), password.size()),
134 std::basic_string_view<uint8_t>(reinterpret_cast<uint8_t const*>(salt.data()), salt.size()),
135 length, iterations);
136}
137}
138
139#endif
The buffer class is a simple buffer where data can be appended at the end and consumed at the front....
Definition buffer.hpp:28
Accumulator for hashing large amounts of data.
Definition hash.hpp:40
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...
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: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 > 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.