npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

altcha-lib

v0.2.0

Published

A library for creating and verifying ALTCHA challenges for Node.js, Bun and Deno.

Downloads

665

Readme

ALTCHA JS Library

ALTCHA JS Library is a lightweight, zero-dependency library designed for creating and verifying ALTCHA challenges specifically tailored for Node.js, Bun, and Deno environments.

Compatibility

This library utilizes Web Crypto and is intended for server-side use.

  • Node.js 16+
  • Bun 1+
  • Deno 1+

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);

API

createChallenge(options)

Creates a new challenge for ALTCHA.

Parameters:

  • options: ChallengeOptions:
    • algorithm?: string: Algorithm to use (SHA-1, SHA-256, SHA-512, default: SHA-256).
    • hmacKey: string (required): Signature HMAC key.
    • maxNumber?: number Optional maximum number for the random number generator (defaults to 1,000,000).
    • number?: number: Optional number to use. If not provided, a random number will be generated.
    • salt?: string: Optional salt string. If not provided, a random salt will be generated.
    • saltLength?: number Optional maximum lenght of the random salt (in bytes, defaults to 12).

Returns: Promise<Challenge>

verifySolution(payload, hmacKey)

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

Parameters:

  • payload: string | Payload
  • hmacKey: string

Returns: Promise<boolean>

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

Finds a solution to the given challenge.

Parameters:

  • challenge: string (required): The challenge hash.
  • salt: string (required): The challenge salt.
  • algorithm?: string: Optional algorithm (default: SHA-256).
  • max?: string: Optional maxnumber to iterate to (default: 1e6).
  • start?: string: Optional starting number (default: 0).

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

verifyServerSignature(payload, hmacKey)

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

Parameters:

  • payload: string | ServerSignaturePayload
  • hmacKey: string

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

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

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

Parameters:

  • workerScript: string (required): The path or URL of the worker script.
  • concurrency: number (required): The concurrency (number of workers).
  • challenge: string (required): The challenge hash.
  • salt: string (required): The challenge salt.
  • algorithm?: string: Optional algorithm (default: SHA-256).
  • max?: string: Optional maxnumber to iterate to (default: 1e6).
  • start?: string: Optional starting number (default: 0).

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...............................        317 ops/s ±2.63%
- n = 10,000..............................         32 ops/s ±1.88%
- n = 100,000.............................          3 ops/s ±0.34%
- n = 500,000.............................          0 ops/s ±0.32%

> solveChallengeWorkers() (8 workers)
- n = 1,000...............................         66 ops/s ±3.44%
- n = 10,000..............................         31 ops/s ±4.28%
- n = 100,000.............................          7 ops/s ±4.40%
- n = 500,000.............................          1 ops/s ±2.49%

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

License

MIT