-
Notifications
You must be signed in to change notification settings - Fork 44
/
steganography_test.go
240 lines (192 loc) · 8.12 KB
/
steganography_test.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package steganography
import (
"bufio"
"bytes"
"image"
"image/color"
"image/jpeg"
"log"
"os"
"testing"
)
var rawInputFilePng = "./examples/stegosaurus.png"
var rawInputFileJpg = "./examples/stegosaurus.jpg"
var encodedInputFilePng = "./examples/encoded_stegosaurus.png"
var encodedInputFileJpg = "./examples/encoded_stegosaurus_from_jpg.png"
var bitmessage = []uint8{84, 104, 101, 113, 117, 97, 100, 114, 117, 112, 101, 100, 97, 108, 83, 116, 101, 103, 111, 115, 97, 117, 114, 117, 115, 105, 115, 111, 110, 101, 111, 102, 116, 104, 101, 109, 111, 115, 116, 101, 97, 115, 105, 108, 121, 105, 100, 101, 110, 116, 105, 102, 105, 97, 98, 108, 101, 100, 105, 110, 111, 115, 97, 117, 114, 103, 101, 110, 101, 114, 97, 44, 100, 117, 101, 116, 111, 116, 104, 101, 100, 105, 115, 116, 105, 110, 99, 116, 105, 118, 101, 100, 111, 117, 98, 108, 101, 114, 111, 119, 111, 102, 107, 105, 116, 101, 45, 115, 104, 97, 112, 101, 100, 112, 108, 97, 116, 101, 115, 114, 105, 115, 105, 110, 103, 118, 101, 114, 116, 105, 99, 97, 108, 108, 121, 97, 108, 111, 110, 103, 116, 104, 101, 114, 111, 117, 110, 100, 101, 100, 98, 97, 99, 107, 97, 110, 100, 116, 104, 101, 116, 119, 111, 112, 97, 105, 114, 115, 111, 102, 108, 111, 110, 103, 115, 112, 105, 107, 101, 115, 101, 120, 116, 101, 110, 100, 105, 110, 103, 104, 111, 114, 105, 122, 111, 110, 116, 97, 108, 108, 121, 110, 101, 97, 114, 116, 104, 101, 101, 110, 100, 111, 102, 116, 104, 101, 116, 97, 105, 108, 46, 65, 108, 116, 104, 111, 117, 103, 104, 108, 97, 114, 103, 101, 105, 110, 100, 105, 118, 105, 100, 117, 97, 108, 115, 99, 111, 117, 108, 100, 103, 114, 111, 119, 117, 112, 116, 111, 57, 109, 40, 50, 57, 46, 53, 102, 116, 41, 105, 110, 108, 101, 110, 103, 116, 104, 91, 52, 93, 97, 110, 100, 53, 46, 51, 116, 111, 55, 109, 101, 116, 114, 105, 99, 116, 111, 110, 115, 40, 53, 46, 56, 116, 111, 55, 46, 55, 115, 104, 111, 114, 116, 116, 111, 110, 115, 41, 105, 110, 119, 101, 105, 103, 104, 116, 44, 91, 53, 93, 91, 54, 93, 116, 104, 101, 118, 97, 114, 105, 111, 117, 115, 115, 112, 101, 99, 105, 101, 115, 111, 102, 83, 116, 101, 103, 111, 115, 97, 117, 114, 117, 115, 119, 101, 114, 101, 100, 119, 97, 114, 102, 101, 100, 98, 121, 99, 111, 110, 116, 101, 109, 112, 111, 114, 97, 114, 105, 101, 115, 44, 116, 104, 101, 103, 105, 97, 110, 116, 115, 97, 117, 114, 111, 112, 111, 100, 115, 46, 83, 111, 109, 101, 102, 111, 114, 109, 111, 102, 97, 114, 109, 111, 114, 97, 112, 112, 101, 97, 114, 115, 116, 111, 104, 97, 118, 101, 98, 101, 101, 110, 110, 101, 99, 101, 115, 115, 97, 114, 121, 44, 97, 115, 83, 116, 101, 103, 111, 115, 97, 117, 114, 117, 115, 115, 112, 101, 99, 105, 101, 115, 99, 111, 101, 120, 105, 115, 116, 101, 100, 119, 105, 116, 104, 108, 97, 114, 103, 101, 112, 114, 101, 100, 97, 116, 111, 114, 121, 116, 104, 101, 114, 111, 112, 111, 100, 100, 105, 110, 111, 115, 97, 117, 114, 115, 44, 115, 117, 99, 104, 97, 115, 65, 108, 108, 111, 115, 97, 117, 114, 117, 115, 97, 110, 100, 67, 101, 114, 97, 116, 111, 115, 97, 117, 114, 117, 115, 46}
func TestEncodeFromPngFile(t *testing.T) {
inFile, err := os.Open(rawInputFilePng)
if err != nil {
log.Printf("Error opening file %s: %v", rawInputFilePng, err)
t.FailNow()
}
defer inFile.Close()
reader := bufio.NewReader(inFile)
img, _, err := image.Decode(reader)
if err != nil {
log.Printf("Error decoding. %v", err)
t.FailNow()
}
w := new(bytes.Buffer)
err = Encode(w, img, bitmessage) // Encode the message into the image file
if err != nil {
log.Printf("Error Encoding file %v", err)
t.FailNow()
}
outFile, err := os.Create(encodedInputFilePng)
if err != nil {
log.Printf("Error creating file %s: %v", encodedInputFilePng, err)
t.FailNow()
}
w.WriteTo(outFile)
defer outFile.Close()
}
func TestEncodeFromJpgFile(t *testing.T) {
inFile, err := os.Open(rawInputFileJpg)
if err != nil {
log.Printf("Error opening file %s: %v", rawInputFileJpg, err)
t.FailNow()
}
defer inFile.Close()
reader := bufio.NewReader(inFile)
img, err := jpeg.Decode(reader)
if err != nil {
log.Printf("Error decoding. %v", err)
t.FailNow()
}
w := new(bytes.Buffer)
err = Encode(w, img, bitmessage) // Encode the message into the image file
if err != nil {
log.Printf("Error Encoding file %v", err)
t.FailNow()
}
outFile, err := os.Create(encodedInputFileJpg)
if err != nil {
log.Printf("Error creating file %s: %v", encodedInputFileJpg, err)
t.FailNow()
}
w.WriteTo(outFile)
defer outFile.Close()
}
func TestDecodeFromPngFile(t *testing.T) {
inFile, err := os.Open(encodedInputFilePng)
if err != nil {
log.Printf("Error opening file %s: %v", encodedInputFilePng, err)
t.FailNow()
}
defer inFile.Close()
reader := bufio.NewReader(inFile)
img, _, err := image.Decode(reader)
if err != nil {
log.Print("Error decoding file")
t.FailNow()
}
sizeOfMessage := GetMessageSizeFromImage(img)
msg := Decode(sizeOfMessage, img) // Read the message from the picture file
if !bytes.Equal(msg, bitmessage) {
log.Print("messages dont match:")
log.Println(string(msg))
t.FailNow()
}
}
func TestEncodeDecodeGeneratedSmallImage(t *testing.T) {
// Creating image
width := 30
height := 1
upLeft := image.Point{0, 0}
lowRight := image.Point{width, height}
newimg := image.NewNRGBA(image.Rectangle{upLeft, lowRight})
// Set color for each pixel.
for x := 0; x < width; x++ {
for y := 0; y < height; y++ {
newimg.Set(x, y, color.White)
}
}
w := new(bytes.Buffer)
err := EncodeNRGBA(w, newimg, []uint8{84, 84, 84}) // Encode the message into the image file
if err != nil {
log.Printf("Error Encoding file %v", err)
t.FailNow()
}
decodeImg, _, err := image.Decode(w)
if err != nil {
log.Println("Failed to Decode Image")
t.FailNow()
}
sizeOfMessage := GetMessageSizeFromImage(decodeImg)
msg := Decode(sizeOfMessage, decodeImg) // Read the message from the picture file
// otherwise, print the message to STDOUT
if !bytes.Equal(msg, []uint8{84, 84, 84}) {
log.Print("messages dont match:")
log.Println(string(msg))
t.FailNow()
}
}
func TestSmalImage(t *testing.T) {
miniImage := image.Image(image.NewNRGBA(image.Rectangle{image.Point{0, 0}, image.Point{18, 1}}))
log.Print(MaxEncodeSize(miniImage))
if MaxEncodeSize(miniImage) > 0 {
log.Printf("Uncaught small image size")
t.FailNow()
}
miniImage = image.Image(image.NewNRGBA(image.Rectangle{image.Point{0, 0}, image.Point{23, 1}}))
if MaxEncodeSize(miniImage) != 4 {
log.Printf("Uncaught minimal image size")
t.FailNow()
}
}
func TestMessageTooLarge(t *testing.T) {
miniImage := image.Image(image.NewNRGBA(image.Rectangle{image.Point{0, 0}, image.Point{24, 1}}))
w := new(bytes.Buffer)
err := Encode(w, miniImage, bitmessage) // Encode the message into the image file
if err == nil {
log.Printf("Uncaught error: message too large for image")
t.FailNow()
}
}
func TestEncodeDecodeGenerateColors(t *testing.T) {
// Creating image
width := 50
height := 50
colors := []color.Color{
color.NRGBA{R: 255, G: 0, B: 0, A: 255},
color.NRGBA{R: 0, G: 255, B: 0, A: 255},
color.NRGBA{R: 0, G: 0, B: 255, A: 255},
color.NRGBA{R: 0, G: 0, B: 0, A: 128},
color.NRGBA{R: 255, G: 255, B: 255, A: 1},
color.NRGBA{R: 255, G: 255, B: 255, A: 128},
color.NRGBA{R: 127, G: 127, B: 127, A: 255},
color.NRGBA{R: 255, G: 255, B: 255, A: 255},
}
for _, color := range colors {
upLeft := image.Point{0, 0}
lowRight := image.Point{width, height}
newimg := image.NewNRGBA(image.Rectangle{upLeft, lowRight})
// Set color for each pixel.
for x := 0; x < width; x++ {
for y := 0; y < height; y++ {
newimg.Set(x, y, color)
}
}
w := new(bytes.Buffer)
err := EncodeNRGBA(w, newimg, bitmessage) // Encode the message into the image file
if err != nil {
log.Printf("Error Encoding file %v", err)
t.FailNow()
}
decodeImg, _, err := image.Decode(w)
if err != nil {
log.Println("Failed to Decode Image")
t.FailNow()
}
sizeOfMessage := GetMessageSizeFromImage(decodeImg)
msg := Decode(sizeOfMessage, decodeImg) // Read the message from the picture file
// otherwise, print the message to STDOUT
if !bytes.Equal(msg, bitmessage) {
log.Printf("Color case: %v", color)
log.Print("messages dont match:")
log.Println(string(msg))
t.FailNow()
}
}
}