-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchallenge.go
43 lines (34 loc) · 1.17 KB
/
challenge.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
// @author: Brian Wojtczak
// @copyright: 2024 by Brian Wojtczak
// @license: BSD-style license found in the LICENSE file
package altcha
// NewChallenge creates a new challenge with default parameters.
func NewChallenge() (msg Message) {
return NewChallengeWithParams(Parameters{})
}
// NewChallengeEncoded creates a new challenge with default parameters and
// encoded for the client.
func NewChallengeEncoded() string {
// Create a new challenge message.
msg := NewChallengeWithParams(Parameters{})
// Return the encoded challenge message.
return msg.Encode()
}
// NewChallengeWithParams creates a new challenge with the given parameters.
func NewChallengeWithParams(params Parameters) (msg Message) {
// Populate any missing parameters.
params.populate()
// Generate the challenge and signature.
algo, _ := AlgorithmFromString(params.Algorithm)
challenge := generateHash(algo, params.Salt, params.Number)
signature := Sign(algo, challenge)
msg = Message{
Algorithm: params.Algorithm,
Salt: params.Salt,
Challenge: challenge,
Signature: signature,
// Number is a secret and must not be exposed to the client.
}
// Return the challenge message.
return msg
}