This repository has been archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
161 lines (140 loc) · 2.85 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
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
156
157
158
159
160
161
package main
import (
"fmt"
"image"
"image/png"
"os"
"path/filepath"
"github.com/ljesparis/glsb/encryption"
"github.com/urfave/cli"
)
var (
// default configuration
conf = defaultConfig
)
func throwError(m string) {
fmt.Println(m)
os.Exit(1)
}
func panicIfErr(err error) {
if err != nil {
throwError(err.Error())
}
}
// verify image format
// only png is supported.
func checkImageFormat(f string) error {
ext := filepath.Ext(f)[1:]
if ext == "png" {
return nil
}
return fmt.Errorf("Image format `%s` not supported", ext)
}
// open png file if exist
func openPngFile(f string) (image.Image, error) {
ff, err := os.Open(f)
if err != nil {
return nil, err
}
defer ff.Close()
i, err := png.Decode(ff)
if err != nil {
return nil, err
}
return i, nil
}
func configureApp(c *cli.Context) {
if c.GlobalIsSet("encryption") {
conf.Encryption.Key = c.GlobalString("key")
e := c.GlobalString("encryption")
switch e {
case "xor":
conf.Encryption.Method = encryption.Xor
break
}
}
}
func read(c *cli.Context) {
if c.NArg() < 1 {
cli.ShowSubcommandHelp(c)
return
}
configureApp(c)
src := c.Args().Get(0)
err := checkImageFormat(src)
panicIfErr(err)
i, err := openPngFile(src)
panicIfErr(err)
message, err := revealMessage(i, conf)
panicIfErr(err)
messageLength := len(message)
fmt.Printf(`Message Found! =).
Message Length: %d
Message: %s
`, messageLength, message)
}
func write(c *cli.Context) {
if c.NArg() < 3 {
cli.ShowSubcommandHelp(c)
return
}
configureApp(c)
args := c.Args()
src, dst, m := args.Get(0), args.Get(1), args.Get(2)
err := checkImageFormat(src)
panicIfErr(err)
err = checkImageFormat(dst)
panicIfErr(err)
i, err := openPngFile(src)
panicIfErr(err)
err = hideMessage(m, dst, i, conf)
panicIfErr(err)
fmt.Println("Message successfully hidden")
}
func methods(_ *cli.Context) {
fmt.Println("\nEncryption Methods Supported:")
for _, m := range []string{"xor"} {
fmt.Println(" *", m)
}
fmt.Println("")
}
func main() {
app := cli.NewApp()
app.Author = author
app.Email = email
app.Version = version
app.Description = description
app.Usage = usage
app.UsageText = usageText
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "encryption",
Value: "None",
},
cli.StringFlag{
Name: "key",
Value: "hide message with glsb!",
},
}
app.Commands = []cli.Command{
{
Name: "write",
Usage: "Used to hide messages within images",
UsageText: "glsb [options...] write [src] [dst] [message]",
Action: write,
},
{
Name: "read",
Usage: "Used to read hidden messages that are placed within images",
UsageText: "glsb [options...] read [src]",
Action: read,
},
{
Name: "methods",
Usage: "Used to list encryption methods supported by glsb",
UsageText: "glsb methods",
Action: methods,
},
}
app.Run(os.Args)
}