Clavem

Verify an Ed25519 license token offline in Unity

Unity's default .NET Standard 2.1 API has no Ed25519 verifier, so add the managed BouncyCastle DLL and call Ed25519Signer over the text "sgt1.<payload>". Then check v, pid, valid, exp and refresh_by on the parsed claims.

Adding Ed25519 to a Unity project

Unity’s API compatibility level defaults to .NET Standard 2.1. That surface has RSA and ECDSA but no Ed25519, so the signature check needs a library. The pure managed one is BouncyCastle for C#:

  1. Download BouncyCastle.Cryptography from NuGet. Its .nupkg is a zip file.
  2. Copy lib/netstandard2.0/BouncyCastle.Cryptography.dll into Assets/Plugins/. The .NET Standard 2.0 build lists no dependencies, so that one DLL is all you need.
  3. Build for a target with IL2CPP and test the check there. If managed code stripping removes something the editor kept, add a link.xml that preserves the assembly. Unity’s manual describes the format.

A native libsodium plug-in also works, but it needs a binary for every platform you ship, which a managed DLL avoids.

The verifier

using System;
using System.Text;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Crypto.Signers;
using UnityEngine;

[Serializable]
public class Claims
{
    public int v;
    public long lid, pid;
    public string kh, status, iid, fp, email;
    public bool valid;
    public long iat, exp, refresh_by; // exp is 0 when the license never expires
}

public static class OfflineToken
{
    const string PublicKey = "YOUR_PUBLIC_KEY"; // from your product page: 32 raw bytes, base64url
    const long ProductId = 75;                  // from your product page
    const long ClockSlack = 600;                // seconds of clock error to forgive

    static byte[] B64Url(string s)
    {
        s = s.Replace('-', '+').Replace('_', '/');
        s = s.PadRight(s.Length + (4 - s.Length % 4) % 4, '='); // the token has no padding
        return Convert.FromBase64String(s);
    }

    /// Claims when the signature is good, otherwise null.
    public static Claims Verify(string token, string publicKey = PublicKey)
    {
        try
        {
            var parts = token.Split('.');
            if (parts.Length != 3 || parts[0] != "sgt1") return null;
            byte[] key = B64Url(publicKey), sig = B64Url(parts[2]), body = B64Url(parts[1]);
            if (key.Length != 32 || sig.Length != 64) return null;

            var signer = new Ed25519Signer();
            signer.Init(false, new Ed25519PublicKeyParameters(key, 0));
            byte[] signed = Encoding.ASCII.GetBytes(parts[0] + "." + parts[1]); // the text as received
            signer.BlockUpdate(signed, 0, signed.Length);
            if (!signer.VerifySignature(sig)) return null;

            return JsonUtility.FromJson<Claims>(Encoding.UTF8.GetString(body));
        }
        catch (Exception) { return null; } // bad base64, bad JSON, bad key
    }

    public static bool IsLicensed(Claims c, long now)
    {
        return c != null && c.v == 1 && c.pid == ProductId && c.valid
            && (c.exp == 0 || c.exp > now) && c.refresh_by > now;
    }

    /// What LicenseChecker calls when the server gave no answer.
    public static bool IsFresh(SavedLicense saved, string publicKey = PublicKey)
    {
        var c = Verify(saved.token, publicKey);
        long now = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
        if (c == null || now + ClockSlack < saved.highIat) return false; // clock behind a time the server stamped
        if (c.iat > saved.highIat) { saved.highIat = c.iat; saved.Save(); }
        return IsLicensed(c, now);
    }
}

Verify returns null on every failure. IsLicensed is the five checks from the reference, and IsFresh adds the clock guard. A token for another product fails its own signature check against your key, but test pid too.

Traps that are specific to Unity

  • exp is 0 when absent. JsonUtility cannot tell a missing field from zero, and a perpetual license has no exp. A real expiry is never 0, so the sample treats 0 as no expiry.
  • Use the wall clock. Time.time and Time.realtimeSinceStartup count from launch. Use DateTimeOffset.UtcNow. highIat stored in the saved file is your high-water mark: it catches a clock moved behind the last token’s iat, and a player who deletes the file resets it.
  • Sign over the text, not the JSON. The signed bytes are sgt1. plus the payload exactly as received. Decoding and re-encoding the payload would change the bytes.
  • Pass an offset. new Ed25519PublicKeyParameters(key, 0) works on both 1.9 and 2.x of the library.
  • The key lives in your assembly. A Mono build can be decompiled, and IL2CPP raises the effort without making it impossible. Do not build a larger scheme on the idea that the public key cannot be replaced.

Test without Clavem’s key

An EditMode test can sign its own tokens. Generate a throwaway key, sign sgt1.<payload>, and pass the public key into Verify:

static string Enc(byte[] b) => Convert.ToBase64String(b).TrimEnd('=').Replace('+', '-').Replace('/', '_');

static string Mint(Ed25519PrivateKeyParameters priv, string json)
{
    string head = "sgt1." + Enc(Encoding.UTF8.GetBytes(json));
    var signer = new Ed25519Signer();
    signer.Init(true, priv);
    byte[] text = Encoding.ASCII.GetBytes(head);
    signer.BlockUpdate(text, 0, text.Length);
    return head + "." + Enc(signer.GenerateSignature());
}
// var priv = new Ed25519PrivateKeyParameters(new Org.BouncyCastle.Security.SecureRandom());
// string pub = Enc(priv.GeneratePublicKey().GetEncoded());

Mint good claims, a payload edited after signing, a token signed by a second key, one with refresh_by a day ago, valid: false, another pid, v: 2 and an exp in the past. Only the first should pass. I ran those eight cases through the same steps in Node.js, and each came out as expected.

Doing this with Clavem

Every activate and validate response carries the token, so there is nothing extra to call. Copy the product’s public key from its page. The format, claims and limits are in offline verification, and the flow that decides when to use it is in validation in Unity.

Questions

Is System.Security.Cryptography enough for Ed25519 in Unity?

No. Unity compiles against .NET Standard 2.1 by default, and that API has no Ed25519. Use a managed library such as BouncyCastle, or a native libsodium plug-in.

Can a player forge a token?

Not without the private key, which stays with Clavem. A player who patches your game to replace the public key or skip the check can still get in. Offline verification stops casual forgery, not a determined cracker.

Sources

  1. Unity manual, .NET profile support (checked November 2026)
  2. BouncyCastle.Cryptography on NuGet, MIT licensed, targets .NET Standard 2.0 (checked November 2026)
  3. Unity manual, managed code stripping and link.xml (checked November 2026)