i2pdtunnelwizard/x25519/x25519cpp.h

40 lines
1.5 KiB
C++

// acetone, 2025
// I hate copyright of any kind. This is a public domain.
// Original source: http://git.community.i2p/acetone/i2pdtunnelwizard
// Notice for everyone who want reuse this file: this class uses i2p-styled base64
#pragma once
#include <string>
#include <array>
#include <vector>
#include <memory>
class X25519Keys
{
public:
X25519Keys(const std::array<uint8_t, 32>& sk);
X25519Keys() = default;
~X25519Keys() = default;
void generateKeys();
const std::array<uint8_t, 32> getPublicKey() const { return m_publicKey; }
const std::array<uint8_t, 32> getSecretKey() const { return m_secretKey; }
const std::shared_ptr<std::array<uint8_t, 32>> agree(const std::array<uint8_t, 32>& publicB64) const; // nullptr if failed
const std::shared_ptr<std::array<uint8_t, 32>> agree(const std::string& publicB64) const; // nullptr if failed
const std::shared_ptr<std::array<uint8_t, 32>> agree(const uint8_t* pkey, size_t size = 32) const; // nullptr if failed
bool setSecretKey (const std::vector<uint8_t>& sec, bool calculatePublic = false);
bool setSecretKey (const std::array<uint8_t, 32>& sec, bool calculatePublic = false);
bool setSecretKey (const std::string& sec, bool calculatePublic = false);
std::string getPublicKeyBase64() const;
std::string getSecretKeyBase64() const;
private:
bool setSecretKey (const uint8_t * sec, bool calculatePublic = false);
std::array<uint8_t, 32> m_secretKey;
std::array<uint8_t, 32> m_publicKey;
};