-
Notifications
You must be signed in to change notification settings - Fork 0
/
altcha.go
402 lines (359 loc) · 10.2 KB
/
altcha.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
package altcha
import (
"crypto/hmac"
"crypto/rand"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"math/big"
"net/url"
"strconv"
"strings"
"time"
)
// Algorithm type definition
type Algorithm string
const (
SHA1 Algorithm = "SHA-1"
SHA256 Algorithm = "SHA-256"
SHA512 Algorithm = "SHA-512"
)
const (
DefaultMaxNumber int64 = 1e6
DefaultSaltLength int = 12
DefaultAlgorithm = SHA256
)
// ChallengeOptions defines the options for creating a challenge
type ChallengeOptions struct {
Algorithm Algorithm
MaxNumber int64
SaltLength int
HMACKey string
Salt string
Number int64
Expires *time.Time
Params url.Values
}
// Challenge represents a challenge for the client to solve
type Challenge struct {
Algorithm string `json:"algorithm"`
Challenge string `json:"challenge"`
MaxNumber int64 `json:"maxNumber"`
Salt string `json:"salt"`
Signature string `json:"signature"`
}
// Payload represents a solution to a Challenge
type Payload struct {
Algorithm string `json:"algorithm"`
Challenge string `json:"challenge"`
Number int64 `json:"number"`
Salt string `json:"salt"`
Signature string `json:"signature"`
}
// ServerSignaturePayload represents the structure of the payload for server signature verification
type ServerSignaturePayload struct {
Algorithm Algorithm `json:"algorithm"`
VerificationData string `json:"verificationData"`
Signature string `json:"signature"`
Verified bool `json:"verified"`
}
// ServerSignatureVerificationData represents the extracted verification data
type ServerSignatureVerificationData struct {
Classification string `json:"classification"`
Country string `json:"country"`
DetectedLanguage string `json:"detectedLanguage"`
Email string `json:"email"`
Expire int64 `json:"expire"`
Fields []string `json:"fields"`
FieldsHash string `json:"fieldsHash"`
IpAddress string `json:"ipAddress"`
Reasons []string `json:"reasons"`
Score float64 `json:"score"`
Time int64 `json:"time"`
Verified bool `json:"verified"`
}
// Solution holds the result of solving a challenge.
type Solution struct {
Number int
Took time.Duration
}
// Generates a random byte array of the specified length
func randomBytes(length int) ([]byte, error) {
bytes := make([]byte, length)
_, err := rand.Read(bytes)
return bytes, err
}
// Generates a random integer between 0 and max (inclusive)
func randomInt(max int64) (int64, error) {
n, err := rand.Int(rand.Reader, big.NewInt(max+1))
if err != nil {
return 0, err
}
return n.Int64(), nil
}
// Hashes the input data using the specified algorithm and returns the hexadecimal representation of the hash
func hashHex(algorithm Algorithm, data string) (string, error) {
hash, err := hash(algorithm, []byte(data))
if err != nil {
return "", err
}
return hex.EncodeToString(hash), nil
}
// Hashes the input data using the specified algorithm
func hash(algorithm Algorithm, data []byte) ([]byte, error) {
var hash []byte
switch algorithm {
case SHA1:
h := sha1.New()
h.Write([]byte(data))
hash = h.Sum(nil)
case SHA256:
h := sha256.New()
h.Write([]byte(data))
hash = h.Sum(nil)
case SHA512:
h := sha512.New()
h.Write([]byte(data))
hash = h.Sum(nil)
default:
return nil, fmt.Errorf("unsupported algorithm: %s", algorithm)
}
return hash, nil
}
// Computes the HMAC of the input data using the specified algorithm and key, and returns the hexadecimal representation
func hmacHex(algorithm Algorithm, data []byte, key string) (string, error) {
h, err := hmacHash(algorithm, []byte(data), key)
if err != nil {
return "", err
}
return hex.EncodeToString(h), nil
}
// Computes the HMAC of the input data using the specified algorithm and key
func hmacHash(algorithm Algorithm, data []byte, key string) ([]byte, error) {
var hash []byte
switch algorithm {
case SHA1:
h := hmac.New(sha1.New, []byte(key))
h.Write(data)
hash = h.Sum(nil)
case SHA256:
h := hmac.New(sha256.New, []byte(key))
h.Write(data)
hash = h.Sum(nil)
case SHA512:
h := hmac.New(sha512.New, []byte(key))
h.Write(data)
hash = h.Sum(nil)
default:
return nil, fmt.Errorf("unsupported algorithm: %s", algorithm)
}
return hash, nil
}
// Creates a challenge for the client to solve
func CreateChallenge(options ChallengeOptions) (Challenge, error) {
algorithm := options.Algorithm
if algorithm == "" {
algorithm = DefaultAlgorithm
}
maxNumber := options.MaxNumber
if maxNumber == 0 {
maxNumber = DefaultMaxNumber
}
saltLength := options.SaltLength
if saltLength == 0 {
saltLength = DefaultSaltLength
}
if options.Params == nil {
options.Params = url.Values{}
}
params := options.Params
if options.Expires != nil {
params.Set("expires", fmt.Sprintf("%d", options.Expires.Unix()))
}
salt := options.Salt
if salt == "" {
randomSalt, err := randomBytes(saltLength)
if err != nil {
return Challenge{}, err
}
salt = hex.EncodeToString(randomSalt)
}
if len(params) > 0 {
salt = salt + "?" + params.Encode()
}
number := options.Number
if number == 0 {
randomNumber, err := randomInt(maxNumber)
if err != nil {
return Challenge{}, err
}
number = randomNumber
}
challenge, err := hashHex(algorithm, salt+fmt.Sprint(number))
if err != nil {
return Challenge{}, err
}
signature, err := hmacHex(algorithm, []byte(challenge), options.HMACKey)
if err != nil {
return Challenge{}, err
}
return Challenge{
Algorithm: string(algorithm),
Challenge: challenge,
MaxNumber: maxNumber,
Salt: salt,
Signature: signature,
}, nil
}
// Verifies the solution provided by the client
func VerifySolution(payload interface{}, hmacKey string, checkExpires bool) (bool, error) {
var parsedPayload Payload
// Parse payload
switch v := payload.(type) {
case string:
decoded, err := base64.StdEncoding.DecodeString(v)
if err != nil {
return false, err
}
err = json.Unmarshal(decoded, &parsedPayload)
if err != nil {
return false, err
}
default:
parsedPayload, _ = v.(Payload)
}
params := ExtractParams(parsedPayload)
expires := params.Get("expires")
if checkExpires && expires != "" {
expireTime, err := strconv.ParseInt(expires, 10, 64)
if err != nil {
return false, err
}
if time.Now().Unix() > expireTime {
return false, nil
}
}
challengeOptions := ChallengeOptions{
Algorithm: Algorithm(parsedPayload.Algorithm),
HMACKey: hmacKey,
Number: parsedPayload.Number,
Salt: parsedPayload.Salt,
}
expectedChallenge, err := CreateChallenge(challengeOptions)
if err != nil {
return false, err
}
return expectedChallenge.Challenge == parsedPayload.Challenge && expectedChallenge.Signature == parsedPayload.Signature, nil
}
// Extracts parameters from the payload
func ExtractParams(payload Payload) url.Values {
splitSalt := strings.Split(payload.Salt, "?")
if len(splitSalt) > 1 {
params, _ := url.ParseQuery(splitSalt[1])
return params
}
return url.Values{}
}
// Verifies the hash of form fields
func VerifyFieldsHash(formData map[string][]string, fields []string, fieldsHash string, algorithm Algorithm) (bool, error) {
var lines []string
for _, field := range fields {
if value, exists := formData[field]; exists && len(value) > 0 {
lines = append(lines, value[0])
} else {
lines = append(lines, "")
}
}
joinedData := strings.Join(lines, "\n")
computedHash, err := hashHex(algorithm, joinedData)
if err != nil {
return false, err
}
return computedHash == fieldsHash, nil
}
// VerifyServerSignature verifies the server's signature
func VerifyServerSignature(payload interface{}, hmacKey string) (bool, ServerSignatureVerificationData, error) {
var parsedPayload ServerSignaturePayload
// Parse payload
switch v := payload.(type) {
case string:
decoded, err := base64.StdEncoding.DecodeString(v)
if err != nil {
return false, ServerSignatureVerificationData{}, err
}
err = json.Unmarshal(decoded, &parsedPayload)
if err != nil {
return false, ServerSignatureVerificationData{}, err
}
default:
parsedPayload, _ = v.(ServerSignaturePayload)
}
// Calculate expected signature
hash, err := hash(parsedPayload.Algorithm, []byte(parsedPayload.VerificationData))
if err != nil {
return false, ServerSignatureVerificationData{}, err
}
expectedSignature, err := hmacHex(parsedPayload.Algorithm, hash, hmacKey)
if err != nil {
return false, ServerSignatureVerificationData{}, err
}
// Extract verification data
var verificationData ServerSignatureVerificationData
params, err := url.ParseQuery(parsedPayload.VerificationData)
if err == nil {
verificationData.Classification = params.Get("classification")
verificationData.Country = params.Get("country")
verificationData.DetectedLanguage = params.Get("detectedLanguage")
verificationData.Email = params.Get("email")
verificationData.Expire, _ = strconv.ParseInt(params.Get("expire"), 10, 64)
verificationData.Fields = strings.Split(params.Get("fields"), ",")
verificationData.Reasons = strings.Split(params.Get("reasons"), ",")
verificationData.Score, _ = strconv.ParseFloat(params.Get("score"), 64)
verificationData.Time, _ = strconv.ParseInt(params.Get("time"), 10, 64)
verificationData.Verified = params.Get("verified") == "true"
}
// Verify the signature
now := time.Now().Unix()
isVerified := parsedPayload.Verified &&
verificationData.Verified &&
verificationData.Expire > now &&
parsedPayload.Signature == expectedSignature
return isVerified, verificationData, nil
}
// SolveChallenge solves a challenge
func SolveChallenge(challenge string, salt string, algorithm Algorithm, max int, start int, stopChan <-chan struct{}) (*Solution, error) {
if algorithm == "" {
algorithm = "SHA-256"
}
if max <= 0 {
max = 1000000
}
if start < 0 {
start = 0
}
startTime := time.Now()
for n := start; n <= max; n++ {
select {
case <-stopChan:
// Stop the process if the stop signal is received.
return nil, nil
default:
// Continue the process.
}
hash, err := hashHex(algorithm, salt+fmt.Sprint(n))
if err != nil {
return nil, err
}
if hash == challenge {
return &Solution{
Number: n,
Took: time.Since(startTime),
}, nil
}
}
return nil, nil
}