altcha-org / altcha-lib

A JavaScript library for creating and verifying ALTCHA challenges.
https://altcha.org
MIT License
18 stars 5 forks source link
altcha captcha javascript typescript webcrypto

ALTCHA JS Library

ALTCHA JS Library is a lightweight, zero-dependency library designed for creating and verifying ALTCHA challenges.

Compatibility

This library utilizes Web Crypto.

Usage

import { createChallenge, verifySolution } from 'altcha-lib';

const hmacKey = 'secret hmac key';

// Create a new challenge and send it to the client:
const challenge = await createChallenge({
  hmacKey,
  maxNumber: 100000, // the maximum random number
});

// When submitted, verify the payload:
const ok = await verifySolution(payload, hmacKey);

Usage with Node.js 16

In Node.js version 16, there is no global reference to crypto by default. To use this library, you need to add the following code to your codebase:

globalThis.crypto = require('node:crypto').webcrypto;

Or with import syntax:

import { webcrypto } from 'node:crypto';

globalThis.crypto = webcrypto;

API

createChallenge(options)

Creates a new challenge for ALTCHA.

Parameters:

Returns: Promise<Challenge>

extractParams(payload)

Extracts optional parameters from the challenge or payload.

Parameters:

Returns: Record<string, string>

verifySolution(payload, hmacKey, checkExpires = true)

Verifies an ALTCHA solution. The payload can be a Base64-encoded JSON payload (as submitted by the widget) or an object.

Parameters:

Returns: Promise<boolean>

verifyServerSignature(payload, hmacKey)

Verifies the server signature returned by the API. The payload can be a Base64-encoded JSON payload or an object.

Parameters:

Returns: Promise<{ verificationData: ServerSignatureVerificationData | null, verified: boolean }>

verifyFieldsHash(formData, fields, fieldsHash, algorithm?)

Verifies the hash of form fields returned by the Spam Filter.

Parameters:

Returns: Promise<boolean>

solveChallenge(challenge, salt, algorithm?, max?, start?)

Finds a solution to the given challenge.

Parameters:

Returns: { controller: AbortController, promise: Promise<Solution | null> }

solveChallengeWorkers(workerScript, concurrency, challenge, salt, algorithm?, max?, start?)

Finds a solution to the given challenge with Web Workers running concurrently.

Parameters:

Returns: Promise<Solution | null>

Usage with altcha-lib/worker:

import { solveChallengeWorkers } from 'altcha-lib';

const solution = await solveChallengeWorkers(
  'altcha-lib/worker', // Worker script URL or path
  8, // Spawn 8 workers
  challenge,
  salt,
);

Benchmarks

> solveChallenge()
- n = 1,000...............................        312 ops/s ±2.90%
- n = 10,000..............................         31 ops/s ±1.50%
- n = 50,000..............................          6 ops/s ±0.82%
- n = 100,000.............................          3 ops/s ±0.37%
- n = 500,000.............................          0 ops/s ±0.31%

> solveChallengeWorkers() (8 workers)
- n = 1,000...............................         62 ops/s ±3.99%
- n = 10,000..............................         31 ops/s ±6.83%
- n = 50,000..............................         11 ops/s ±4.00%
- n = 100,000.............................          7 ops/s ±2.32%
- n = 500,000.............................          1 ops/s ±1.89%

Run with Bun on MacBook Pro M3-Pro. See /benchmark folder for more details.

License

MIT