Arachne 1.0
Arachne - the perpetual stitcher of Wikidata entities.
Loading...
Searching...
No Matches
crypto.hpp
Go to the documentation of this file.
1#ifndef ARACHNE_CRYPTO_HPP
2#define ARACHNE_CRYPTO_HPP
3
4#include <array>
5#include <cstddef>
6#include <cstdint>
7#include <filesystem>
8#include <span>
9#include <string>
10#include <string_view>
11
12namespace arachne::crypto {
13
14using sha256_digest = std::array<std::byte, 32>;
15
16/** Incremental SHA-256 calculator. */
17class sha256_hasher final {
18public:
19 sha256_hasher() noexcept;
20
21 void reset() noexcept;
22 void update(std::span<const std::byte> bytes);
23 void update(std::string_view bytes);
24
25 /** Finalize the digest. Repeated calls return the same value. */
26 [[nodiscard]] sha256_digest finish();
27 [[nodiscard]] std::string finish_hex();
28
29private:
30 void transform(const std::byte* block) noexcept;
31
32 std::array<std::uint32_t, 8> state_ {};
33 std::array<std::byte, 64> buffer_ {};
35 std::size_t buffered_ = 0;
36 bool finished_ = false;
38};
39
40/** Return the lowercase hexadecimal SHA-256 digest of arbitrary bytes. */
41[[nodiscard]] std::string sha256(std::span<const std::byte> bytes);
42
43/** Return the lowercase hexadecimal SHA-256 digest of a byte string. */
44[[nodiscard]] std::string sha256(std::string_view bytes);
45
46/** Stream a file in binary mode and return its lowercase SHA-256 digest. */
47[[nodiscard]] std::string sha256_file(const std::filesystem::path& path);
48
49/** Compatibility spelling used by early Penelope code. */
50[[nodiscard]] inline std::string sha256_string(std::string_view bytes) {
51 return sha256(bytes);
52}
53
54/**
55 * Return true only for a portable, non-empty relative artifact reference.
56 * Backslashes, absolute paths, empty components, and dot components are
57 * rejected so a reference has the same meaning on POSIX and Windows.
58 */
59[[nodiscard]] bool
60is_safe_relative_artifact_ref(std::string_view artifact_ref) noexcept;
61
62/**
63 * Join a validated artifact reference to a root. This is a lexical safety
64 * primitive; callers that write files must additionally reject symlinked
65 * directory components.
66 */
67[[nodiscard]] std::filesystem::path safe_artifact_path(
68 const std::filesystem::path& root, std::string_view artifact_ref
69);
70
71}
72
73#endif // ARACHNE_CRYPTO_HPP
domain_lock(const std::filesystem::path &lock_root, std::string_view graph_domain, std::string_view run_id, std::chrono::seconds stale_after=std::chrono::hours(6))
domain_lock(const domain_lock &)=delete
domain_lock(domain_lock &&other) noexcept
domain_lock & operator=(const domain_lock &)=delete
domain_lock & operator=(domain_lock &&other) noexcept
void bind_product_run_inputs(std::string_view run_id, const std::vector< std::string > &envelope_ids)
operational_ledger & operator=(operational_ledger &&)=delete
void finish_integrated_product_run(std::string_view run_id, std::string_view manifest_ref)
operational_ledger(const operational_ledger &)=delete
envelope_record transition(std::string_view envelope_id, cocoon_status next, std::string_view actor_ref, std::string_view reason={})
operational_ledger(std::filesystem::path database_path, std::optional< std::filesystem::path > legacy_inbox_root=std::nullopt)
envelope_record get(std::string_view envelope_id) const
envelope_record intake(const intake_request &request)
void capture_inbox_baseline(const std::filesystem::path &inbox_root)
accumulation_state accumulation() const
bool retire_queued_payload(std::string_view envelope_id, const std::filesystem::path &internal_queue_root, std::optional< std::filesystem::path > legacy_inbox_root=std::nullopt)
void finish_run(std::string_view run_id, std::string_view status, std::string_view manifest_ref={})
std::optional< std::filesystem::path > legacy_inbox_root_
std::vector< envelope_record > product_run_inputs(std::string_view run_id) const
std::vector< envelope_record > list(cocoon_status status) const
std::vector< verification_issue > verify_inbox(const std::filesystem::path &inbox_root) const
bool should_integrate(const accumulation_policy &policy) const
std::vector< state_event > history(std::string_view envelope_id) const
operational_ledger & operator=(const operational_ledger &)=delete
bool claim_logical_run(std::string_view run_id, std::string_view graph_domain, std::string_view logical_date, std::string_view configuration_sha256, bool retry_failed=false, bool resume_running=false)
operational_ledger(operational_ledger &&)=delete
void update(std::span< const std::byte > bytes)
Definition crypto.cpp:76
void transform(const std::byte *block) noexcept
Definition crypto.cpp:153
std::array< std::byte, 64 > buffer_
Definition crypto.hpp:33
std::array< std::uint32_t, 8 > state_
Definition crypto.hpp:32
std::unique_ptr< sqlite3, decltype(&sqlite3_close)> sqlite_handle
std::unique_ptr< sqlite3_stmt, decltype(&sqlite3_finalize)> statement_handle
std::string_view to_string(const cocoon_status status) noexcept
cocoon_status cocoon_status_from_string(const std::string_view value)
bool can_transition(const cocoon_status from, const cocoon_status to) noexcept
std::string sha256(std::string_view bytes)
Definition crypto.cpp:215
bool is_safe_relative_artifact_ref(std::string_view artifact_ref) noexcept
Definition crypto.cpp:249
std::filesystem::path safe_artifact_path(const std::filesystem::path &root, std::string_view artifact_ref)
Definition crypto.cpp:302
std::array< std::byte, 32 > sha256_digest
Definition crypto.hpp:14
std::string sha256(std::span< const std::byte > bytes)
Definition crypto.cpp:209
std::string sha256_string(std::string_view bytes)
Definition crypto.hpp:50
std::string sha256_file(const std::filesystem::path &path)
Definition crypto.cpp:221
std::optional< std::string > supersedes
std::optional< std::string > accepted_by
std::optional< std::string > supersedes