UTF-8 based SHA1 from FString

As there are many gotchas when it comes to encodings and I could not find any code that used the internal SHA1 implementation I would love if someone with some more experience could verify that I’m not being stupid as I’m not a cpp programmer at all. :stuck_out_tongue:

Also, if this is working as intended then I guess this place is as good as any to share that information. :slight_smile:

h file

static FString SHA1(const FString & Data);

cpp file

#include "SecureHash.h"
#include <string>

FString UVHServerCommunication::SHA1(const FString & Data)
{
    //Make sure we are using UTF-8 as the browser
    //and server will be using UTF-8
    std::string stdstring(TCHAR_TO_UTF8(*Data));

    FSHA1 HashState;
    HashState.Update((uint8*)stdstring.c_str(), stdstring.size());
    HashState.Final();

    uint8 Hash[FSHA1::DigestSize];
    HashState.GetHash(Hash);
    return BytesToHex(Hash, FSHA1::DigestSize);
}

The thing I am worried about is stdstring(TCHAR_TO_UTF8(*Data)), is there some strange chars that will break this? I have tried stuff with åäö and some other signs and it seems to work but I don’t want to figure out after a while that there is chars that gives the wrong hash.

I’m pretty sure something breaks if you do

FString str("åäö");
auto _utf8 = TCHAR_TO_UTF8(str);
str = UTF8_TO_TCHAR(_utf8);

I have no idea why though.

Strange, I think I tried that and verified it worked. I’ll have a look again when I get home from work and report back.

Alright, here’s the gist; if the string is longer than 128U characters it will break. Also, we did mess up with the conversion since we stored the converted UTF8 string with the size of char times the length of the string, which isn’t correct after conversion.