79 lines
2.6 KiB
C++
79 lines
2.6 KiB
C++
#pragma once
|
|
|
|
#include <Void/Functional/Error.hpp>
|
|
#include <Void/Functional/Result.hpp>
|
|
|
|
#include <cstddef>
|
|
#include <filesystem>
|
|
#include <memory>
|
|
#include <span>
|
|
#include <string_view>
|
|
#include <vector>
|
|
|
|
namespace Void::Crypto
|
|
{
|
|
enum class HashAlgorithm { Sha1, Sha256, Sha512 };
|
|
using Digest = std::vector<std::byte>;
|
|
|
|
class Hasher
|
|
{
|
|
public:
|
|
|
|
[[nodiscard]] static Void::Result::Result<Hasher>
|
|
create(HashAlgorithm algorithm);
|
|
~Hasher();
|
|
Hasher(Hasher&&) noexcept;
|
|
Hasher& operator=(Hasher&&) noexcept;
|
|
Hasher(const Hasher&) = delete;
|
|
Hasher& operator=(const Hasher&) = delete;
|
|
|
|
[[nodiscard]] Void::Result::Result<void>
|
|
update(std::span<const std::byte> data);
|
|
[[nodiscard]] Void::Result::Result<void>
|
|
update(std::string_view data);
|
|
/// Finalize this hash. Calling finalize or update again is an
|
|
/// error.
|
|
[[nodiscard]] Void::Result::Result<Digest> finalize();
|
|
|
|
private:
|
|
|
|
struct Impl;
|
|
std::unique_ptr<Impl> m_impl;
|
|
explicit Hasher(std::unique_ptr<Impl> impl) noexcept;
|
|
};
|
|
|
|
[[nodiscard]] Void::Result::Result<Digest>
|
|
hash(HashAlgorithm algorithm, std::span<const std::byte> data);
|
|
[[nodiscard]] Void::Result::Result<Digest> hash(HashAlgorithm algorithm,
|
|
std::string_view data);
|
|
[[nodiscard]] Void::Result::Result<Digest>
|
|
hashFile(HashAlgorithm algorithm, const std::filesystem::path& path);
|
|
|
|
[[nodiscard]] Void::Result::Result<Digest>
|
|
sha1(std::span<const std::byte> data);
|
|
[[nodiscard]] Void::Result::Result<Digest> sha1(std::string_view data);
|
|
[[nodiscard]] Void::Result::Result<Digest>
|
|
sha256(std::span<const std::byte> data);
|
|
[[nodiscard]] Void::Result::Result<Digest> sha256(std::string_view data);
|
|
[[nodiscard]] Void::Result::Result<Digest>
|
|
sha512(std::span<const std::byte> data);
|
|
[[nodiscard]] Void::Result::Result<Digest> sha512(std::string_view data);
|
|
[[nodiscard]] Void::Result::Result<Digest>
|
|
sha1File(const std::filesystem::path& path);
|
|
[[nodiscard]] Void::Result::Result<Digest>
|
|
sha256File(const std::filesystem::path& path);
|
|
[[nodiscard]] Void::Result::Result<Digest>
|
|
sha512File(const std::filesystem::path& path);
|
|
|
|
inline constexpr std::size_t Ed25519PublicKeySize = 32;
|
|
inline constexpr std::size_t Ed25519SignatureSize = 64;
|
|
|
|
/// Verify an Ed25519 detached signature over the exact message bytes.
|
|
/// Invalid key/signature sizes are errors; a well-formed but invalid
|
|
/// signature returns false.
|
|
[[nodiscard]] Void::Result::Result<bool>
|
|
verifyEd25519(std::span<const std::byte> message,
|
|
std::span<const std::byte> signature,
|
|
std::span<const std::byte> publicKey);
|
|
} // namespace Void::Crypto
|