UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
Blake3.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
8#include "HAL/Platform.h"
10#include "HAL/UnrealMemory.h"
11#include "Memory/MemoryFwd.h"
12#include "Memory/MemoryView.h"
15#include "String/BytesToHex.h"
16#include "String/HexToBytes.h"
18
19class FArchive;
21template <typename CharType> class TStringBuilderBase;
22
24
27{
28public:
29 using ByteArray = uint8[32];
30
32 FBlake3Hash() = default;
33
35 inline explicit FBlake3Hash(const ByteArray& Hash);
36
38 inline explicit FBlake3Hash(FAnsiStringView HexHash);
39 inline explicit FBlake3Hash(FWideStringView HexHash);
40 inline explicit FBlake3Hash(FUtf8StringView HexHash);
41
43 inline static FBlake3Hash FromView(FMemoryView Hash);
44
46 inline void Reset() { *this = FBlake3Hash(); }
47
49 inline bool IsZero() const;
50
52 inline ByteArray& GetBytes() { return Hash; }
53 inline const ByteArray& GetBytes() const { return Hash; }
54
56 static const FBlake3Hash Zero;
57
58 inline bool operator==(const FBlake3Hash& B) const
59 {
60 return FMemory::Memcmp(GetBytes(), B.GetBytes(), sizeof(decltype(GetBytes()))) == 0;
61 }
62
63 inline bool operator!=(const FBlake3Hash& B) const
64 {
65 return FMemory::Memcmp(GetBytes(), B.GetBytes(), sizeof(decltype(GetBytes()))) != 0;
66 }
67
68 inline bool operator<(const FBlake3Hash& B) const
69 {
70 return FMemory::Memcmp(GetBytes(), B.GetBytes(), sizeof(decltype(GetBytes()))) < 0;
71 }
72
74 {
75 Ar.Serialize(Value.GetBytes(), sizeof(decltype(Value.GetBytes())));
76 return Ar;
77 }
78
79 friend inline uint32 GetTypeHash(const FBlake3Hash& Value)
80 {
81 return *reinterpret_cast<const uint32*>(Value.GetBytes());
82 }
83
84private:
85 alignas(uint32) ByteArray Hash{};
86};
87
89
91
94{
95public:
96 inline FBlake3() { Reset(); }
97
98 FBlake3(const FBlake3&) = delete;
99 FBlake3& operator=(const FBlake3&) = delete;
100
102 CORE_API void Reset();
103
105 CORE_API void Update(FMemoryView View);
106 CORE_API void Update(const void* Data, uint64 Size);
108
115
118 [[nodiscard]] CORE_API static FBlake3Hash HashBuffer(const void* Data, uint64 Size);
120
121private:
122 TAlignedBytes<1912, 8> HasherBytes;
123};
124
126
128{
129 FMemory::Memcpy(Hash, InHash, sizeof(ByteArray));
130}
131
133{
134 check(HexHash.Len() == sizeof(ByteArray) * 2);
136}
137
139{
140 check(HexHash.Len() == sizeof(ByteArray) * 2);
142}
143
145{
146 check(HexHash.Len() == sizeof(ByteArray) * 2);
148}
149
151{
152 checkf(InHash.GetSize() == sizeof(ByteArray),
153 TEXT("FBlake3Hash cannot be constructed from a view of %" UINT64_FMT " bytes."), InHash.GetSize());
155 FMemory::Memcpy(NewHash.Hash, InHash.GetData(), sizeof(ByteArray));
156 return NewHash;
157}
158
159inline bool FBlake3Hash::IsZero() const
160{
161 using UInt32Array = uint32[8];
162 static_assert(sizeof(UInt32Array) == sizeof(ByteArray), "Invalid size for UInt32Array");
163 for (uint32 Value : reinterpret_cast<const UInt32Array&>(Hash))
164 {
165 if (Value != 0)
166 {
167 return false;
168 }
169 }
170 return true;
171}
172
173template <typename CharType>
175{
176 UE::String::BytesToHexLower(Hash.GetBytes(), Builder);
177 return Builder;
178}
179
182{
184}
185
187[[nodiscard]] CORE_API FString LexToString(const FBlake3Hash& Hash);
#define UINT64_FMT
Definition AndroidPlatformString.h:66
#define check(expr)
Definition AssertionMacros.h:314
#define checkf(expr, format,...)
Definition AssertionMacros.h:315
CORE_API FString LexToString(const FBlake3Hash &Hash)
Definition Blake3.cpp:101
TStringBuilderBase< CharType > & operator<<(TStringBuilderBase< CharType > &Builder, const FBlake3Hash &Hash)
Definition Blake3.h:174
void LexFromString(FBlake3Hash &OutHash, const TCHAR *Buffer)
Definition Blake3.h:181
#define TEXT(x)
Definition Platform.h:1272
FPlatformTypes::TCHAR TCHAR
Either ANSICHAR or WIDECHAR, depending on whether the platform supports wide characters or the requir...
Definition Platform.h:1135
FPlatformTypes::uint64 uint64
A 64-bit unsigned integer.
Definition Platform.h:1117
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
uint32 Size
Definition VulkanMemory.cpp:4034
uint8_t uint8
Definition binka_ue_file_header.h:8
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition Archive.h:1208
virtual void Serialize(void *V, int64 Length)
Definition Archive.h:1689
Definition Blake3.h:94
CORE_API FBlake3Hash Finalize() const
Definition Blake3.cpp:65
static CORE_API FBlake3Hash HashBuffer(FMemoryView View)
Definition Blake3.cpp:78
FBlake3()
Definition Blake3.h:96
FBlake3(const FBlake3 &)=delete
CORE_API void Reset()
Definition Blake3.cpp:18
FBlake3 & operator=(const FBlake3 &)=delete
Definition CompositeBuffer.h:27
Definition StringBuilder.h:79
void BytesToHexLower(TConstArrayView< uint8 > Bytes, ANSICHAR *OutHex)
Definition BytesToHex.cpp:42
int32 HexToBytes(FWideStringView Hex, uint8 *OutBytes)
Definition HexToBytes.cpp:30
Definition Blake3.h:27
bool IsZero() const
Definition Blake3.h:159
const ByteArray & GetBytes() const
Definition Blake3.h:53
static const FBlake3Hash Zero
Definition Blake3.h:56
bool operator!=(const FBlake3Hash &B) const
Definition Blake3.h:63
friend uint32 GetTypeHash(const FBlake3Hash &Value)
Definition Blake3.h:79
ByteArray & GetBytes()
Definition Blake3.h:52
uint8[32] ByteArray
Definition Blake3.h:29
friend FArchive & operator<<(FArchive &Ar, FBlake3Hash &Value)
Definition Blake3.h:73
FBlake3Hash()=default
static FBlake3Hash FromView(FMemoryView Hash)
Definition Blake3.h:150
void Reset()
Definition Blake3.h:46
bool operator<(const FBlake3Hash &B) const
Definition Blake3.h:68
bool operator==(const FBlake3Hash &B) const
Definition Blake3.h:58
static UE_FORCEINLINE_HINT int32 Memcmp(const void *Buf1, const void *Buf2, SIZE_T Count)
Definition UnrealMemory.h:114
static UE_FORCEINLINE_HINT void * Memcpy(void *Dest, const void *Src, SIZE_T Count)
Definition UnrealMemory.h:160
Definition TypeCompatibleBytes.h:17