-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
80 lines (64 loc) · 1.85 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
package main
import (
"github.com/OlegChuev/encryptor/pkg/encryption"
"github.com/OlegChuev/encryptor/pkg/filesystem"
"flag"
"fmt"
"io/ioutil"
)
func main() {
encryptFlag, decryptFlag, sourceFlag, destFlag, keyFlag, helpFlag := parseFlags()
if *helpFlag {
flag.PrintDefaults()
return
}
if *encryptFlag && *decryptFlag {
fmt.Println("Please specify either -encrypt or -decrypt, not both.")
return
}
if *sourceFlag == "" || *destFlag == "" {
fmt.Println("Please provide both source and destination file paths.")
return
}
data, err := ioutil.ReadFile(*sourceFlag)
if err != nil {
fmt.Println("Error reading source file:", err)
return
}
result, err := process(data, *encryptFlag, *keyFlag)
if err != nil {
var processName string
if *encryptFlag {
processName = "encryption"
} else {
processName = "decryption"
}
errMsg := fmt.Sprintf("Error during the %s process:", processName)
fmt.Println(errMsg, err)
return
}
err = filesystem.WriteToFile(*destFlag, result)
if err != nil {
fmt.Println("Error writing to destination file:", err)
return
}
fmt.Println("Operation completed successfully.")
}
func parseFlags() (*bool, *bool, *string, *string, *string, *bool) {
encryptFlag := flag.Bool("encrypt", false, "Encrypts data")
decryptFlag := flag.Bool("decrypt", false, "Decrypts data")
sourceFlag := flag.String("src", "", "Source file")
destFlag := flag.String("dest", "", "Destination file")
keyFlag := flag.String("key", "", "Private key string")
helpFlag := flag.Bool("help", false, "Print this help message")
flag.Parse()
return encryptFlag, decryptFlag, sourceFlag, destFlag, keyFlag, helpFlag
}
func process(data []byte, isEncryption bool, key string) (result []byte, err error) {
if isEncryption {
result, err = encryption.Encrypt(data, key)
} else {
result, err = encryption.Decrypt(data, key)
}
return
}