-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
93 lines (76 loc) · 2.3 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
package main
import (
"fmt"
"os"
"github.com/KalimaraPeleteiro/CLI-FileEncryption/criptography"
"golang.org/x/term"
)
func main() {
if len(os.Args) < 2 {
helpMessage()
os.Exit(0)
}
action := os.Args[1]
switch action {
case "help":
helpMessage()
case "encrypt":
encryptHandler()
case "decrypt":
decryptHandler()
default:
fmt.Println("Use encrypt para criptografar um arquivo, ou decrypt para descriptografá-lo.")
}
}
func helpMessage() {
fmt.Println("Programa de Encriptação")
fmt.Println("Use para criptografar arquivos cujo conteúdo deseja manter secreto, e acessar somente uma senha prévia.")
fmt.Println("")
fmt.Println("Como Usar?")
fmt.Println("go run . encrypt caminho/para/o/arquivo")
fmt.Println("")
fmt.Println("Comandos Disponíveis")
fmt.Println("encrypt \t Para criptografar arquivos (é necessário passar uma senha).")
fmt.Println("decrypt \t Para descriptografar arquivos com base na senha entregue.")
fmt.Println("help \t Para obter informações sobre o programa (é esse texto).")
fmt.Println("")
}
func encryptHandler() {
if len(os.Args) < 3 {
fmt.Println("Você não passou o caminho para o arquivo. Para entender melhor o funcionamento do programa, tente [go run . help]")
os.Exit(0)
}
file := os.Args[2]
if !isFileValid(file) {
panic("Não pude encontrar o arquivo.")
}
password := getPassword()
fmt.Println("\nCriptografando seu arquivo...")
criptography.Encrypt(file, password)
fmt.Println("\nArquivo seguro!")
}
func decryptHandler() {
if len(os.Args) < 3 {
fmt.Println("Você não passou o caminho para o arquivo. Para entender melhor o funcionamento do programa, tente [go run . help]")
os.Exit(0)
}
file := os.Args[2]
if !isFileValid(file) {
panic("Não pude encontrar o arquivo.")
}
fmt.Print("Passe a senha do arquivo: ")
password, _ := term.ReadPassword(0)
fmt.Println("\nTentando descriptografar o arquivo...")
criptography.Decrypt(file, password)
fmt.Println("\nArquivo Descriptografado!")
}
func getPassword() []byte {
fmt.Print("Insira a senha: ")
password, _ := term.ReadPassword(0)
fmt.Print("\nConfirme sua senha: ")
passwordConfirmation, _ := term.ReadPassword(0)
if !isPasswordValid(password, passwordConfirmation) {
fmt.Print("\nVocê confirmou com uma senha diferente. Talvez escreveu errado?\n")
}
return password
}