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
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
25class FZ_PUBLIC_SYMBOL hash_accumulator final
26{
27public:
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
59 std::vector<std::uint8_t> export_state();
60 bool import_state(std::vector<std::uint8_t> const& state);
61
62 class impl;
63private:
64 impl* impl_;
65};
66
71std::vector<uint8_t> FZ_PUBLIC_SYMBOL md5(std::string_view const& data);
72std::vector<uint8_t> FZ_PUBLIC_SYMBOL md5(std::vector<uint8_t> const& data);
73
75std::vector<uint8_t> FZ_PUBLIC_SYMBOL sha256(std::string_view const& data);
76std::vector<uint8_t> FZ_PUBLIC_SYMBOL sha256(std::vector<uint8_t> const& data);
77
82std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha1(std::string_view const& key, std::string_view const& data);
83std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha1(std::vector<uint8_t> const& key, std::vector<uint8_t> const& data);
84std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha1(std::vector<uint8_t> const& key, std::string_view const& data);
85std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha1(std::string_view const& key, std::vector<uint8_t> const& data);
86
88std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha256(std::string_view const& key, std::string_view const& data);
89std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha256(std::vector<uint8_t> const& key, std::vector<uint8_t> const& data);
90std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha256(std::vector<uint8_t> const& key, std::string_view const& data);
91std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha256(std::string_view const& key, std::vector<uint8_t> const& data);
92
93std::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);
94
95template <typename PasswordContainer, typename SaltContainer,
96 std::enable_if_t<sizeof(typename PasswordContainer::value_type) == sizeof(uint8_t) &&
97 sizeof(typename SaltContainer::value_type) == sizeof(uint8_t)>* = nullptr>
98std::vector<uint8_t> pbkdf2_hmac_sha256(PasswordContainer const& password, SaltContainer const& salt, size_t length, unsigned int iterations)
99{
100 return pbkdf2_hmac_sha256(std::basic_string_view<uint8_t>(reinterpret_cast<uint8_t const*>(password.data()), password.size()),
101 std::basic_string_view<uint8_t>(reinterpret_cast<uint8_t const*>(salt.data()), salt.size()),
102 length, iterations);
103}
104}
105
106#endif
Accumulator for hashing large amounts of data.
Definition: hash.hpp:26
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.
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.