diff options
| author | Stefan Boberg <[email protected]> | 2021-05-11 13:05:39 +0200 |
|---|---|---|
| committer | Stefan Boberg <[email protected]> | 2021-05-11 13:05:39 +0200 |
| commit | f8d9ac5d13dd37b8b57af0478e77ba1e75c813aa (patch) | |
| tree | 1daf7621e110d48acd5e12e3073ce48ef0dd11b2 /zencore/iohash.cpp | |
| download | zen-f8d9ac5d13dd37b8b57af0478e77ba1e75c813aa.tar.xz zen-f8d9ac5d13dd37b8b57af0478e77ba1e75c813aa.zip | |
Adding zenservice code
Diffstat (limited to 'zencore/iohash.cpp')
| -rw-r--r-- | zencore/iohash.cpp | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/zencore/iohash.cpp b/zencore/iohash.cpp new file mode 100644 index 000000000..afe2e54ba --- /dev/null +++ b/zencore/iohash.cpp @@ -0,0 +1,73 @@ +// Copyright Epic Games, Inc. All Rights Reserved. + +#include <zencore/iohash.h> + +#include <zencore/blake3.h> +#include <zencore/string.h> + +#include <doctest/doctest.h> +#include <gsl/gsl-lite.hpp> + +namespace zen { + +IoHash IoHash::Zero; // Initialized to all zeros + +IoHash +IoHash::HashMemory(const void* data, size_t byteCount) +{ + BLAKE3 b3 = BLAKE3::HashMemory(data, byteCount); + + IoHash io; + memcpy(io.Hash, b3.Hash, sizeof io.Hash); + + return io; +} + +IoHash +IoHash::FromHexString(const char* string) +{ + return FromHexString({string, sizeof(IoHash::Hash) * 2}); +} + +IoHash +IoHash::FromHexString(std::string_view string) +{ + ZEN_ASSERT(string.size() == 2 * sizeof(IoHash::Hash)); + + IoHash io; + + ParseHexBytes(string.data(), string.size(), io.Hash); + + return io; +} + +const char* +IoHash::ToHexString(char* outString /* 40 characters + NUL terminator */) const +{ + ToHexBytes(Hash, sizeof(IoHash), outString); + outString[2 * sizeof(IoHash)] = '\0'; + + return outString; +} + +StringBuilderBase& +IoHash::ToHexString(StringBuilderBase& outBuilder) const +{ + String_t Str; + ToHexString(Str); + + outBuilder.AppendRange(Str, &Str[StringLength]); + + return outBuilder; +} + +std::string +IoHash::ToHexString() const +{ + String_t Str; + ToHexString(Str); + + return Str; +} + +} // namespace zen |