-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparameters.go
59 lines (46 loc) · 1.77 KB
/
parameters.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// @author: Brian Wojtczak
// @copyright: 2024 by Brian Wojtczak
// @license: BSD-style license found in the LICENSE file
package altcha
// MinimumComplexity is the minimum complexity allowed.
// @see https://altcha.org/docs/complexity
const MinimumComplexity = 1000
// DefaultComplexity is the default complexity used. This should be increased
// to make the challenge harder to solve.
// @see https://altcha.org/docs/complexity
const DefaultComplexity = 100000
// Parameters are the parameters used to generate a challenge. If any of the
// parameters are missing, they will be generated.
type Parameters struct {
// Algorithm is the hashing algorithm used to generate the challenge.
// Supported algorithms are SHA-256, SHA-384, and SHA-512.
Algorithm string `json:"algorithm"`
// Salt is a random string used to generate the challenge.
// The minimum length is 10 characters.
Salt string `json:"salt"`
// Complexity is the number of iterations used to generate the challenge.
// This is only considered when Number is not provided.
Complexity int `json:"complexity,omitempty"`
// Number is the secret number which the client must solve for.
Number int `json:"number,omitempty"`
}
// Populate generates any missing parameters.
func (params *Parameters) populate() {
// Without an algorithm, we use SHA-256.
algo, ok := AlgorithmFromString(params.Algorithm)
if !ok {
algo = SHA256
params.Algorithm = algo.String()
}
// Without salt, we generate a new one.
if len(params.Salt) < 10 {
params.Salt = randomString(16)
}
// Without a number, we use the complexity to generate a new one.
if params.Number <= 0 {
if params.Complexity <= MinimumComplexity {
params.Complexity = DefaultComplexity
}
params.Number = randomInt(MinimumComplexity, params.Complexity)
}
}