This commit is contained in:
Daniel Regeci
2024-03-23 11:45:05 +01:00
parent fc9cf3cfda
commit cb0a90d3b8
7 changed files with 25 additions and 8 deletions

3
.gitignore vendored
View File

@@ -1,2 +1,3 @@
/node_modules
.TODO
.TODO
.DS_Store

View File

@@ -37,8 +37,10 @@ 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).
### `verifySolution(payload, hmacKey)`

9
dist/index.js vendored
View File

@@ -1,10 +1,13 @@
import { ab2hex, hash, hmac, randomBytes, randomInt } from './helpers.js';
const DEFAULT_MAX_NUMBER = 1e7;
const DEFAULT_MAX_NUMBER = 1e6;
const DEFAULT_SALT_LEN = 12;
const DEFAULT_ALG = 'SHA-256';
export async function createChallenge(options) {
const algorithm = options.algorithm || DEFAULT_ALG;
const salt = options.salt || ab2hex(randomBytes(12));
const number = options.number === void 0 ? randomInt(DEFAULT_MAX_NUMBER) : options.number;
const maxNumber = options.maxNumber || DEFAULT_MAX_NUMBER;
const saltLength = options.saltLength || DEFAULT_SALT_LEN;
const salt = options.salt || ab2hex(randomBytes(saltLength));
const number = options.number === void 0 ? randomInt(maxNumber) : options.number;
const challenge = await hash(algorithm, salt + number);
return {
algorithm,

2
dist/types.d.ts vendored
View File

@@ -8,8 +8,10 @@ export interface Challenge {
export interface ChallengeOptions {
algorithm?: Algorithm;
hmacKey: string;
maxNumber?: number;
number?: number;
salt?: string;
saltLength?: number;
}
export interface Payload {
algorithm: Algorithm;

View File

@@ -6,16 +6,19 @@ import type {
Payload,
} from './types.js';
const DEFAULT_MAX_NUMBER = 1e7;
const DEFAULT_MAX_NUMBER = 1e6;
const DEFAULT_SALT_LEN = 12;
const DEFAULT_ALG: Algorithm = 'SHA-256';
export async function createChallenge(
options: ChallengeOptions
): Promise<Challenge> {
const algorithm = options.algorithm || DEFAULT_ALG;
const salt = options.salt || ab2hex(randomBytes(12));
const maxNumber = options.maxNumber || DEFAULT_MAX_NUMBER;
const saltLength = options.saltLength || DEFAULT_SALT_LEN;
const salt = options.salt || ab2hex(randomBytes(saltLength));
const number =
options.number === void 0 ? randomInt(DEFAULT_MAX_NUMBER) : options.number;
options.number === void 0 ? randomInt(maxNumber) : options.number;
const challenge = await hash(algorithm, salt + number);
return {
algorithm,

View File

@@ -10,8 +10,10 @@ export interface Challenge {
export interface ChallengeOptions {
algorithm?: Algorithm;
hmacKey: string;
maxNumber?: number;
number?: number;
salt?: string;
saltLength?: number;
}
export interface Payload {

View File

@@ -1,6 +1,6 @@
{
"name": "altcha-lib",
"version": "0.1.0",
"version": "0.1.1",
"description": "A library for creating and verifying ALTCHA challenges for Node.js, Bun and Deno.",
"author": "Daniel Regeci",
"license": "MIT",
@@ -29,6 +29,10 @@
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js"
},
"./types": {
"types": "./dist/types.d.ts",
"import": "./dist/types.js"
}
},
"typesVersions": {