From cb0a90d3b806b727246d8f763536ee6aff741a70 Mon Sep 17 00:00:00 2001 From: Daniel Regeci <536331+ovx@users.noreply.github.com> Date: Sat, 23 Mar 2024 11:45:05 +0100 Subject: [PATCH] 0.1.1 --- .gitignore | 3 ++- README.md | 2 ++ dist/index.js | 9 ++++++--- dist/types.d.ts | 2 ++ lib/index.ts | 9 ++++++--- lib/types.ts | 2 ++ package.json | 6 +++++- 7 files changed, 25 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 6ceaf66..8abbdbc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /node_modules -.TODO \ No newline at end of file +.TODO +.DS_Store \ No newline at end of file diff --git a/README.md b/README.md index 2cffaae..49b18a0 100644 --- a/README.md +++ b/README.md @@ -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)` diff --git a/dist/index.js b/dist/index.js index d5e1204..cbf485c 100644 --- a/dist/index.js +++ b/dist/index.js @@ -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, diff --git a/dist/types.d.ts b/dist/types.d.ts index 7186cae..3c9cba4 100644 --- a/dist/types.d.ts +++ b/dist/types.d.ts @@ -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; diff --git a/lib/index.ts b/lib/index.ts index 191cdff..e2bb47f 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -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 { 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, diff --git a/lib/types.ts b/lib/types.ts index 7fed753..48ba028 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -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 { diff --git a/package.json b/package.json index 731f727..2b08223 100644 --- a/package.json +++ b/package.json @@ -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": {