Clavem

Verify an Ed25519 license token offline in a Wails app

Use Go's standard library: crypto/ed25519 and the RawURLEncoding in encoding/base64. Split the token on dots, decode, verify the signature over "sgt1.<payload>", then apply the five claim checks. Check the key length first, because ed25519.Verify panics on a wrong-size key.

Standard library only

Go has had crypto/ed25519 since Go 1.13, and encoding/base64 has a RawURLEncoding for base64url without padding, which is the encoding Clavem uses for the key, the payload and the signature. Nothing to add to go.mod. The code belongs in the Go backend of your Wails app, next to the validation client that calls it, so the webview never holds the token.

package license

import (
	"crypto/ed25519"
	"crypto/sha256"
	"encoding/base64"
	"encoding/hex"
	"encoding/json"
	"regexp"
	"strings"
	"time"
)

type Claims struct {
	V         int    `json:"v"`
	LID       int64  `json:"lid"`
	PID       int64  `json:"pid"`
	KH        string `json:"kh"`
	Status    string `json:"status"`
	Valid     bool   `json:"valid"`
	IID       string `json:"iid"`
	FP        string `json:"fp"`
	Email     string `json:"email"`
	IAT       int64  `json:"iat"`
	EXP       *int64 `json:"exp"` // nil for perpetual licenses
	RefreshBy int64  `json:"refresh_by"`
}

var b64 = base64.RawURLEncoding // rejects "=" padding, accepts unpadded input

// VerifyToken returns the claims when the signature is valid, otherwise nil.
func VerifyToken(token, publicKey string) *Claims {
	parts := strings.Split(token, ".")
	if len(parts) != 3 || parts[0] != "sgt1" {
		return nil
	}
	key, err1 := b64.DecodeString(publicKey)
	sig, err2 := b64.DecodeString(parts[2])
	body, err3 := b64.DecodeString(parts[1])
	// ed25519.Verify panics when the key is not 32 bytes, so check both lengths first.
	if err1 != nil || err2 != nil || err3 != nil ||
		len(key) != ed25519.PublicKeySize || len(sig) != ed25519.SignatureSize {
		return nil
	}
	if !ed25519.Verify(key, []byte(parts[0]+"."+parts[1]), sig) {
		return nil
	}
	var c Claims
	if json.Unmarshal(body, &c) != nil {
		return nil
	}
	return &c
}

// Licensed applies the checks from the docs. A nil receiver is false.
func (c *Claims) Licensed(productID int64, now time.Time) bool {
	if c == nil {
		return false
	}
	t := now.Unix()
	return c.V == 1 && c.PID == productID && c.Valid &&
		(c.EXP == nil || *c.EXP > t) && c.RefreshBy > t
}

var nativeKey = regexp.MustCompile(`^[0-9A-Za-z]{5}(?:-[0-9A-Za-z]{5}){4}$`)

// MatchesKey is the optional check that the token was issued for the key the customer typed.
func (c *Claims) MatchesKey(licenseKey string) bool {
	k := strings.TrimSpace(licenseKey)
	if nativeKey.MatchString(k) {
		k = strings.NewReplacer("I", "1", "L", "1", "O", "0", "U", "V").Replace(strings.ToUpper(k))
	} else {
		k = strings.ToLower(k)
	}
	sum := sha256.Sum256([]byte(k))
	return hex.EncodeToString(sum[:])[:16] == c.KH
}

A method on a nil *Claims is legal in Go, so VerifyToken(t, key).Licensed(id, time.Now()) is safe when verification failed.

What I ran

I ran this on the Go Playground with a throwaway key pair and tokens in Clavem’s format. The real demo product’s public key is not available to me, so the demo token could not be verified.

Token Signature Licensed Reason
good valid yes All checks pass
tampered invalid no pid edited after signing
stale valid no refresh_by has passed
expiredLicense valid no valid is false
otherProduct valid no pid is not yours
wrongVersion valid no v is 2
pastExp valid no exp is in the past
wrongKey invalid no Signed by another product’s key

The garbage inputs not-a-token and sgt1.a.b with a 5-byte key returned nil. The kh check matched the demo key S1GNE-TDEM0-7K4QX-9WZ2P-H5RB8 against a token carrying its real kh, 7e0e4148136b6dae, also when typed in lowercase with spaces, and rejected another key.

Go traps

  • ed25519.Verify panics on a bad key. In the playground, a 5-byte key produced ed25519: bad public key length: 5. A panic skips your normal error handling, so check len(key) and len(sig) as above, and never pass an unvalidated key string through.
  • RawURLEncoding is strict. YQ== failed with illegal base64 data at input byte 2 and YQ decoded. If you ever paste a token through a tool that adds padding, strip the = first.
  • Optional exp. A plain int64 would read a missing exp as 0 and mark every perpetual license expired. Use *int64, as above.
  • Whole-number JSON. Unix seconds fit int64 exactly. Do not decode into float64 and compare.
  • Clock set back. Save the highest iat you have seen next to the token. If now + 5 minutes < maxIAT, treat the clock as untrustworthy and ask for an online validation. The five minutes absorb a slow clock.

Where the key and token live

Compile the public key into the binary as a constant, or set it at build time with -ldflags "-X". It is not a secret. The token goes in the keychain with the license key. A user who patches a Go binary can still skip the check; the signature stops forged tokens, not removed checks.

When refresh_by passes, Licensed returns false and the validation client asks the server again. See the validation guide.

Doing this with Clavem

Each product has its own Ed25519 key pair. The public half is on the product page and in GET /v1/admin/products. The token fields and the 14-day window are described in the offline verification docs.

Questions

Should the webview verify the token with Web Crypto?

No. Do it in Go. The webview version is outside your control, especially on Linux where it is the system's WebKitGTK, and JavaScript in a webview can be inspected and changed. Go's standard library behaves the same on every target.

Does verification need the network?

No. It uses only the public key compiled into your app and the token you stored. The network matters only when refresh_by has passed and the app has to validate online again.

Sources

  1. Go documentation, crypto/ed25519 (checked November 2026)