Skip to content

Commit eeaad4d

Browse files
committed
Modernize code
Uses types where possible and improves array handling. The default challenge options now use sane defaults and optional parameters. Improves validation of user input.
1 parent 4acd49c commit eeaad4d

10 files changed

+386
-186
lines changed

README.md

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ use AltchaOrg\Altcha\Altcha;
3636
$hmacKey = 'secret hmac key';
3737

3838
// Create a new challenge
39-
$options = new ChallengeOptions([
40-
'hmacKey' => $hmacKey,
41-
'maxNumber' => 50000, // the maximum random number
39+
$options = new ChallengeOptions(
40+
$hmacKey,
41+
ChallengeOptions::DEFAULT_ALGORITHM,
42+
50000, // the maximum random number
4243
]);
4344

4445
$challenge = Altcha::createChallenge($options);
@@ -65,46 +66,37 @@ if ($ok) {
6566

6667
## API
6768

68-
### `Altcha::createChallenge(array $options): array`
69+
### `Altcha::createChallenge(ChallengeOptions $options): Challenge`
6970

7071
Creates a new challenge for ALTCHA.
7172

72-
**Parameters:**
73+
**Returns:** `Challenge`
7374

74-
- `options array`:
75-
- `algorithm string`: Hashing algorithm to use (`SHA-1`, `SHA-256`, `SHA-512`, default: `SHA-256`).
76-
- `maxNumber int`: Maximum number for the random number generator (default: 1,000,000).
77-
- `saltLength int`: Length of the random salt (default: 12 bytes).
78-
- `hmacKey string`: Required HMAC key.
79-
- `salt string`: Optional salt string. If not provided, a random salt will be generated.
80-
- `number int`: Optional specific number to use. If not provided, a random number will be generated.
81-
- `expires \DateTime`: Optional expiration time for the challenge.
82-
- `params array`: Optional URL-encoded query parameters.
75+
#### `ChallengeOptions`
8376

84-
**Returns:** `array`
77+
```php
78+
$options = new ChallengeOptions(
79+
$hmacKey,
80+
ChallengeOptions::DEFAULT_ALGORITHM,
81+
ChallengeOptions::DEFAULT_MAX_NUMBER,
82+
(new \DateTimeImmutable())->add(new \DateInterval('PT10S')),
83+
['query_param' => '123'],
84+
ChallengeOptions::DEFAULT_SALT_LENGTH
85+
]);
86+
```
8587

86-
### `Altcha::verifySolution(array $payload, string $hmacKey, bool $checkExpires): bool`
88+
### `Altcha::verifySolution(array|string $payload, string $hmacKey, bool $checkExpires): bool`
8789

8890
Verifies an ALTCHA solution.
8991

9092
**Parameters:**
9193

92-
- `payload array`: The solution payload to verify.
94+
- `data array|string`: The solution payload to verify.
9395
- `hmacKey string`: The HMAC key used for verification.
9496
- `checkExpires bool`: Whether to check if the challenge has expired.
9597

9698
**Returns:** `bool`
9799

98-
### `Altcha::extractParams(array $payload): array`
99-
100-
Extracts URL parameters from the payload's salt.
101-
102-
**Parameters:**
103-
104-
- `payload array`: The payload containing the salt.
105-
106-
**Returns:** `array`
107-
108100
### `Altcha::verifyFieldsHash(array $formData, array $fields, string $fieldsHash, string $algorithm): bool`
109101

110102
Verifies the hash of form fields.
@@ -118,18 +110,18 @@ Verifies the hash of form fields.
118110

119111
**Returns:** `bool`
120112

121-
### `Altcha::verifyServerSignature($payload, string $hmacKey): array`
113+
### `Altcha::verifyServerSignature(array|string $payload, string $hmacKey): ServerSignatureVerification`
122114

123115
Verifies the server signature.
124116

125117
**Parameters:**
126118

127-
- `payload mixed`: The payload to verify (string or `ServerSignaturePayload` array).
119+
- `data array|string`: The payload to verify (string or `ServerSignaturePayload` array).
128120
- `hmacKey string`: The HMAC key used for verification.
129121

130-
**Returns:** `array`
122+
**Returns:** `ServerSignatureVerification`
131123

132-
### `Altcha::solveChallenge(string $challenge, string $salt, string $algorithm, int $max, int $start, $stopChan = null): array`
124+
### `Altcha::solveChallenge(string $challenge, string $salt, string $algorithm, int $max, int $start = 0): array`
133125

134126
Finds a solution to the given challenge.
135127

@@ -141,7 +133,7 @@ Finds a solution to the given challenge.
141133
- `max int`: Maximum number to iterate to.
142134
- `start int`: Starting number.
143135

144-
**Returns:** `array`
136+
**Returns:** `null|Solution`
145137

146138

147139
## Tests
@@ -152,4 +144,4 @@ vendor/bin/phpunit --bootstrap src/Altcha.php tests/AltchaTest.php
152144

153145
## License
154146

155-
MIT
147+
MIT

0 commit comments

Comments
 (0)