ed25519.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Based on
  3. * 1. OpenSSL lib
  4. * 2. PurpleI2P source code
  5. * 3. cppcodec lib
  6. *
  7. * PUBLIC DOMAIN C++ WRAPPER
  8. * acetone, 2022
  9. */
  10. #ifndef ED25519_H
  11. #define ED25519_H
  12. #include <vector>
  13. #include <string>
  14. #include <array>
  15. #include <memory>
  16. using uint8_t = unsigned char;
  17. namespace FriendlyCrypto {
  18. namespace Ed25519 {
  19. class KeyPair {
  20. public:
  21. KeyPair() {}
  22. void generateKeys() noexcept;
  23. void setSecretKey (const std::array<uint8_t, 32>& secret) noexcept;
  24. const std::array<uint8_t, 32> getSecretKey() const noexcept;
  25. const std::array<uint8_t, 32> getPublicKey() const noexcept;
  26. const std::string getPublicKeyBase64String() const noexcept;
  27. const std::string getSecretKeyBase64String() const noexcept;
  28. private:
  29. std::array<uint8_t, 32> m_secret {0};
  30. std::array<uint8_t, 32> m_public {0};
  31. };
  32. class Signature {
  33. public:
  34. Signature(const std::array<uint8_t, 64>& raw)
  35. : m_data( new std::array<uint8_t, 64>(raw) ) {}
  36. const std::shared_ptr<std::array<uint8_t, 64>> data() const noexcept;
  37. const std::string base64String() const noexcept;
  38. bool operator==(const Ed25519::Signature& another) const noexcept;
  39. bool operator==(const std::array<uint8_t, 64>& rawAnother) const noexcept;
  40. bool operator==(const std::string& base64String) const noexcept;
  41. private:
  42. std::shared_ptr<std::array<uint8_t, 64>> m_data;
  43. };
  44. //// FUNCTIONS
  45. Ed25519::Signature sign (const std::vector<uint8_t>& message, const std::array<uint8_t, 32>& secretKey) noexcept;
  46. bool verify (const std::vector<uint8_t>& message, const Ed25519::Signature& signature, const std::array<uint8_t, 32>& publicKey) noexcept;
  47. } // namespace Ed25519
  48. } // namespace FriendlyCrypto
  49. #endif // ED25519_H