SHA256 Hash FString

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.