altcha

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 19, 2026 License: MIT Imports: 12 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package altcha implements BRAIN's Altcha-style SHA-256 proof-of-work captcha.

BRAIN migrated from reCAPTCHA v2 to this PoW some time before 2026-05-17. The full flow used by the BRAIN registration path:

  1. GET /captcha -> { algorithm, challenge, salt, signature, maxNumber }
  2. Find n in [0, maxNumber] such that hex(sha256(salt + str(n))) == challenge
  3. POST /users with auxiliary.captcha = base64(JSON({...challenge, number, took}))

On a modern CPU the linear single-threaded solve median is ~250ms for maxNumber=1_000_000. The parallel solver here splits the [0, maxNumber] range across runtime.NumCPU() goroutines and races them — typically 4-8x faster on multi-core hardware.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoSolution = errors.New("altcha: no solution in range")

ErrNoSolution is returned when no n in [0, MaxNumber] satisfies the challenge. BRAIN's server only issues challenges with a known solution in range; this error means the challenge has been tampered with or the algorithm field doesn't match what we can solve.

Functions

func Encode

func Encode(s *Solution) (string, error)

Encode produces the base64-of-JSON payload to drop into auxiliary.captcha on POST /users. The JSON field order doesn't matter to BRAIN's verifier — it parses the payload back into a struct on the server side.

Types

type CaptchaAdapter

type CaptchaAdapter struct {
	Workers int
}

CaptchaAdapter implements the brainapi.CaptchaSolver structural interface using the parallel SHA-256 solver in this package.

Wire it up in NewClient:

client, _ := brainapi.NewClient(brainapi.Options{
    CaptchaSolver: altcha.CaptchaAdapter{Workers: 8},
    ...
})

Workers <= 0 picks runtime.NumCPU(); a reasonable default for production.

func (CaptchaAdapter) Solve

func (s CaptchaAdapter) Solve(ctx context.Context, fetch func(context.Context) ([]byte, error)) (string, error)

Solve fetches a challenge via the SDK-supplied callback and runs the PoW. Returns the base64-encoded payload ready for auxiliary.captcha.

type Challenge

type Challenge struct {
	Algorithm string `json:"algorithm"`
	Challenge string `json:"challenge"`
	Salt      string `json:"salt"`
	Signature string `json:"signature"`
	MaxNumber int64  `json:"maxNumber"`
}

Challenge is the body BRAIN's /captcha endpoint returns.

func ParseChallenge

func ParseChallenge(body []byte) (*Challenge, error)

ParseChallenge decodes a /captcha JSON body into a Challenge.

type Solution

type Solution struct {
	Algorithm string `json:"algorithm"`
	Challenge string `json:"challenge"`
	Salt      string `json:"salt"`
	Signature string `json:"signature"`
	MaxNumber int64  `json:"maxNumber"`
	Number    int64  `json:"number"`
	Took      int64  `json:"took"`
}

Solution embeds the challenge and adds the found Number plus solve duration in milliseconds (Altcha's wire format expects "took" in ms).

func Solve

func Solve(ctx context.Context, ch Challenge, workers int) (*Solution, error)

Solve searches [0, ch.MaxNumber] for an n where hex(sha256(salt+str(n))) == challenge. workers <= 0 picks runtime.NumCPU().

The context can cancel the search; on ctx.Done() it returns ctx.Err().

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL