/
action.go
155 lines (145 loc) · 3.83 KB
/
action.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package action
import (
"context"
"errors"
"flag"
"github.com/CrackedPoly/AES-implementation-in-Golang/src/aes"
"github.com/CrackedPoly/AES-implementation-in-Golang/src/utils"
"github.com/otokaze/go-kit/log"
"github.com/peterh/liner"
"github.com/urfave/cli/v2"
"strings"
)
func EncryptAction(ctx *cli.Context) (err error) {
mode := ctx.String("m")
plainfile := ctx.String("p")
keyfile := ctx.String("k")
vifile := ctx.String("v")
cipherfile := ctx.String("c")
authfile := ctx.String("a")
tagfile := ctx.String("tag")
plain := utils.ReadBytesHex(plainfile)
key := utils.ReadBytesHex(keyfile)
iv := utils.ReadBytesHex(vifile)
auth := utils.ReadBytesHex(authfile)
_aes, err := aes.NewAES(key)
if err != nil {
log.Error("%s", err)
return err
}
var ciphertext []byte
switch strings.ToTitle(mode) {
case "ECB":
ciphertext = _aes.EncryptECB(plain, utils.PKCS7Padding)
case "CBC":
ciphertext = _aes.EncryptCBC(plain, iv[0:16], utils.PKCS7Padding)
case "CFB":
ciphertext = _aes.EncryptCFB(plain, iv[0:16], 16)
case "OFB":
ciphertext = _aes.EncryptOFB(plain, iv[0:16])
case "CTR":
ciphertext = _aes.EncryptCTR(plain, iv[0:16])
case "GCM":
var tag []byte
ciphertext, tag = _aes.EncryptGCM(plain, iv, auth, 16)
utils.WriteBytesHex(tagfile, tag)
default:
log.Error("invalid mode")
return errors.New("invalid mode")
}
utils.WriteBytesHex(cipherfile, ciphertext)
return nil
}
func DecryptAction(ctx *cli.Context) (err error) {
mode := ctx.String("m")
plainfile := ctx.String("p")
keyfile := ctx.String("k")
vifile := ctx.String("v")
cipherfile := ctx.String("c")
authfile := ctx.String("a")
tagfile := ctx.String("tag")
ciphertext := utils.ReadBytesHex(cipherfile)
key := utils.ReadBytesHex(keyfile)
iv := utils.ReadBytesHex(vifile)
auth := utils.ReadBytesHex(authfile)
tag := utils.ReadBytesHex(tagfile)
_aes, err := aes.NewAES(key)
if err != nil {
log.Error("%s", err)
return err
}
var plaintext []byte
switch strings.ToTitle(mode) {
case "ECB":
plaintext = _aes.DecryptECB(ciphertext, utils.PKCS7Unpadding)
case "CBC":
plaintext = _aes.DecryptCBC(ciphertext, iv[0:16], utils.PKCS7Unpadding)
case "CFB":
plaintext = _aes.DecryptCFB(ciphertext, iv[0:16], 16)
case "OFB":
plaintext = _aes.DecryptOFB(ciphertext, iv[0:16])
case "CTR":
plaintext = _aes.EncryptCTR(ciphertext, iv[0:16])
case "GCM":
plaintext = _aes.DecryptGCM(ciphertext, iv, auth, tag)
default:
log.Error("invalid mode")
return errors.New("invalid mode")
}
utils.WriteBytesHex(plainfile, plaintext)
return nil
}
func AfterAction(ctx *cli.Context) (err error) {
line := liner.NewLiner()
line.SetCtrlCAborts(true)
defer line.Close()
line.SetCompleter(func(line string) (cs []string) {
for _, c := range ctx.App.Commands {
if strings.HasPrefix(c.Name, strings.ToLower(line)) {
cs = append(cs, c.Name)
}
}
return
})
for {
var input string
if input, err = line.Prompt("> "); err != nil {
if err == liner.ErrPromptAborted {
err = nil
return
}
log.Error("line.Prompt() error(%v)", err)
continue
}
line.AppendHistory(input)
input = strings.TrimSpace(input)
args := strings.Split(input, " ")
if args[0] == "" {
continue
}
var command *cli.Command
if command = ctx.App.Command(args[0]); command == nil {
log.Error("command(%s) not found!", args[0])
continue
}
var fset = flag.NewFlagSet(args[0], flag.ContinueOnError)
for _, f := range command.Flags {
f.Apply(fset)
}
if !command.SkipFlagParsing {
if err = fset.Parse(args[1:]); err != nil {
if err == flag.ErrHelp {
err = nil
continue
}
log.Error("fs.Parse(%v) error(%v)", args[1:], err)
continue
}
}
var key interface{} = "args"
nCtx := cli.NewContext(ctx.App, fset, ctx)
nCtx.Context = context.WithValue(nCtx.Context, key, args[1:])
nCtx.Command = command
command.Action(nCtx)
}
}