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
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
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.
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
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
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:
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:
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.
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();
// 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;
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;
// 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:
/** 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: