-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
109 lines (86 loc) · 1.72 KB
/
app.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
package main
import (
"io"
"syscall"
"golang.org/x/term"
)
const (
minArgsLen = 2
msgForAskingPassphrase = "Enter your password (min 8 characters and max 64 characters): "
)
type cryptHandler func(plainData, key []byte) ([]byte, error)
type app struct {
args []string
input io.ReadWriter
output io.ReadWriter
}
func (ap app) run() error {
if len(ap.args) < minArgsLen {
return ErrCmd
}
commands := Commands{
newEncryptCmd(cryptHandler(Encrypt)),
newDecryptCmd(cryptHandler(Decrypt)),
}
cmdInput := ap.args[1]
cmd, err := commands.Get(cmdInput)
if err != nil {
return err
}
err = cmd.Validate(ap.args)
if err != nil {
return err
}
_, err = io.WriteString(ap.output, msgForAskingPassphrase)
if err != nil {
return err
}
passphraseInput, err := term.ReadPassword(int(syscall.Stdin))
if err != nil {
return ErrPassNotFound
}
key, err := createKey(passphraseInput)
if err != nil {
return err
}
err = cmd.Execute(key.HashResult())
if err != nil {
return err
}
return nil
}
func (ap app) runForTesting() error {
if len(ap.args) < minArgsLen {
return ErrCmd
}
commands := Commands{
newEncryptCmd(cryptHandler(Encrypt)),
newDecryptCmd(cryptHandler(Decrypt)),
}
cmdInput := ap.args[1]
cmd, err := commands.Get(cmdInput)
if err != nil {
return err
}
err = cmd.Validate(ap.args)
if err != nil {
return err
}
_, err = io.WriteString(ap.output, msgForAskingPassphrase)
if err != nil {
return err
}
passphraseInput, err := io.ReadAll(ap.input)
if err != nil {
return ErrPassNotFound
}
passphrase, err := createKey(passphraseInput)
if err != nil {
return err
}
err = cmd.Execute(passphrase.HashResult())
if err != nil {
return err
}
return nil
}