-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathget_api.go
73 lines (67 loc) · 1.64 KB
/
get_api.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
package main
import (
"fmt"
"os"
"time"
)
// get new API key
func getAPIKey(promptForNewKey bool) string {
if promptForNewKey {
clearScreen()
for {
fmt.Println("Enter your API key from hashes.com/profile")
var newAPIKey string
fmt.Scanln(&newAPIKey)
if verifyAPIKey(newAPIKey) {
encryptedKey, err := encrypt([]byte(newAPIKey), encryptionKey)
if err != nil {
fmt.Printf("Error encrypting API key: %v\n", err)
continue
}
err = os.WriteFile(apiKeyFile, encryptedKey, 0644)
if err != nil {
fmt.Printf("Error writing encrypted API key to file: %v\n", err)
continue
}
return newAPIKey
} else {
fmt.Println("API key not verified, try again.")
time.Sleep(500 * time.Millisecond)
}
}
}
for {
var apiKey string
if _, err := os.Stat(apiKeyFile); err == nil {
encryptedKey, err := os.ReadFile(apiKeyFile)
if err != nil {
fmt.Printf("Error reading API key file: %v\n", err)
} else {
decryptedKey, err := decrypt(encryptedKey, encryptionKey)
if err != nil {
fmt.Printf("Error decrypting API key: %v\n", err)
} else {
apiKey = string(decryptedKey)
if verifyAPIKey(apiKey) {
return apiKey
}
}
}
}
fmt.Println("Enter your API key from hashes.com/profile")
fmt.Scanln(&apiKey)
encryptedKey, err := encrypt([]byte(apiKey), encryptionKey)
if err != nil {
fmt.Printf("Error encrypting API key: %v\n", err)
continue
}
err = os.WriteFile(apiKeyFile, encryptedKey, 0644)
if err != nil {
fmt.Printf("Error writing encrypted API key to file: %v\n", err)
continue
}
if verifyAPIKey(apiKey) {
return apiKey
}
}
}