altcha-lib 2.0.1 → 2.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +235 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,6 +2,45 @@
|
|
|
2
2
|
|
|
3
3
|
ALTCHA TS/JS Library is a lightweight library for creating and verifying [ALTCHA](https://altcha.org) challenges on the server.
|
|
4
4
|
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install altcha-lib
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { createChallenge, deriveHmacKeySecret, randomInt, verifySolution } from 'altcha-lib';
|
|
15
|
+
import { deriveKey } from 'altcha-lib/algorithms/pbkdf2';
|
|
16
|
+
|
|
17
|
+
const HMAC_SECRET = 'your-secret-key';
|
|
18
|
+
const HMAC_KEY_SECRET = 'your-other-secret-key';
|
|
19
|
+
|
|
20
|
+
// On the server: create a challenge and send it to the client
|
|
21
|
+
const challenge = await createChallenge({
|
|
22
|
+
algorithm: 'PBKDF2/SHA-256',
|
|
23
|
+
cost: 5_000,
|
|
24
|
+
counter: randomInt(5_000, 10_000),
|
|
25
|
+
deriveKey,
|
|
26
|
+
hmacSignatureSecret: HMAC_SECRET,
|
|
27
|
+
hmacKeySignatureSecret: HMAC_KEY_SECRET,
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
// On the server: verify the solution submitted by the client
|
|
31
|
+
const result = await verifySolution({
|
|
32
|
+
challenge: payload.challenge,
|
|
33
|
+
solution: payload.solution,
|
|
34
|
+
deriveKey,
|
|
35
|
+
hmacSignatureSecret: HMAC_SECRET,
|
|
36
|
+
hmacKeySignatureSecret: HMAC_KEY_SECRET,
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
if (result.verified) {
|
|
40
|
+
// challenge passed
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
5
44
|
## Get Started
|
|
6
45
|
|
|
7
46
|
The library includes plugins for several popular frameworks to simplify integration.
|
|
@@ -37,7 +76,7 @@ If your framework is not listed, see the [Advanced Usage](/docs/advanced-usage.m
|
|
|
37
76
|
| Deno | 2+ | Argon2 not available natively |
|
|
38
77
|
| WinterCG runtimes | — | Use WebCrypto [algorithms](/docs/algorithms.md) |
|
|
39
78
|
|
|
40
|
-
|
|
79
|
+
## Run Examples
|
|
41
80
|
|
|
42
81
|
**Express example (Node.js with `tsx`):**
|
|
43
82
|
|
|
@@ -71,6 +110,201 @@ The API for the previous PoW version (v1) remains available under the `altcha-li
|
|
|
71
110
|
import { createChallenge } from 'altcha-lib/v1';
|
|
72
111
|
```
|
|
73
112
|
|
|
113
|
+
## API Reference
|
|
114
|
+
|
|
115
|
+
The default import path (`altcha-lib`) uses the v2 API. The v1 API is available at `altcha-lib/v1`.
|
|
116
|
+
|
|
117
|
+
### v2 (`altcha-lib`)
|
|
118
|
+
|
|
119
|
+
#### `createChallenge(options: CreateChallengeOptions): Promise<Challenge>`
|
|
120
|
+
|
|
121
|
+
Creates a new proof-of-work challenge. Generates a random nonce and salt, optionally pre-computes a key prefix from a known counter value, and optionally signs the challenge with HMAC.
|
|
122
|
+
|
|
123
|
+
| Option | Type | Description |
|
|
124
|
+
|---|---|---|
|
|
125
|
+
| `algorithm` | `string` | Key derivation algorithm (e.g. `'PBKDF2/SHA-256'`, `'ARGON2ID'`, `'SCRYPT'`). |
|
|
126
|
+
| `cost` | `number` | Algorithm-specific cost controlling computational difficulty. |
|
|
127
|
+
| `deriveKey` | `DeriveKeyFunction` | Key derivation function. |
|
|
128
|
+
| `counter` | `number?` | Known counter value for deterministic mode. |
|
|
129
|
+
| `counterMode` | `'uint32' \| 'string'` | Counter encoding format. Defaults to `'uint32'`. |
|
|
130
|
+
| `data` | `Record<string, ...>?` | Arbitrary metadata embedded in the challenge. |
|
|
131
|
+
| `expiresAt` | `number \| Date?` | Expiry timestamp (seconds) or Date. |
|
|
132
|
+
| `hmacAlgorithm` | `HmacAlgorithm?` | HMAC digest algorithm. Defaults to `'SHA-256'`. |
|
|
133
|
+
| `hmacKeySignatureSecret` | `string?` | HMAC secret for signing derived keys (deterministic mode). |
|
|
134
|
+
| `hmacSignatureSecret` | `string?` | HMAC secret for signing the challenge payload. |
|
|
135
|
+
| `keyLength` | `number?` | Derived key length in bytes. Defaults to `32`. |
|
|
136
|
+
| `keyPrefix` | `string?` | Required prefix the derived key must match. |
|
|
137
|
+
| `keyPrefixLength` | `number?` | Number of bytes used as prefix in deterministic mode. Defaults to `keyLength / 2`. |
|
|
138
|
+
| `memoryCost` | `number?` | Memory cost in KiB (Argon2id/scrypt only). |
|
|
139
|
+
| `parallelism` | `number?` | Parallelism setting (Argon2id/scrypt only). |
|
|
140
|
+
|
|
141
|
+
#### `solveChallenge(options: SolveChallengeOptions): Promise<Solution | null>`
|
|
142
|
+
|
|
143
|
+
Solves a challenge by brute-forcing counter values until the derived key starts with the required prefix. Returns `null` on timeout or abort.
|
|
144
|
+
|
|
145
|
+
| Option | Type | Description |
|
|
146
|
+
|---|---|---|
|
|
147
|
+
| `challenge` | `Challenge` | The challenge to solve. |
|
|
148
|
+
| `deriveKey` | `DeriveKeyFunction` | Key derivation function. |
|
|
149
|
+
| `controller` | `AbortController?` | For cancelling the solve operation. |
|
|
150
|
+
| `counterStart` | `number?` | Initial counter value. Defaults to `0`. |
|
|
151
|
+
| `counterStep` | `number?` | Increment between attempts. Defaults to `1`. |
|
|
152
|
+
| `counterMode` | `'uint32' \| 'string'?` | Counter encoding format. Defaults to `'uint32'`. |
|
|
153
|
+
| `timeout` | `number?` | Timeout in milliseconds. Defaults to `90000`. |
|
|
154
|
+
|
|
155
|
+
#### `solveChallengeWorkers(options): Promise<Solution | null>`
|
|
156
|
+
|
|
157
|
+
Solves a challenge using multiple Web Workers in parallel. Each worker tests a different interleaved subset of counter values. Automatically retries with fewer workers on out-of-memory errors.
|
|
158
|
+
|
|
159
|
+
| Option | Type | Description |
|
|
160
|
+
|---|---|---|
|
|
161
|
+
| `challenge` | `Challenge` | The challenge to solve. |
|
|
162
|
+
| `concurrency` | `number` | Number of workers to use (max 16). |
|
|
163
|
+
| `createWorker` | `(algorithm: string) => Worker \| Promise<Worker>` | Factory function to create a worker. |
|
|
164
|
+
| `controller` | `AbortController?` | For cancelling the solve operation. |
|
|
165
|
+
| `counterMode` | `'uint32' \| 'string'?` | Counter encoding format. |
|
|
166
|
+
| `onOutOfMemory` | `(concurrency: number) => number \| void?` | Called on OOM; return new concurrency to retry or falsy to abort. |
|
|
167
|
+
| `timeout` | `number?` | Timeout in milliseconds. |
|
|
168
|
+
|
|
169
|
+
#### `verifySolution(options: VerifySolutionOptions): Promise<VerifySolutionResult>`
|
|
170
|
+
|
|
171
|
+
Verifies a submitted solution against a challenge. Checks expiration, challenge signature integrity, and that the derived key matches the solution.
|
|
172
|
+
|
|
173
|
+
| Option | Type | Description |
|
|
174
|
+
|---|---|---|
|
|
175
|
+
| `challenge` | `Challenge` | The original challenge. |
|
|
176
|
+
| `solution` | `Solution` | The solution to verify. |
|
|
177
|
+
| `hmacSignatureSecret` | `string` | HMAC secret used when the challenge was created. |
|
|
178
|
+
| `deriveKey` | `DeriveKeyFunction` | Key derivation function. |
|
|
179
|
+
| `counterMode` | `'uint32' \| 'string'?` | Counter encoding format. |
|
|
180
|
+
| `hmacAlgorithm` | `HmacAlgorithm?` | HMAC digest algorithm. Defaults to `'SHA-256'`. |
|
|
181
|
+
| `hmacKeySignatureSecret` | `string?` | HMAC secret for verifying derived-key signatures. |
|
|
182
|
+
|
|
183
|
+
Returns `VerifySolutionResult`:
|
|
184
|
+
|
|
185
|
+
| Field | Type | Description |
|
|
186
|
+
|---|---|---|
|
|
187
|
+
| `verified` | `boolean` | Whether the solution is valid. |
|
|
188
|
+
| `expired` | `boolean` | Whether the challenge has expired. |
|
|
189
|
+
| `invalidSignature` | `boolean \| null` | Whether the challenge signature is invalid. |
|
|
190
|
+
| `invalidSolution` | `boolean \| null` | Whether the solution is incorrect. |
|
|
191
|
+
| `time` | `number` | Time taken to verify in milliseconds. |
|
|
192
|
+
|
|
193
|
+
#### `verifyFieldsHash(options): Promise<boolean>`
|
|
194
|
+
|
|
195
|
+
Verifies the SHA hash of specified form fields.
|
|
196
|
+
|
|
197
|
+
| Option | Type | Description |
|
|
198
|
+
|---|---|---|
|
|
199
|
+
| `formData` | `FormData \| Record<string, unknown>` | The form data to verify. |
|
|
200
|
+
| `fields` | `string[]` | Field names to include in the hash. |
|
|
201
|
+
| `fieldsHash` | `string` | The expected hash value. |
|
|
202
|
+
| `algorithm` | `string?` | Hash algorithm. Defaults to `'SHA-256'`. |
|
|
203
|
+
|
|
204
|
+
#### `verifyServerSignature(options): Promise<VerifyServerSignatureResult>`
|
|
205
|
+
|
|
206
|
+
Verifies a server signature payload from ALTCHA Sentinel.
|
|
207
|
+
|
|
208
|
+
| Option | Type | Description |
|
|
209
|
+
|---|---|---|
|
|
210
|
+
| `payload` | `ServerSignaturePayload` | The payload to verify. |
|
|
211
|
+
| `hmacSecret` | `string` | The HMAC secret. |
|
|
212
|
+
|
|
213
|
+
Returns `VerifyServerSignatureResult` (extends `VerifySolutionResult`):
|
|
214
|
+
|
|
215
|
+
| Field | Type | Description |
|
|
216
|
+
|---|---|---|
|
|
217
|
+
| `verified` | `boolean` | Whether the signature is valid. |
|
|
218
|
+
| `verificationData` | `ServerSignatureVerificationData \| null` | Parsed verification data. |
|
|
219
|
+
|
|
220
|
+
#### `obfuscate(str: string, options?): Promise<string>`
|
|
221
|
+
|
|
222
|
+
> Import from `altcha-lib/obfuscation`
|
|
223
|
+
|
|
224
|
+
Encrypts a string using AES-GCM, with the key derived from a PoW challenge. Returns a base64-encoded payload.
|
|
225
|
+
|
|
226
|
+
| Option | Type | Description |
|
|
227
|
+
|---|---|---|
|
|
228
|
+
| `counterMin` | `number?` | Minimum counter value. Defaults to `20`. |
|
|
229
|
+
| `counterMax` | `number?` | Maximum counter value. Defaults to `200`. |
|
|
230
|
+
| `deriveKey` | `DeriveKeyFunction?` | Key derivation function. Defaults to PBKDF2. |
|
|
231
|
+
| `...` | | Any `CreateChallengeOptions` fields. |
|
|
232
|
+
|
|
233
|
+
#### `deobfuscate(obfuscatedData: string, options?): Promise<string>`
|
|
234
|
+
|
|
235
|
+
> Import from `altcha-lib/obfuscation`
|
|
236
|
+
|
|
237
|
+
Decrypts an obfuscated string by solving the embedded PoW challenge and using the derived key.
|
|
238
|
+
|
|
239
|
+
| Option | Type | Description |
|
|
240
|
+
|---|---|---|
|
|
241
|
+
| `concurrency` | `number?` | Worker concurrency. Defaults to up to 4. |
|
|
242
|
+
| `createWorker` | `(algorithm: string) => Worker?` | Factory to create a worker for solving. |
|
|
243
|
+
| `deriveKey` | `DeriveKeyFunction?` | Key derivation function. Defaults to PBKDF2. |
|
|
244
|
+
|
|
245
|
+
#### `randomInt(max: number, min?: number): number`
|
|
246
|
+
|
|
247
|
+
Returns a cryptographically random integer between `min` (default `1`) and `max`.
|
|
248
|
+
|
|
249
|
+
#### `class CappedMap<K, V>`
|
|
250
|
+
|
|
251
|
+
A `Map` subclass with a fixed maximum size. When full, the oldest entry is evicted on insertion.
|
|
252
|
+
|
|
253
|
+
```ts
|
|
254
|
+
new CappedMap({ maxSize: number })
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
#### `enum HmacAlgorithm`
|
|
258
|
+
|
|
259
|
+
| Value | String |
|
|
260
|
+
|---|---|
|
|
261
|
+
| `HmacAlgorithm.SHA_256` | `'SHA-256'` |
|
|
262
|
+
| `HmacAlgorithm.SHA_384` | `'SHA-384'` |
|
|
263
|
+
| `HmacAlgorithm.SHA_512` | `'SHA-512'` |
|
|
264
|
+
|
|
265
|
+
---
|
|
266
|
+
|
|
267
|
+
### v1 (`altcha-lib/v1`)
|
|
268
|
+
|
|
269
|
+
#### `createChallenge(options: ChallengeOptions): Promise<Challenge>`
|
|
270
|
+
|
|
271
|
+
Creates a v1 SHA-based proof-of-work challenge.
|
|
272
|
+
|
|
273
|
+
| Option | Type | Description |
|
|
274
|
+
|---|---|---|
|
|
275
|
+
| `hmacKey` | `string` | **Required.** HMAC key for signing. |
|
|
276
|
+
| `algorithm` | `'SHA-1' \| 'SHA-256' \| 'SHA-512'?` | Hash algorithm. Defaults to `'SHA-256'`. |
|
|
277
|
+
| `expires` | `Date?` | Expiry date embedded in the salt. |
|
|
278
|
+
| `maxNumber` | `number?` | Maximum random number. Defaults to `1000000`. |
|
|
279
|
+
| `number` | `number?` | Fixed number (skips random selection). |
|
|
280
|
+
| `params` | `Record<string, string>?` | Extra parameters embedded in the salt. |
|
|
281
|
+
| `salt` | `string?` | Custom salt value. |
|
|
282
|
+
| `saltLength` | `number?` | Random salt length in bytes. Defaults to `12`. |
|
|
283
|
+
|
|
284
|
+
#### `verifySolution(payload: string | Payload, hmacKey: string, checkExpires?: boolean): Promise<boolean>`
|
|
285
|
+
|
|
286
|
+
Verifies a v1 solution payload.
|
|
287
|
+
|
|
288
|
+
#### `verifyFieldsHash(formData, fields, fieldsHash, algorithm?): Promise<boolean>`
|
|
289
|
+
|
|
290
|
+
Verifies the hash of specified form fields.
|
|
291
|
+
|
|
292
|
+
#### `verifyServerSignature(payload: string | ServerSignaturePayload, hmacKey: string): Promise<{ verified: boolean, verificationData: ServerSignatureVerificationData | null }>`
|
|
293
|
+
|
|
294
|
+
Verifies a v1 server signature.
|
|
295
|
+
|
|
296
|
+
#### `solveChallenge(challenge, salt, algorithm?, max?, start?): { promise: Promise<Solution | null>, controller: AbortController }`
|
|
297
|
+
|
|
298
|
+
Solves a v1 challenge by brute force.
|
|
299
|
+
|
|
300
|
+
#### `solveChallengeWorkers(workerScript, concurrency, challenge, salt, algorithm?, max?, startNumber?): Promise<Solution | null>`
|
|
301
|
+
|
|
302
|
+
Solves a v1 challenge using Web Workers.
|
|
303
|
+
|
|
304
|
+
#### `extractParams(payload: string | Payload | Challenge): Record<string, string>`
|
|
305
|
+
|
|
306
|
+
Extracts URL parameters embedded in a challenge salt.
|
|
307
|
+
|
|
74
308
|
## License
|
|
75
309
|
|
|
76
310
|
MIT
|