Unreal Engine Random Id Generator

03/02/2017 22:32 #Metho#1
Wer mal einen Session Id Generator für die Unreal Engine möchte oder nur einen Random Wert :)

SessionIdGenerator.cpp
PHP Code:
/**

    Generates a session id.

  [MENTION=1332190]author[/MENTION] Metho
  [MENTION=1805674]Version[/MENTION] 1.0 10/02/17

*/

// Session->SessionId
unsigned int Session::SessionId = 0;

// Session->GetSessionId()
unsigned int Session::GetSessionId()
{
    return 
this->SessionId;
}

// Session->IncreaseSessionId()
void Session::IncreaseSessionId() {
    
this->SessionId++;
}

// Session->GetProcessId()
uint32 Session::GetProcessId()
{
    return 
FPlatformProcess::GetCurrentProcessId();
}

// Session->UpdateTime()
void Session::UpdateTime()
{
    
this->Time = FDateTime::UtcNow();
    
this->Seconds = Time.ToUnixTimestamp();
    
this->Milliseconds = Time.ToUnixTimestamp() * 1000 + Time.GetMillisecond();
    
this->UpdateLinearCongruential();
}

// Session->GetSeconds()
unsigned long long Session::GetSeconds()
{
    return 
this->Seconds;
}

// Session->GetMilliseconds()
unsigned long long Session::GetMilliseconds()
{
    return 
this->Milliseconds;
}

// Session->UpdateLinearCongruential()
void Session::UpdateLinearCongruential()
{
#define MODMULT(a, b, c, m, s) x = s / a; s = b * (s - a * x) - c * x; if (s < 0) s += m
#define A1 53668
#define A2 52774
#define B1 40014
#define B2 40692
#define C1 12211
#define C2 3791
#define M1 2147483563L
#define M2 2147483399L
#define SH 2147483562
#define FA 4.656613e-10

    
auto x = this->GetSeconds() ^ (this->GetMilliseconds() << 11);
    
auto y = this->GetProcessId() ^ (this->GetMilliseconds() << 11);

    
MODMULT(A1, B1, C1, M1, x);
    
MODMULT(A2, B2, C2, M2, y);

    
auto z = x - y;

    if (
z < 1)
    {
        
z += SH;
    }

    
this->LinearCongruential = z;

#undef MODMULT
#undef A1
#undef A2
#undef B1
#undef B2
#undef C1
#undef C2
#undef M1
#undef M2
#undef SH
#undef FA
}

// Session->GetLinearCongruential()
long long Session::GetLinearCongruential()
{
    return 
this->LinearCongruential;
}

// Session->GenerateId()
FString Session::GenerateId()
{
    
this->IncreaseSessionId();
    
this->UpdateTime();
    
FMD5 MD5;

    
// Phase 1 --[[
    
uint8 SystemSeed[2048];
    
FString SystemString = FString::Printf(TEXT("%.15s%ld%ld%0.8F"),
        
this->GetSessionId(),
        
this->GetSeconds(),
        
this->GetMilliseconds(),
        
this->GetLinearCongruential()
    );
    
FString::ToBlob(SystemString, SystemSeed, sizeof(SystemSeed));
    
MD5.Update(SystemSeed, sizeof(SystemSeed));
    
// ]]-- Phase 1

    // Phase 2 --[[
#define RandomSeedLength 2048
    
uint8 RandomSeed[RandomSeedLength];
#define RandomSeedSize sizeof(RandomSeed)
    
FString RandomString = FString::FromInt(FMath::RandRange(10 ^ (RandomSeedLength - 1), 10 ^ RandomSeedLength - 1));
    
FString::ToBlob(RandomString, RandomSeed, RandomSeedSize);
    
MD5.Update(RandomSeed, RandomSeedSize);
#undef RandomSeedLength
#undef RandomSeedSize
    // ]]-- Phase 2

    // Phase 3 --[[
#define DigestLength 32
    
uint8 Digest[DigestLength];
    
MD5.Final(Digest);

    
FString HashString;

    for (
unsigned short i = 0; i < DigestLength; i++)
    {
        
HashString += FString::Printf(TEXT("%02x"), Digest[i]);
    }
#undef DigestLength
    // ]]-- Phase 3

    // UE_LOG(LogTemp, Log, TEXT("Session was generated. Session id: %d, Session string: %s"), this->GetSessionId(), HashString);

    
return HashString;
} 
SessionIdGenerator.h

PHP Code:
/**

Generates a session id.

  [MENTION=1332190]author[/MENTION] Metho
  [MENTION=1805674]Version[/MENTION] 1.0 10/02/17

*/

// Session
class Session {

public:

    
// *->GenerateId()
    
FString GenerateId();

    
// *->GetSessionId()
    
unsigned int GetSessionId();

    
// *->GetProcessId()
    
uint32 GetProcessId();

    
// *->GetSeconds()
    
unsigned long long GetSeconds();

    
// *->GetMilliseconds()
    
unsigned long long GetMilliseconds();

    
// *->GetLinearCongruential()
    
long long GetLinearCongruential();

private:

    
// *->SessionId
    
static unsigned int SessionId;

    
// *->IncreaseSessionId
    
void IncreaseSessionId();

    
// *->Time
    
FDateTime Time;

    
// *->UpdateTime()
    
void UpdateTime();

    
// *->Seconds
    
unsigned long long Seconds;

    
// *->Milliseconds
    
unsigned long long Milliseconds;

    
// *->UpdateLinearCongruential()
    
void UpdateLinearCongruential();

    
// *->LinearCongruential
    
long long LinearCongruential;

}; 
03/09/2017 02:59 atom0s#2
MD5 and SHA1 are both known for having hash collisions. You may want to look into using a different hashing algo to avoid such issues.