-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
135 lines (121 loc) · 3.23 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
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
package main
import (
"bytes"
"fmt"
"os"
"github.com/Abhinandan-Khurana/EncryptGuard/filecrypt"
"github.com/fatih/color"
)
func main() {
if len(os.Args) < 2 {
printHelp()
os.Exit(0)
}
function := os.Args[1]
switch function {
case "help":
printHelp()
case "encrypt":
encryptHandle()
case "decrypt":
decryptHandle()
default:
fmt.Println("Run encrypt to encrypt a file, and decrypt tp decrypt a file.")
os.Exit(1)
}
}
func printHelp() {
magenta := color.New(color.FgMagenta).Add(color.Bold)
blue := color.New(color.FgCyan).Add(color.Bold)
green := color.New(color.FgGreen).Add(color.Bold)
yellow := color.New(color.FgYellow).Add(color.Bold)
fmt.Println("")
magenta.Println(`
____ _ _ ____ ____ _ _ ___ ___ ____ _ _ ____ ____ ___
|___ |\ | | |__/ \_/ |__] | | __ | | |__| |__/ | \
|___ | \| |___ | \ | | | |__] |__| | | | \ |__/
`)
fmt.Println("")
blue.Println("Usage:")
fmt.Println("")
yellow.Println("\t EncryptGuard encrypt /path/to/your.file")
fmt.Println("")
green.Println("Commands:")
fmt.Println("")
fmt.Println("\tencrypt:\tEncrypt a file given a password")
fmt.Println("\tdecrypt:\tDecrypt a file using a password")
fmt.Println("\thelp:\t\tDisplays help description")
fmt.Println("")
}
func encryptHandle() {
magenta := color.New(color.FgMagenta).Add(color.Bold)
green := color.New(color.FgGreen).Add(color.Bold)
if len(os.Args) < 3 {
println("missing the path to the file. For more info, run --> go run . help")
os.Exit(0)
}
file := os.Args[2]
if !validateFile(file) {
panic("File not found!")
}
password := getPassword()
magenta.Println("\nEncrypting...")
filecrypt.Encrypt(file, password)
green.Println("\n File successfully protected!")
}
func decryptHandle() {
green := color.New(color.FgGreen).Add(color.Bold)
magenta := color.New(color.FgMagenta).Add(color.Bold)
if len(os.Args) < 3 {
println("missing the path to the file. For more info, run --> go run . help")
os.Exit(0)
}
file := os.Args[2]
if !validateFile(file) {
panic("File not found!")
}
fmt.Println("Enter Password:")
password, _ := readPassword()
magenta.Println("\nDecrypting...")
filecrypt.Decrypt(file, password)
green.Println("\n File successfully decrypted!")
}
func getPassword() []byte {
yellow := color.New(color.FgYellow).Add(color.Bold)
red := color.New(color.FgRed).Add(color.Bold)
fmt.Print("Set Password:")
password, err := readPassword()
if err != nil {
red.Println("\nFailed to read password. Error:", err)
os.Exit(1)
}
fmt.Print("\nConfirm Password: ")
password2, err := readPassword()
if err != nil {
red.Println("\nFailed to read password. Error:", err)
os.Exit(1)
}
if !validatePassword(password, password2) {
yellow.Print("\nPasswords do not match, try again!\n")
return getPassword()
}
return password
}
// func readPassword() ([]byte, error) {
// if runtime.GOOS == "windows" {
// return term.ReadPassword(int(windows.Handle(os.Stdin.Fd())))
// }
// return term.ReadPassword(int(syscall.Stdin))
// }
func validatePassword(password1 []byte, password2 []byte) bool {
if !bytes.Equal(password1, password2) {
return false
}
return true
}
func validateFile(file string) bool {
if _, err := os.Stat(file); os.IsNotExist(err) {
return false
}
return true
}