-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
90 lines (82 loc) · 1.74 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
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"github.com/pierods/mgade/decrypt"
"golang.org/x/crypto/ssh/terminal"
"io/ioutil"
"os"
"syscall"
)
var helpFlag = flag.Bool("help", false, "")
func main() {
flag.Parse()
if *helpFlag {
fmt.Println("Usage: mgade encryptedfile")
fmt.Println("or mgade encryptedfile clearfile")
return
}
inFile := ""
outFile := ""
switch len(os.Args) {
case 1:
fmt.Print("Enter encrypted file name/path: ")
consoleReader := bufio.NewScanner(os.Stdin)
consoleReader.Scan()
inFile = consoleReader.Text()
case 2:
inFile = os.Args[1]
fmt.Println("Reading encrypted file " + inFile)
case 3:
inFile = os.Args[1]
fmt.Println("Reading encrypted file " + inFile)
outFile = os.Args[2]
fmt.Println("Writing to clear file " + outFile)
}
password := getPassword()
inData, err := ioutil.ReadFile(inFile)
if err != nil {
fmt.Println(err)
os.Exit(3)
}
clearData, err := decrypt.Open(inData, password)
if err != nil {
fmt.Println(err)
os.Exit(4)
}
if outFile != "" {
err = ioutil.WriteFile(outFile, clearData, 0644)
if err != nil {
fmt.Println(err)
os.Exit(5)
}
} else {
fmt.Println(string(clearData))
}
}
func getPassword() []byte {
fmt.Print("Enter password: ")
password, err := terminal.ReadPassword(int(syscall.Stdin))
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
if len(password) == 0 {
fmt.Println("Empty password. Exiting.")
os.Exit(2)
}
fmt.Print("\nConfirm password: ")
passwordConfirm, err := terminal.ReadPassword(int(syscall.Stdin))
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
if bytes.Compare(password, passwordConfirm) != 0 {
fmt.Println("\nPasswords don't match. Exiting")
os.Exit(3)
}
fmt.Println()
return password
}