-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathcryptogo.go
120 lines (94 loc) · 2.29 KB
/
cryptogo.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
package main
import (
"bytes"
"fmt"
"github.com/isfonzar/filecrypt"
"golang.org/x/crypto/ssh/terminal"
"os"
)
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")
}
password := getPassword()
fmt.Println("\nEncrypting...")
filecrypt.Encrypt(file, password)
fmt.Println("\nFile successfully protected")
}
func getPassword() []byte {
fmt.Print("Enter password: ")
password, _ := terminal.ReadPassword(0)
fmt.Print("\nConfirm password: ")
password2, _ := terminal.ReadPassword(0)
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")
}
fmt.Print("Enter password: ")
password, _ := terminal.ReadPassword(0)
fmt.Println("\nDecrypting...")
filecrypt.Decrypt(file, password)
fmt.Println("\nFile successfully decrypted.")
}
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
}