navid/veins / 4fd13fa7c7603bf558806ccfb7f45c765d05b76656b66e4497a9bbab0ca1f09f / xveins/src/crypto/crypto.h

crypto.h

← xveins/src/crypto · 1155 bytes · raw · download

#pragma once

#include "core/common.h"

#include <array>
#include <optional>
#include <string>
#include <string_view>
#include <vector>

namespace vn {

using PublicKey = std::array<std::uint8_t, 32>;
using Signature = std::array<std::uint8_t, 64>;
using FingerprintBytes = std::array<std::uint8_t, 16>;

std::optional<PublicKey> ed25519_public_from_seed(Bytes seed);
std::optional<Signature> ed25519_sign(Bytes seed, Bytes message);
bool ed25519_verify(const PublicKey& public_key, Bytes message, Bytes signature);

FingerprintBytes key_fingerprint_bytes(const PublicKey& public_key);
std::string key_fingerprint(const PublicKey& public_key);

// RFC 4648 base32 with no padding. The reference implementation emits
// fingerprints in uppercase, so this matches it.
std::string base32_text(Bytes raw);
// Unpadded base64url.
std::string base64url_encode(Bytes raw);
std::optional<std::vector<std::uint8_t>> base64url_decode(std::string_view text);

std::string format_public_key(const PublicKey& public_key);
std::optional<PublicKey> parse_public_key(std::string_view text);

std::vector<std::uint8_t> secure_random(std::size_t count);

} // namespace vn