-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
129 lines (104 loc) · 2.61 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
package main
import (
"bytes"
"fmt"
"os"
"github.com/sachinandan-05/go-file-encryption/filecrypt"
"golang.org/x/term"
)
func main() {
// If not enough args, return help text
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 CryptoGo encrypt to encrypt a file, and CryptoGo decrypt to decrypt a file.")
os.Exit(1)
}
}
func printHelp() {
fmt.Println("CryptoGo")
fmt.Println("Simple file encrypter for your day-to-day needs.")
fmt.Println("")
fmt.Println("Usage:")
fmt.Println("")
fmt.Println("\tCryptoGo encrypt /path/to/your/file")
fmt.Println("")
fmt.Println("Commands:")
fmt.Println("")
fmt.Println("\t encrypt\tEncrypts a file given a password")
fmt.Println("\t decrypt\tTries to decrypt a file using a password")
fmt.Println("\t help\t\tDisplays help text")
fmt.Println("")
}
func encryptHandle() {
if len(os.Args) < 3 {
println("Missing the path to the file. For more information run CryptoGo help")
os.Exit(0)
}
file := os.Args[2]
if !validateFile(file) {
panic("File not found or is a directory")
}
password := getPassword()
fmt.Println("\nEncrypting...")
filecrypt.Encrypt(file, password)
fmt.Println("\nFile successfully protected")
}
func getPassword() []byte {
fmt.Print("Enter password: ")
password, err := term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
fmt.Println("\nError reading password:", err)
os.Exit(1)
}
fmt.Print("\nConfirm password: ")
password2, err := term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
fmt.Println("\nError reading password:", err)
os.Exit(1)
}
if !validatePassword(password, password2) {
fmt.Print("\nPasswords do not match. Please try again.\n")
return getPassword()
}
return password
}
func decryptHandle() {
if len(os.Args) < 3 {
println("Missing the path to the file. For more information run CryptoGo help")
os.Exit(0)
}
file := os.Args[2]
if !validateFile(file) {
panic("File not found or is a directory")
}
fmt.Print("Enter password: ")
password, err := term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
fmt.Println("\nError reading password:", err)
os.Exit(1)
}
fmt.Println("\nDecrypting...")
filecrypt.Decrypt(file, password)
fmt.Println("\nFile successfully decrypted.")
}
func validatePassword(password1 []byte, password2 []byte) bool {
return bytes.Equal(password1, password2)
}
func validateFile(file string) bool {
info, err := os.Stat(file)
if os.IsNotExist(err) || info.IsDir() {
return false
}
return true
}