载入中...
搜索中...
未找到
SnapshotHash.cpp
浏览该文件的文档.
1
7
8#include "common/Capability.h"
9#include "common/Diagnostic.h"
10
11#include <array>
12#include <cstdint>
13#include <utility>
14
15namespace eve {
16namespace {
17
27class BuiltinSnapshotContentHasher final : public ISnapshotContentHasher {
28public:
29 [[nodiscard]] std::string_view id() const noexcept override { return "fnv1a64x2-noncrypto"; }
30
31 [[nodiscard]] ContentDigestKind digestKind() const noexcept override {
33 }
34
35 [[nodiscard]] Result<ContentId> hash(std::string_view canonicalInput) const override {
36 std::uint64_t left = 14695981039346656037ull;
37 std::uint64_t right = 1099511628211ull;
38 for (const unsigned char byte : canonicalInput) {
39 left = (left ^ byte) * 1099511628211ull;
40 right = (right ^ (static_cast<std::uint64_t>(byte) + 0x9e3779b97f4a7c15ull)) * 14029467366897019727ull;
41 }
43 for (int index = 0; index < 8; ++index) {
44 bytes[static_cast<std::size_t>(index)] =
45 static_cast<std::uint8_t>(left >> static_cast<unsigned>(56 - index * 8));
46 bytes[static_cast<std::size_t>(index + 8)] =
47 static_cast<std::uint8_t>(right >> static_cast<unsigned>(56 - index * 8));
48 }
50 }
51};
52
54std::shared_ptr<ISnapshotContentHasher>& builtinHolder() {
55 static std::shared_ptr<ISnapshotContentHasher> value = std::make_shared<BuiltinSnapshotContentHasher>();
56 return value;
57}
58
60std::shared_ptr<ISnapshotContentHasher> activeHasher() {
61 if (ISnapshotContentHasher* registered = registeredSnapshotContentHasher(); registered != nullptr) {
62 // The provider callable only needs the object for the duration of the call; the
63 // registering module owns it, so a non-owning alias is deliberate here.
64 return std::shared_ptr<ISnapshotContentHasher>(registered, [](ISnapshotContentHasher*) {});
65 }
66 return builtinHolder();
67}
68
69} // namespace
70
71std::string_view contentDigestKindName(ContentDigestKind kind) noexcept {
72 switch (kind) {
73 case ContentDigestKind::NonCryptographic: return "non_cryptographic";
74 case ContentDigestKind::Cryptographic: return "cryptographic";
75 }
76 return "non_cryptographic";
77}
78
79std::shared_ptr<ISnapshotContentHasher> builtinSnapshotContentHasher() { return builtinHolder(); }
80
82 return cap::query<ISnapshotContentHasher>();
83}
84
86 if (registeredSnapshotContentHasher() != nullptr)
88 cap::provide<ISnapshotContentHasher>(builtinHolder().get());
90}
91
93 std::shared_ptr<ISnapshotContentHasher> hasher = activeHasher();
94 return [hasher](std::string_view canonicalInput) -> Result<ContentId> { return hasher->hash(canonicalInput); };
95}
96
97std::string_view activeSnapshotHashAlgorithm() noexcept {
99 return hasher != nullptr ? hasher->id() : builtinHolder()->id();
100}
101
102} // namespace eve
double value
Stable, structured diagnostics shared by engine modules.
std::array< std::uint8_t, 32 > hash
Definition Evpack.cpp:172
HexVec3 left
HexVec3 right
TokenKind kind
std::uint64_t bytes
std::string id
Definition PlayHost.cpp:108
Injectable content-digest provider for snapshots, with an honest default.
uint32_t index
One content-digest implementation usable for snapshot sealing and verification.
virtual std::string_view id() const noexcept=0
Stable algorithm id, persisted or logged next to the data it hashed.
Move-only operation result carrying either a value or Status.
Definition Result.h:155
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:164
static Status success(StatusCode code=StatusCode::Ok)
Construct a successful status with an explicit non-error outcome.
Definition Status.h:81
std::array< std::uint8_t, 16 > Bytes
Definition Identity.h:114
Build metadata (engine git commit, build time, third-party version).
Definition Build.cpp:16
std::string_view contentDigestKindName(ContentDigestKind kind) noexcept
Stable protocol spelling of a digest kind.
ContentDigestKind
What a digest implementation actually guarantees, stated rather than implied.
@ Cryptographic
A cryptographic digest; the provider documents its algorithm and properties.
@ NonCryptographic
Fast, deterministic, collision-resistant enough for storage identity only.
Result< void > registerBuiltinSnapshotHasher()
Register the built-in hasher, unless a module already registered one.
StrongUuid< detail::ContentIdTag > ContentId
Deterministic identity of equivalent canonical content.
Definition Identity.h:264
SnapshotHashProvider snapshotContentHashProvider()
A SnapshotHashProvider backed by the active hasher.
std::shared_ptr< ISnapshotContentHasher > builtinSnapshotContentHasher()
The engine's built-in content hasher.
std::string_view activeSnapshotHashAlgorithm() noexcept
Id of the algorithm snapshotContentHashProvider currently uses.
ISnapshotContentHasher * registeredSnapshotContentHasher() noexcept
The currently registered hasher, if a module registered one.
std::function< Result< ContentId >(std::string_view canonicalInput)> SnapshotHashProvider
Injected content-digest implementation used by snapshots.
Definition Snapshot.h:36