-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
64 lines (52 loc) · 1.33 KB
/
main.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
package main
import (
"crypto/rand"
"fmt"
"math/big"
"os"
"secret-generator/flags"
"secret-generator/writer"
"strings"
)
func main() {
f := flags.GetFlags()
if !strings.HasSuffix(f.SecretsPath, ".bin") {
println("secrets filename extension should be .bin")
return
}
if f.SecretSize == 0 {
println("Secret size should be more than 0")
return
}
if f.SecretAmount == 0 {
println("Amount of secrets should be more than 0")
return
}
file, err := os.Create(f.SecretsPath)
if err != nil {
fmt.Println("Error creating file:", err)
return
}
defer file.Close()
fmt.Printf("Started generating %d secrets of size %d into %s\n", f.SecretAmount, f.SecretSize, f.SecretsPath)
err = writer.WriteNumberOfSecrets(file, f.SecretAmount)
if err != nil {
fmt.Printf("Failed to write number of secrets: %v\n", err)
return
}
for i := 0; i < f.SecretAmount; i++ {
secret := new(big.Int).Lsh(big.NewInt(1), uint(f.SecretSize))
// Generate a random big.Int between 0 and max-1
n, err := rand.Int(rand.Reader, secret)
if err != nil {
fmt.Println("Failed to generate a random number:", err)
return
}
err = writer.WriteSecret(file, n)
if err != nil {
fmt.Printf("Failed to write a secret: %v\n", err)
return
}
}
fmt.Printf("%d secrets of size %d successfullt generated!\n", f.SecretAmount, f.SecretSize)
}