-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
123 lines (98 loc) · 2.48 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
package main
import (
"bytes"
"fmt"
"os"
"github.com/amolpratap-singh/file-encrypter/filecrypt"
"github.com/amolpratap-singh/file-encrypter/logger"
"golang.org/x/term"
)
func main() {
logger.Info("Go CLI Tool for file encryption and decryption")
if len(os.Args) < 2 {
helper()
os.Exit(0)
}
action := os.Args[1]
switch action {
case "help":
helper()
case "encrypt":
encryptHandler()
case "decrypt":
decryptHandler()
default:
logger.Error("Execute EncrypterCLI to encrypt or decrypt a file")
os.Exit(1)
}
}
func helper() {
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 encryptHandler() {
if len(os.Args) < 3 {
logger.Info("Missing the path to the file. For more information run CryptoGo help")
os.Exit(0)
}
file := os.Args[2]
if !validateFile(file) {
logger.Error("File not found")
panic("File not found")
}
fmt.Println("Enter Password: ")
password := getPassword()
logger.Info("Encrytping the file ...")
//logger.Log.Info().Msgf("%v",password)
filecrypt.Encrypt(file, password)
logger.Info("File encrypted succesfully ...")
}
func decryptHandler() {
if len(os.Args) < 3 {
logger.Error("Missing the path to the file. For more information run EncrypterCLI help")
os.Exit(0)
}
file := os.Args[2]
if !validateFile(file) {
logger.Error("File not found")
panic("File not found")
}
fmt.Println("Enter Password: ")
password := getPassword()
logger.Info("Decrytping the file ...")
filecrypt.Decrypt(file, password)
logger.Info("File succesfully decrypted ...")
}
func validateFile(fileName string) bool {
if _, err := os.Stat(fileName); os.IsNotExist(err) {
return false
}
return true
}
func getPassword() []byte {
password, _ := term.ReadPassword(0)
fmt.Println("Confirm password: ")
confirmPassword, _ := term.ReadPassword(0)
if !validatePassword(password, confirmPassword) {
logger.Error("Passwords do not match. Please try again.")
return getPassword()
}
return password
}
func validatePassword(password []byte, confirmPassword []byte) bool {
if !bytes.Equal(password, confirmPassword) {
return false
}
return true
}