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
58 class impl;
59private:
60 impl* impl_;
61};
62
67std::vector<uint8_t> FZ_PUBLIC_SYMBOL md5(std::string_view const& data);
68std::vector<uint8_t> FZ_PUBLIC_SYMBOL md5(std::vector<uint8_t> const& data);
69
71std::vector<uint8_t> FZ_PUBLIC_SYMBOL sha256(std::string_view const& data);
72std::vector<uint8_t> FZ_PUBLIC_SYMBOL sha256(std::vector<uint8_t> const& data);
73
78std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha1(std::string_view const& key, std::string_view const& data);
79std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha1(std::vector<uint8_t> const& key, std::vector<uint8_t> const& data);
80std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha1(std::vector<uint8_t> const& key, std::string_view const& data);
81std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha1(std::string_view const& key, std::vector<uint8_t> const& data);
82
84std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha256(std::string_view const& key, std::string_view const& data);
85std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha256(std::vector<uint8_t> const& key, std::vector<uint8_t> const& data);
86std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha256(std::vector<uint8_t> const& key, std::string_view const& data);
87std::vector<uint8_t> FZ_PUBLIC_SYMBOL hmac_sha256(std::string_view const& key, std::vector<uint8_t> const& data);
88
89std::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);
90
91template <typename PasswordContainer, typename SaltContainer,
92 std::enable_if_t<sizeof(typename PasswordContainer::value_type) == sizeof(uint8_t) &&
93 sizeof(typename SaltContainer::value_type) == sizeof(uint8_t)>* = nullptr>
94std::vector<uint8_t> pbkdf2_hmac_sha256(PasswordContainer const& password, SaltContainer const& salt, size_t length, unsigned int iterations)
95{
96 return pbkdf2_hmac_sha256(std::basic_string_view<uint8_t>(reinterpret_cast<uint8_t const*>(password.data()), password.size()),
97 std::basic_string_view<uint8_t>(reinterpret_cast<uint8_t const*>(salt.data()), salt.size()),
98 length, iterations);
99}
100}
101
102#endif
Accumulator for hashing large amounts of data.
Definition: hash.hpp:26
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.