-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
110 lines (99 loc) · 2.12 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
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
package main
import (
"bufio"
"fmt"
"github.com/TwoWaySix/enigma/internal"
"github.com/TwoWaySix/enigma/internal/decryption"
"github.com/TwoWaySix/enigma/internal/encryption"
"os"
"strings"
)
func main() {
fmt.Println("Starting ENIGMA")
var err error
if len(os.Args) > 1 {
err = normalMode()
} else {
err = simpleDecryptMode()
}
if err != nil {
fmt.Printf("ERROR: %v\n", err)
}
}
func simpleDecryptMode() error {
fmt.Println("Starting simple decrypt mode.")
fmt.Println("Looking for files with '.roll' extension.")
dirEntries, err := os.ReadDir(".")
if err != nil {
return err
}
for _, entry := range dirEntries {
if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".roll") {
continue
}
err := simpleDecryptFile(entry)
if err != nil {
fmt.Printf("ERROR: %v", err)
continue
}
}
return nil
}
func simpleDecryptFile(entry os.DirEntry) error {
fmt.Printf("Decrypting file %s\n", entry.Name())
fmt.Printf("Please enter encryption key: ")
reader := bufio.NewReader(os.Stdin)
key, err := reader.ReadString('\n')
if err != nil {
return fmt.Errorf("decrypting file '%s': %w", entry.Name(), err)
}
config := internal.Config{
Mode: "unroll",
InputPath: entry.Name(),
OutputPath: ".",
Key: key,
}
err = startUnrollJob(config)
if err != nil {
return fmt.Errorf("decrypting file '%s': %w", entry.Name(), err)
}
return nil
}
func normalMode() error {
config, err := internal.NewConfigFromFlags()
if err != nil {
return err
}
switch config.Mode {
case "roll":
err = startRollJob(config)
case "unroll":
err = startUnrollJob(config)
}
if err != nil {
return err
}
return nil
}
func startRollJob(config internal.Config) error {
job, err := encryption.NewJob(config)
if err != nil {
return fmt.Errorf("roll job: %w", err)
}
err = job.Start()
if err != nil {
return fmt.Errorf("roll job: %w", err)
}
return nil
}
func startUnrollJob(config internal.Config) error {
job, err := decryption.NewJob(config)
if err != nil {
return fmt.Errorf("unroll job: %w", err)
}
err = job.Start()
if err != nil {
return fmt.Errorf("unroll job: %w", err)
}
return nil
}