Skip to content

Commit

Permalink
0.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
ovx committed Mar 23, 2024
1 parent fc9cf3c commit cb0a90d
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/node_modules
.TODO
.TODO
.DS_Store
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)`

Expand Down
9 changes: 6 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
2 changes: 2 additions & 0 deletions dist/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 6 additions & 3 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -29,6 +29,10 @@
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js"
},
"./types": {
"types": "./dist/types.d.ts",
"import": "./dist/types.js"
}
},
"typesVersions": {
Expand Down

0 comments on commit cb0a90d

Please sign in to comment.