ALTCHA Java Library
The ALTCHA Java Library is a lightweight library designed for creating and verifying ALTCHA challenges.
Compatibility
Examples
examples/server/
Minimal ALTCHA v2 example server. Run: gradle :examples:server:run
examples/argon2/
Example using Argon2id algorithm. Run: gradle :examples:argon2:run
Installation
Maven Central: org.altcha/altcha
Maven:
<dependency>
<groupId>org.altcha</groupId>
<artifactId>altcha</artifactId>
<version>2.1.1</version>
</dependency>
Gradle:
implementation 'org.altcha:altcha:2.1.1'
org.json must be present at runtime (it is a provided dependency):
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20240303</version>
</dependency>
Protocol versions
| Version |
Package |
Algorithm |
| v1 (legacy) |
org.altcha.altcha.v1 |
SHA-1 / SHA-256 / SHA-512 |
| v2 |
org.altcha.altcha.v2 |
PBKDF2 / SHA-iterative (pluggable KDF) |
Both versions live side-by-side. Use org.altcha.altcha.v2 for new integrations.
v2 Usage
v2 uses a configurable key-derivation function (KDF). The server creates a signed challenge; the client brute-forces a counter until the derived key starts with the required prefix.
Create a challenge (server)
import org.altcha.altcha.v2.Altcha;
var options = new Altcha.CreateChallengeOptions()
.algorithm("PBKDF2/SHA-256")
.cost(5_000) // PBKDF2 iterations
.hmacSignatureSecret("your-secret-key")
.expiresInSeconds(600); // 10 minutes
Altcha.Challenge challenge = Altcha.createChallenge(options);
// Serialize challenge to JSON and send to client
Supported algorithms (built-in):
| String |
KDF |
"PBKDF2/SHA-256" |
PBKDF2-HMAC-SHA-256 |
"PBKDF2/SHA-384" |
PBKDF2-HMAC-SHA-384 |
"PBKDF2/SHA-512" |
PBKDF2-HMAC-SHA-512 |
"SHA-256" |
Iterative SHA-256 |
"SHA-384" |
Iterative SHA-384 |
"SHA-512" |
Iterative SHA-512 |
External KDFs (Argon2id, Scrypt) can be plugged in via the KeyDerivationFunction interface.
Solve a challenge (client-side utility)
var kdf = Altcha.kdf(challenge.parameters().algorithm());
var solution = Altcha.solveChallenge(challenge, kdf); // null if not solved within 90 s
// Encode {challenge, solution} as JSON, base64 it, and submit
Like the JavaScript library, solveChallenge gives up after Altcha.DEFAULT_SOLVE_TIMEOUT (90 s) and returns null; pass a Duration to change it (null or zero disables it). To abort a running solve, interrupt its thread (e.g. Future.cancel(true)); it then throws InterruptedException.
var solution = Altcha.solveChallenge(challenge, kdf, 0, 1, Duration.ofSeconds(10));
Verify a solution (server)
// From a base64-encoded payload submitted by the client:
Altcha.VerifySolutionResult result = Altcha.verifySolution(
base64Payload,
"your-secret-key",
Altcha.kdf("PBKDF2/SHA-256"));
// Challenges created with hmacAlgorithm / counterMode must be verified with the same values:
Altcha.VerifySolutionResult result384 = Altcha.verifySolution(
base64Payload, "your-secret-key", null, "SHA-384", null, Altcha.kdf("PBKDF2/SHA-256"));
if (result.verified()) {
// accept
} else if (result.expired()) {
// challenge expired
} else if (Boolean.TRUE.equals(result.invalidSignature())) {
// challenge was tampered with
}
Or from already-parsed objects:
Altcha.VerifySolutionResult result = Altcha.verifySolution(
challenge, solution, "your-secret-key", kdf);
Custom metadata / expiry
var options = new Altcha.CreateChallengeOptions()
.algorithm("PBKDF2/SHA-256")
.cost(5_000)
.hmacSignatureSecret("secret")
.expiresInSeconds(300)
.data(Map.of("userId", "42", "action", "login"));
data values must be JSON types: String, Number, Boolean, null, Map or List. Signing uses the same canonical JSON as the JavaScript library (JSON.stringify with recursively sorted keys), so signatures interoperate with it and survive the widget's JSON round trip: numbers are formatted like JavaScript (1.0 → 1, 1e21 → 1e+21), and objects inside a List are not sorted but keep their iteration order — use a LinkedHashMap for those.
Deterministic mode (key signature)
In deterministic mode the server pre-computes the expected key prefix from a known counter. This allows fast verification without re-running the KDF.
var options = new Altcha.CreateChallengeOptions()
.algorithm("SHA-256")
.cost(5_000)
.counter(123) // random counter
.hmacSignatureSecret("secret")
.hmacKeySignatureSecret("key-secret"); // signs the derived key
Altcha.Challenge challenge = Altcha.createChallenge(options);
// Verify using key signature (fast — no KDF re-invocation).
// The 5th argument is the HMAC algorithm (null = "SHA-256"); it must match CreateChallengeOptions.hmacAlgorithm.
Altcha.VerifySolutionResult result = Altcha.verifySolution(
challenge, solution, "secret", "key-secret", null, null);
Pluggable KDF (e.g. Argon2id)
Altcha.KeyDerivationFunction argon2id = (params, salt, password) -> {
byte[] dk = /* your Argon2id library */ computeArgon2id(
password, salt,
params.cost(), // time cost
params.memoryCost(), // memory in KiB
params.parallelism(),
params.keyLength());
return new Altcha.DeriveKeyResult(dk);
};
var options = new Altcha.CreateChallengeOptions()
.algorithm("ARGON2ID")
.cost(3)
.memoryCost(65536)
.parallelism(1)
.deriveKey(argon2id)
.hmacSignatureSecret("secret");
A KDF can also return updated parameters, e.g. defaults it picked:
new Altcha.DeriveKeyResult(dk, updatedParams). In deterministic mode (counter set) createChallenge uses them in place of the challenge parameters before computing keyPrefix and signing, like the JavaScript library. Solve and verify ignore them.
Counter mode
By default the counter is appended to the nonce as a big-endian 32-bit integer (CounterMode.UINT32). CounterMode.STRING appends its decimal digits instead, for compatibility with the JavaScript library's counterMode: 'string'. The mode is not part of the signed challenge, so creator, solver and verifier must use the same one:
options.counterMode(Altcha.CounterMode.STRING);
var solution = Altcha.solveChallenge(challenge, kdf, 0, 1, Altcha.DEFAULT_SOLVE_TIMEOUT, Altcha.CounterMode.STRING);
var result = Altcha.verifySolution(challenge, solution, "secret", null, null, Altcha.CounterMode.STRING, kdf);
Fields hash (ALTCHA Sentinel)
boolean ok = Altcha.verifyFieldsHash(formData, fields, fieldsHash, "SHA-256");
Server signature (ALTCHA Sentinel)
Altcha.ServerSignatureVerification result =
Altcha.verifyServerSignature(base64Payload, "secret");
if (result.verified()) {
var data = result.verificationData();
Number score = data.score(); // null if absent
var reasons = data.reasons(); // empty list if none
var country = data.get("location.countryCode"); // any field, typed like JS
}
Verification data is parsed like the JavaScript library's parseVerificationData: true/false become Boolean, integers Long, decimals Double, everything else a trimmed String; non-empty fields/reasons become a List<String>. result.toJson() has the same fields as the JavaScript result (expired, invalidSignature, invalidSolution, time, verificationData, verified).
v2 API reference
Static methods
| Method |
Returns |
Description |