Table of Contents

Class CodeHashGenerator

Namespace
CodeStage.AntiCheat.Genuine.CodeHash
Assembly
Build.dll

Generates current application runtime code hash to let you validate it against previously generated runtime code hash to detect external code manipulations.

[AddComponentMenu("")]
[DisallowMultipleComponent]
public class CodeHashGenerator : KeepAliveBehaviour<CodeHashGenerator>, ICodeHashGenerator
Inheritance
object
Object
Component
Behaviour
MonoBehaviour
CodeHashGenerator
Implements
Inherited Members

Remarks

Calculation is done on the separate threads where possible to prevent noticeable CPU spikes and performance impact.
Supported platforms: Windows PC, Android (more to come)
Resulting hash in most cases should match value you get from the CodeHashGeneratorPostprocessor.

// Basic usage with event subscription
if (!CodeHashGenerator.IsTargetPlatformCompatible())
    return;

if (CodeHashGenerator.Instance.IsBusy)
    return;

CodeHashGenerator.HashGenerated += OnHashGenerated;
CodeHashGenerator.Generate();

private void OnHashGenerated(HashGeneratorResult result)
{
    CodeHashGenerator.HashGenerated -= OnHashGenerated;

    if (result.Success)
    {
        Debug.Log($"Summary Hash: {result.SummaryHash}");
        Debug.Log($"Files hashed: {result.FileHashes.Count}");
    }
    else
    {
        Debug.LogError($"Hash generation failed: {result.ErrorMessage}");
    }
}
// Async usage (recommended)
private async void GenerateHashAsync()
{
    if (!CodeHashGenerator.IsTargetPlatformCompatible())
        return;

    if (CodeHashGenerator.Instance.IsBusy)
        return;

    try
    {
        var result = await CodeHashGenerator.GenerateAsync();

        if (result.Success)
        {
            Debug.Log($"Summary Hash: {result.SummaryHash}");
            Debug.Log($"Files hashed: {result.FileHashes.Count}");

            // Upload hashes to server for validation
            // UploadHashes(result.SummaryHash, result.FileHashes);
        }
        else
        {
            Debug.LogError($"Hash generation failed: {result.ErrorMessage}");
        }
    }
    catch (Exception ex)
    {
        Debug.LogError($"Exception during hash generation: {ex.Message}");
    }
}

Properties

IsBusy

Indicates if hash generation is currently in process.

public bool IsBusy { get; }

Property Value

bool

LastResult

Stores previously calculated result. Can be null if Generate() wasn't called yet or if it was called but calculation is still in process.

public HashGeneratorResult LastResult { get; }

Property Value

HashGeneratorResult
See Also

Methods

AddToSceneOrGetExisting()

Creates new instance of the CodeHashGenerator at scene if it doesn't exists. Make sure to call NOT from Awake phase.

public static ICodeHashGenerator AddToSceneOrGetExisting()

Returns

ICodeHashGenerator

New or existing instance of the detector.

Generate(int)

Call to start current runtime code hash generation. Automatically adds instance to the scene if necessary.

public static ICodeHashGenerator Generate(int maxThreads = 1)

Parameters

maxThreads int

Threads to use while hashing the files.

Returns

ICodeHashGenerator

GenerateAsync(int)

Awaitable version of Generate(). Allows awaiting for the generation result.

public static Task<HashGeneratorResult> GenerateAsync(int maxThreads = 1)

Parameters

maxThreads int

Threads to use while hashing the files.

Returns

Task<HashGeneratorResult>

IsTargetPlatformCompatible()

Call to make sure current platform is compatible before calling Generate().

public static bool IsTargetPlatformCompatible()

Returns

bool

True if current platform is supported by the CodeHashGenerator, otherwise returns false. Can return true in Editor but Hash Generation in Editor is not possible (nothing to hash).

Events

HashGenerated

Subscribe to get resulting hash right after it gets calculated.

public static event HashGeneratorResultHandler HashGenerated

Event Type

HashGeneratorResultHandler