libfilezilla
encryption.hpp
Go to the documentation of this file.
1 #ifndef LIBFILEZILLA_ENCRYPTION_HEADER
2 #define LIBFILEZILLA_ENCRYPTION_HEADER
3 
10 #include "libfilezilla.hpp"
11 
12 #include <vector>
13 #include <string>
14 
15 namespace fz {
16 
21 class FZ_PUBLIC_SYMBOL public_key
22 {
23 public:
25  enum {
26  key_size = 32,
27  salt_size = 32
28  };
29 
30  explicit operator bool() const {
31  return key_.size() == key_size && salt_.size() == salt_size;
32  }
33 
34  bool operator==(public_key const& rhs) const {
35  return key_ == rhs.key_ && salt_ == rhs.salt_;
36  }
37 
38  bool operator!=(public_key const& rhs) const {
39  return !(*this == rhs);
40  }
41 
42  bool operator<(public_key const& rhs) const {
43  return key_ < rhs.key_ || (key_ == rhs.key_ && salt_ < rhs.salt_);
44  }
45 
46  std::string to_base64() const;
47  static public_key from_base64(std::string const& base64);
48 
49  std::vector<uint8_t> key_;
50  std::vector<uint8_t> salt_;
51 };
52 
57 class FZ_PUBLIC_SYMBOL private_key
58 {
59 public:
61  enum {
62  key_size = 32,
63  salt_size = 32
64  };
65 
67  static private_key generate();
68 
70  static private_key from_password(std::vector<uint8_t> const& password, std::vector<uint8_t> const& salt);
71  static private_key from_password(std::string const& password, std::vector<uint8_t> const& salt)
72  {
73  return from_password(std::vector<uint8_t>(password.begin(), password.end()), salt);
74  }
75 
76  explicit operator bool() const {
77  return key_.size() == key_size && salt_.size() == salt_size;
78  }
79 
80  std::vector<uint8_t> const& salt() const {
81  return salt_;
82  }
83 
85  public_key pubkey() const;
86 
88  std::vector<uint8_t> shared_secret(public_key const& pub) const;
89 
90  std::string to_base64() const;
91  static private_key from_base64(std::string const& base64);
92 
93 private:
94  std::vector<uint8_t> key_;
95  std::vector<uint8_t> salt_;
96 };
97 
119 std::vector<uint8_t> FZ_PUBLIC_SYMBOL encrypt(std::vector<uint8_t> const& plain, public_key const& pub, bool authenticated = true);
120 std::vector<uint8_t> FZ_PUBLIC_SYMBOL encrypt(std::string const& plain, public_key const& pub, bool authenticated = true);
121 std::vector<uint8_t> FZ_PUBLIC_SYMBOL encrypt(uint8_t const* plain, size_t size, public_key const& pub, bool authenticated = true);
122 
148 std::vector<uint8_t> FZ_PUBLIC_SYMBOL decrypt(std::vector<uint8_t> const& chiper, private_key const& priv, bool authenticated = true);
149 std::vector<uint8_t> FZ_PUBLIC_SYMBOL decrypt(std::string const& chiper, private_key const& priv, bool authenticated = true);
150 std::vector<uint8_t> FZ_PUBLIC_SYMBOL decrypt(uint8_t const* cipher, size_t size, private_key const& priv, bool authenticated = true);
151 
152 }
153 #endif
std::vector< uint8_t > encrypt(std::vector< uint8_t > const &plain, public_key const &pub, bool authenticated=true)
Encrypt the plaintext to the given public key.
Represents a X25519 public key with associated salt.
Definition: encryption.hpp:21
std::vector< uint8_t > decrypt(std::vector< uint8_t > const &chiper, private_key const &priv, bool authenticated=true)
Decrypt the ciphertext using the given private key.
The namespace used by libfilezilla.
Definition: apply.hpp:16
Sets some global macros and further includes string.hpp.
Represents a X25519 private key with associated salt.
Definition: encryption.hpp:57