-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
109 lines (89 loc) · 2.17 KB
/
main.go
File metadata and controls
109 lines (89 loc) · 2.17 KB
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
package main
import (
"bytes"
"fmt"
"os"
"github.com/AmulyaParitosh/GoCrypt/filecrypt"
"golang.org/x/term"
)
func main() {
if len(os.Args) < 2 {
printHelp()
os.Exit(0)
}
function := os.Args[1]
switch function {
case "help":
printHelp()
case "encrypt":
encryptHandler()
case "decrypt":
decryptHandler()
default:
fmt.Println("Run encrypt to encrypt a file, nad decrypt to decrypt a file.")
}
}
func printHelp() {
fmt.Println("GoCrypt")
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 {
println("missing the path to the file. Run help for more info.")
os.Exit(0)
}
filePath := os.Args[2]
if !validateFile(filePath) {
panic("File not found")
}
password := getPassword()
fmt.Println("\nEncrypting...")
filecrypt.Encrypt(filePath, password)
fmt.Println("File successfully encrypted")
}
func decryptHandler() {
if len(os.Args) < 3 {
println("missing the path to the file. Run help for more info.")
os.Exit(0)
}
filePath := os.Args[2]
if !validateFile(filePath) {
panic("File not found")
}
fmt.Print("Enter password: ")
password, _ := term.ReadPassword(0)
fmt.Println("\nDecrypting...")
filecrypt.Decrypt(filePath, password)
fmt.Println("File successfully decrypted.")
}
func getPassword() []byte {
fmt.Print("Enter password: ")
password, _ := term.ReadPassword(0)
fmt.Print("\nConfirm Password: ")
password2, _ := term.ReadPassword(0)
if !validatePassword(password, password2) {
fmt.Println("\nPasswords do not match. Please try again")
return getPassword()
}
return password
}
func validatePassword(password1 []byte, password2 []byte) bool {
return bytes.Equal(password1, password2)
}
func validateFile(filePath string) bool {
if _, err := os.Stat(filePath); os.IsNotExist(err) {
return false
}
return true
}