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 sha512
22};
23
24enum class hmac_algorithm
25{
26 sha256
27};
28
30size_t FZ_PUBLIC_SYMBOL get_digest_size(hash_algorithm);
31
32class buffer;
33
35class FZ_PUBLIC_SYMBOL hash_accumulator final
36{
37public:
40 hash_accumulator(hmac_algorithm algorithm, std::vector<uint8_t> const& key);
42
43 hash_accumulator(hash_accumulator const&) = delete;
44 hash_accumulator& operator=(hash_accumulator const&) = delete;
45
46 size_t digest_size() const;
47
48 void reinit();
49
50 void update(std::string_view const& data);
51 void update(std::basic_string_view<uint8_t> const& data);
52 void update(std::vector<uint8_t> const& data);
53 void update(uint8_t const* data, size_t size);
54 void update(buffer const& data);
55 void update(uint8_t in) {
56 update(&in, 1);
57 }
58
60 std::vector<uint8_t> digest();
61 void digest(uint8_t* out, size_t s);
62
63 operator std::vector<uint8_t>() {
64 return digest();
65 }
66
67 bool is_digest(std::string_view const& ref);
68 bool is_digest(uint8_t const* ref, size_t s);
69
70 template<typename T>
71 hash_accumulator& operator<<(T && in) {
72 update(std::forward<T>(in));
73 return *this;
74 }
75
77 std::vector<std::uint8_t> export_state();
78 bool import_state(std::vector<std::uint8_t> const& state);
79
80 class impl;
81private:
82 impl* impl_;
83};
84
89std::vector<uint8_t> FZ_PUBLIC_SYMBOL md5(std::string_view const& data);
90std::vector<uint8_t> FZ_PUBLIC_SYMBOL md5(std::vector<uint8_t> const& data);
91
93std::vector<uint8_t> FZ_PUBLIC_SYMBOL sha256(std::string_view const& data);
94std::vector<uint8_t> FZ_PUBLIC_SYMBOL sha256(std::vector<uint8_t> const& data);
95
100std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha1(std::string_view const& key, std::string_view const& data);
101std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha1(std::vector<uint8_t> const& key, std::vector<uint8_t> const& data);
102std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha1(std::vector<uint8_t> const& key, std::string_view const& data);
103std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha1(std::string_view const& key, std::vector<uint8_t> const& data);
104
106std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha256(std::string_view const& key, std::string_view const& data);
107std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha256(std::vector<uint8_t> const& key, std::vector<uint8_t> const& data);
108std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha256(std::vector<uint8_t> const& key, std::string_view const& data);
109std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha256(std::string_view const& key, std::vector<uint8_t> const& data);
110
111std::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);
112
113template <typename PasswordContainer, typename SaltContainer,
114 std::enable_if_t<sizeof(typename PasswordContainer::value_type) == sizeof(uint8_t) &&
115 sizeof(typename SaltContainer::value_type) == sizeof(uint8_t)>* = nullptr>
116std::vector<uint8_t> pbkdf2_hmac_sha256(PasswordContainer const& password, SaltContainer const& salt, size_t length, unsigned int iterations)
117{
118 return pbkdf2_hmac_sha256(std::basic_string_view<uint8_t>(reinterpret_cast<uint8_t const*>(password.data()), password.size()),
119 std::basic_string_view<uint8_t>(reinterpret_cast<uint8_t const*>(salt.data()), salt.size()),
120 length, iterations);
121}
122}
123
124#endif
The buffer class is a simple buffer where data can be appended at the end and consumed at the front....
Definition buffer.hpp:27
Accumulator for hashing large amounts of data.
Definition hash.hpp:36
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.
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 > 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 > 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.