navid/veins / 89aa02ed6b6660b6cf09e1a7700cf3b020a1b1e2bd33fd14dace8eb1a92db071 / xveins/src/crypto.h

crypto.h

← xveins/src · 1170 bytes · raw · download

#pragma once

#include "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