# SHA256 Hash FString

**URL:** <https://forums.unrealengine.com/t/sha256-hash-fstring/426945>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [June 8, 2018, 12:45pm UTC](https://forums.unrealengine.com/t/sha256-hash-fstring/426945 "2018-06-08T12:45:38Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![tehhax](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@tehhax](https://forums.unrealengine.com/u/tehhax)\
**Post date:** [June 8, 2018, 12:45pm UTC](https://forums.unrealengine.com/t/sha256-hash-fstring/426945/1 "2018-06-08T12:45:38Z")

</div>

I simply want to SHA256 an FString to send to my server. I spent the last month creating an account server that authenticates using SHA256 strings after hearing about the flaws with SHA1. I have no problems generating SHA1 with UE4 but it seems that the 256 version is implemented differently?

Everything compiles fine but crashes during runtime

**Here is the function:**

```
FString UHashFunctionLibrary::SHA256_ENCODE(const FString& inString) {
	uint32 size = inString.Len();
	
	TArray<uint8> sData;
	sData.AddUninitialized(size);
	StringToBytes(inString, sData.GetData(), size);
	
	FSHA256Signature retSig;
	FGenericPlatformMisc::GetSHA256Signature(sData.GetData(), size, retSig);
	return retSig.ToString();
}

```

**And the crash reports:**  
Assertion failed: false [File:D:\Build++UE4+Release-  
4.18+Compile\Sync\Engine\Source\Runtime\Core\Private\GenericPlatform\GenericPlatformMisc.cpp] [Line: 899] **No SHA256 Platform implementation**

**The problem line:**

```
FGenericPlatformMisc::GetSHA256Signature(sData.GetData(), size, retSig);

```

I haven’t seen any questions asked on the forum or hub about this. I see there is a plugin or two available but I would strongly prefer not to use them. I am developing a multi-platform game and would like to use unreal’s built-in hashing functions that are cross-platform compatible. I know UE4 uses SHA256 internally and there is clearly some implementation. I just don’t really know what to make of that error.

Thank you so much in advance! This is a serious roadblock for me and while I could switch everything to SHA1 authentication, that would be taking a step back security-wise.

---

<div class="post-metadata">

**Author:** ![tehhax](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@tehhax](https://forums.unrealengine.com/u/tehhax)\
**Post date:** [June 9, 2018, 1:39am UTC](https://forums.unrealengine.com/t/sha256-hash-fstring/426945/2 "2018-06-09T01:39:00Z")

</div>

So I took a look at the source of GetSHA256Signature() and it looks like its not implemented? The first checkf is hardcoded with false?

```
bool FGenericPlatformMisc::GetSHA256Signature(const void* Data, uint32 ByteSize, FSHA256Signature& OutSignature)
{
	checkf(false, TEXT("No SHA256 Platform implementation"));
	FMemory::Memzero(OutSignature.Signature);
	return false;
}

```

Can UE4 create a SHA256 hash without a third party library?

---

<div class="post-metadata">

**Author:** ![Shadowriver](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Shadowriver](https://forums.unrealengine.com/u/Shadowriver)\
**Post date:** [June 9, 2018, 2:38am UTC](https://forums.unrealengine.com/t/sha256-hash-fstring/426945/3 "2018-06-09T02:38:28Z")

</div>

Use FPlatfromMisc instead of FGenericPlatformMisc. “Generic” is fall back implementation, some of them simply crashes if something is not implemented in specific platform implementation and you hit one of such examples

What UE4 platform API work is that each platform have it own of set classes, so for example Windows have prefix FWindowsPlatform\* and Android for example will have FAndroidPlatform\* and during compilation the specific ser for platform that compilation happens for gets typename-ed to FPlatform, so always use FPlatform. Because this is kind of a C++ hack API reference only shows Generic platform implementation which is fallback implementation that all platform implementation inherent form and if there missing function it falls back to Generic. Some platform implementations also have some extra exclusive functions, this is only case where you not use FPlatfrom and reference platform implementation directly

---

<div class="post-metadata">

**Author:** ![tehhax](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@tehhax](https://forums.unrealengine.com/u/tehhax)\
**Post date:** [June 9, 2018, 6:51am UTC](https://forums.unrealengine.com/t/sha256-hash-fstring/426945/4 "2018-06-09T06:51:22Z")

</div>

Thanks for the reply! Unfortunately changing “FGenericPlatformMisc” to “FPlatformMisc” still produces the same error. It seems to be calling the generic one anyways ☹

```
Assertion failed: false [File:D:\Build\++UE4+Release-4.18+Compile\Sync\Engine\Source\Runtime\Core\Private\GenericPlatform\GenericPlatformMisc.cpp] [Line: 899] No SHA256 Platform implementation
```

---

<div class="post-metadata">

**Author:** ![Shadowriver](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Shadowriver](https://forums.unrealengine.com/u/Shadowriver)\
**Post date:** [June 23, 2018, 10:26pm UTC](https://forums.unrealengine.com/t/sha256-hash-fstring/426945/5 "2018-06-23T22:26:13Z")

</div>

You right there no implmentation on any platfrom and this function in engine is only used by HTTP module. I can only find MD5 and SHA1 implmentations in engine:

[https://github.com/EpicGames/UnrealEngine/blob/8e4560b8c22b309e73ff0ce90377742c3dfe13cc/Engine/Source/Runtime/Core/Public/Misc/SecureHash.h](https://github.com/EpicGames/UnrealEngine/blob/8e4560b8c22b309e73ff0ce90377742c3dfe13cc/Engine/Source/Runtime/Core/Public/Misc/SecureHash.h)

Hmmm you could try calling OpenSSL direcly for SHA256 hash, OpenSSL is included in UE4, you just need to ThirdParty depency in your module build script, here example:

[https://github.com/EpicGames/UnrealEngine/blob/8e4560b8c22b309e73ff0ce90377742c3dfe13cc/Engine/Plugins/Developer/PerforceSourceControl/Source/PerforceSourceControl/PerforceSourceControl.Build.cs](https://github.com/EpicGames/UnrealEngine/blob/8e4560b8c22b309e73ff0ce90377742c3dfe13cc/Engine/Plugins/Developer/PerforceSourceControl/Source/PerforceSourceControl/PerforceSourceControl.Build.cs)

---

<div class="post-metadata">

**Author:** ![shadow\_tj](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/shadow_tj/32/751579_2.png) [@shadow\_tj](https://forums.unrealengine.com/u/shadow_tj)\
**Post date:** [January 18, 2019, 6:50pm UTC](https://forums.unrealengine.com/t/sha256-hash-fstring/426945/6 "2019-01-18T18:50:41Z")

</div>

Got it working… prehaps its not best solution but its working for me.

I using the sha256.h and sha256.cpp file from zedwood function. found here  
[http://www.zedwood.com/article/cpp-sha256-function](http://www.zedwood.com/article/cpp-sha256-function)

Jah need to insert the #define \_CRT\_SECURE\_NO\_WARNINGS in the sha256.cpp

Because the function expects std.string and not fstring … jah need to convert them like this  
FString hash\_user\_name = \*FString(sha256(TCHAR\_TO\_UTF8(\*User\_Name)).c\_str());

Then jah can get the code working and jah got a nice sha256 hash signature.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_e7dc37e2](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_e7dc37e2](https://forums.unrealengine.com/u/anonymous_user_e7dc37e2)\
**Post date:** [June 2, 2022, 9:03am UTC](https://forums.unrealengine.com/t/sha256-hash-fstring/426945/7 "2022-06-02T09:03:15Z")

</div>

Sorry I am new to CPP, may I know how can you include the OpenSSL and example usage of it? Thank you!

---

<div class="post-metadata">

**Author:** ![DarthKronos](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/darthkronos/32/484150_2.png) [@DarthKronos](https://forums.unrealengine.com/u/DarthKronos)\
**Post date:** [July 3, 2023, 11:34am UTC](https://forums.unrealengine.com/t/sha256-hash-fstring/426945/8 "2023-07-03T11:34:31Z")

</div>

I do not know if anyone else is looking for a solution to the problem or maybe just someone will need the ability to transfer Fstring to SHA256. Then here is an example of the code that I found and adapted for UE5. You can call the Get\_Hash function in the Bluеprint.

> Block.h
> 
> #include “CoreMinimal.h”  
> #include “GameFramework/Actor.h”  
> #include “C\_Block.generated.h”
> 
> UCLASS()  
> class BLOCKCHAINE\_API AC\_Block : public AActor  
> {  
> GENERATED\_BODY()
> 
> // Functions
> 
> public:   
> // Sets default values for this actor’s properties  
> AC\_Block();
> 
> UFUNCTION(BlueprintCallable)  
> FString Get\_Hash(FString Get\_Word);
> 
> protected:
> 
> // Called when the game starts or when spawned  
> virtual void BeginPlay() override;
> 
> private:
> 
> std::string Calculate\_Hash(const std::string str) const;
> 
> public:
> 
> // Called every frame  
> virtual void Tick(float DeltaTime) override;  
> };

And

> Block.cpp
> 
> #include “C\_Block.h”
> 
> #include \<iomanip  
> #include \<sstream  
> #include \<openssl/sha.h\>
> 
> // Sets default values  
> AC\_Block::AC\_Block()  
> {  
> // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don’t need it.  
> PrimaryActorTick.bCanEverTick = true;
> 
> }
> 
> FString AC\_Block::Get\_Hash(FString Get\_Word)  
> {
> 
> std::string TestString = Calculate\_Hash(std::string(TCHAR\_TO\_UTF8(\*Get\_Word)));  
> FString HappyString(TestString.c\_str());  
> return HappyString;  
> }
> 
> // Called when the game starts or when spawned  
> void AC\_Block::BeginPlay()  
> {  
> Super::BeginPlay();
> 
> }
> 
> std::string AC\_Block::Calculate\_Hash(const std::string str) const  
> {  
> unsigned char hash[SHA256\_DIGEST\_LENGTH];  
> SHA256\_CTX sha256;  
> SHA256\_Init(&sha256);  
> SHA256\_Update(&sha256, str.c\_str(), str.size());  
> SHA256\_Final(hash, &sha256);
> 
> std::stringstream ss;
> 
> for (int i = 0; i \< SHA256\_DIGEST\_LENGTH; i++) {  
> ss \<\< std::hex \<\< std::setw(2) \<\< std::setfill(‘0’) \<\< static\_cast(hash[i]);  
> }
> 
> return ss.str();  
> }
> 
> // Called every frame  
> void AC\_Block::Tick(float DeltaTime)  
> {  
> Super::Tick(DeltaTime);
> 
> }

Too

> YourProject.Build.cs
> 
> using UnrealBuildTool;
> 
> public class Blockchain : ModuleRules  
> {  
> public Blockchain(ReadOnlyTargetRules Target) : base(Target)  
> {  
> PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
> 
> ```
> PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "OpenSSL" });
> 
> PrivateDependencyModuleNames.AddRange(new string[] { "OpenSSL" });
> 
> AddEngineThirdPartyPrivateStaticDependencies(Target, "OpenSSL");
> 
> ```
> 
> }  
> }

---

<div class="post-metadata">

**Author:** ![fweiseNV](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@fweiseNV](https://forums.unrealengine.com/u/fweiseNV)\
**Post date:** [August 30, 2024, 4:03pm UTC](https://forums.unrealengine.com/t/sha256-hash-fstring/426945/9 "2024-08-30T16:03:43Z")

</div>

Hi,

Using the OpenSSL SHA256 implementation a good practice to hash for SHA256. The below is a cleaned up version from the latest post:

> FSHA256Signature UYourClass::CalculateSHA256Hash(const TArray& Data)  
> {  
> FSHA256Signature Hash;
> 
> // Use OpenSSL to compute the SHA-256 hash  
> SHA256\_CTX Sha256Context;  
> SHA256\_Init(&Sha256Context);  
> SHA256\_Update(&Sha256Context, Data.GetData(), Data.Num());  
> SHA256\_Final(Hash.Signature, &Sha256Context);
> 
> return Hash;  
> }

When hashing for SHA512 the GenericPlatformMisc.cpp looks quite dated b/c it does not hold a definition of `FSHA512Signature`. Though this can easily be added by looking at the definition of `FSHA256Signature` like this:

> struct FSHA512Signature  
> {  
> uint8 Signature[64]; // SHA-512 generates a 64-byte hash
> 
> /\*\* Generates a hex string of the signature \*/  
> FString ToString() const  
> {  
> FString Result;  
> for (int32 i = 0; i \< 64; i++)  
> {  
> Result += FString::Printf(TEXT(“%02x”), Signature[i]);  
> }  
> return Result;  
> }  
> };

The `sha.h` in OpenSSL delivers the corresponding functionality for SHA512, so one can use the folllowing to then also hash for SHA512:

> FSHA512Signature UYourClass::CalculateSHA512Hash(const TArray& Data)  
> {  
> FSHA512Signature Hash;
> 
> // Use OpenSSL to compute the SHA-512 hash  
> SHA512\_CTX Sha512Context;  
> SHA512\_Init(&Sha512Context);  
> SHA512\_Update(&Sha512Context, Data.GetData(), Data.Num());  
> SHA512\_Final(Hash.Signature, &Sha512Context);
> 
> return Hash;  
> }

Hope that helps others trying to generate a stable hash or checksum for their projects.
