Spaces:
Sleeping
Sleeping
finish
Browse files- main.go +17 -54
- templates/index.html +1 -0
main.go
CHANGED
@@ -11,9 +11,9 @@ import (
|
|
11 |
"image/png"
|
12 |
"log"
|
13 |
"net/http"
|
|
|
14 |
"runtime/debug"
|
15 |
"strconv"
|
16 |
-
"strings"
|
17 |
"time"
|
18 |
)
|
19 |
|
@@ -68,9 +68,9 @@ func embedLSB(img image.Image, message string) image.Image {
|
|
68 |
for i := 0; i < bounds.Dx(); i++ {
|
69 |
for j := 0; j < bounds.Dy(); j++ {
|
70 |
r, g, b, a := img.At(i, j).RGBA()
|
71 |
-
pixel := [
|
72 |
|
73 |
-
for k := 0; k <
|
74 |
if dataIndex < panjangPesan {
|
75 |
isiPixel := intToBinaryString(int(pixel[k]))
|
76 |
isiPixel = isiPixel[:7] + string(binaryPesan[dataIndex])
|
@@ -80,7 +80,7 @@ func embedLSB(img image.Image, message string) image.Image {
|
|
80 |
}
|
81 |
}
|
82 |
|
83 |
-
newImg.
|
84 |
}
|
85 |
}
|
86 |
return newImg
|
@@ -94,10 +94,10 @@ func extractLSB(img image.Image) string {
|
|
94 |
|
95 |
for i := 0; i < bounds.Dx(); i++ {
|
96 |
for j := 0; j < bounds.Dy(); j++ {
|
97 |
-
r, g, b,
|
98 |
-
pixel := [
|
99 |
|
100 |
-
for k := 0; k <
|
101 |
binaryPixel := intToBinaryString(int(pixel[k]))
|
102 |
binaryMessage += string(binaryPixel[7])
|
103 |
dataIndex++
|
@@ -126,49 +126,27 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) {
|
|
126 |
if r.Method == http.MethodPost {
|
127 |
start := time.Now()
|
128 |
|
129 |
-
file,
|
130 |
if err != nil {
|
131 |
http.Error(w, err.Error(), http.StatusBadRequest)
|
132 |
return
|
133 |
}
|
134 |
defer file.Close()
|
135 |
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
switch strings.ToLower(filepath.Ext(fileHeader.Filename)) {
|
140 |
-
case ".png":
|
141 |
-
img, imgFormat, err = image.Decode(file)
|
142 |
-
if err != nil {
|
143 |
-
http.Error(w, err.Error(), http.StatusBadRequest)
|
144 |
-
return
|
145 |
-
}
|
146 |
-
case ".jpg", ".jpeg":
|
147 |
-
img, err = jpeg.Decode(file)
|
148 |
-
if err != nil {
|
149 |
-
http.Error(w, err.Error(), http.StatusBadRequest)
|
150 |
-
return
|
151 |
-
}
|
152 |
-
imgFormat = "jpeg"
|
153 |
-
default:
|
154 |
-
http.Error(w, "Unsupported file format", http.StatusBadRequest)
|
155 |
return
|
156 |
}
|
157 |
|
158 |
message := r.FormValue("message")
|
159 |
binaryPesan := pesanKeBinary(message)
|
160 |
|
161 |
-
// Embed message
|
162 |
newImg := embedLSB(img, binaryPesan)
|
163 |
embedDuration := time.Since(start).Seconds()
|
164 |
|
165 |
var buf bytes.Buffer
|
166 |
-
|
167 |
-
case "jpeg":
|
168 |
-
err = jpeg.Encode(&buf, newImg, nil)
|
169 |
-
case "png":
|
170 |
-
err = png.Encode(&buf, newImg)
|
171 |
-
}
|
172 |
if err != nil {
|
173 |
http.Error(w, err.Error(), http.StatusInternalServerError)
|
174 |
return
|
@@ -185,36 +163,21 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) {
|
|
185 |
http.ServeFile(w, r, "templates/index.html")
|
186 |
}
|
187 |
}
|
|
|
188 |
func extractHandler(w http.ResponseWriter, r *http.Request) {
|
189 |
if r.Method == http.MethodPost {
|
190 |
start := time.Now()
|
191 |
|
192 |
-
file,
|
193 |
if err != nil {
|
194 |
http.Error(w, err.Error(), http.StatusBadRequest)
|
195 |
return
|
196 |
}
|
197 |
defer file.Close()
|
198 |
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
switch strings.ToLower(filepath.Ext(fileHeader.Filename)) {
|
203 |
-
case ".png":
|
204 |
-
img, imgFormat, err = image.Decode(file)
|
205 |
-
if err != nil {
|
206 |
-
http.Error(w, err.Error(), http.StatusBadRequest)
|
207 |
-
return
|
208 |
-
}
|
209 |
-
case ".jpg", ".jpeg":
|
210 |
-
img, err = jpeg.Decode(file)
|
211 |
-
if err != nil {
|
212 |
-
http.Error(w, err.Error(), http.StatusBadRequest)
|
213 |
-
return
|
214 |
-
}
|
215 |
-
imgFormat = "jpeg"
|
216 |
-
default:
|
217 |
-
http.Error(w, "Unsupported file format", http.StatusBadRequest)
|
218 |
return
|
219 |
}
|
220 |
|
|
|
11 |
"image/png"
|
12 |
"log"
|
13 |
"net/http"
|
14 |
+
|
15 |
"runtime/debug"
|
16 |
"strconv"
|
|
|
17 |
"time"
|
18 |
)
|
19 |
|
|
|
68 |
for i := 0; i < bounds.Dx(); i++ {
|
69 |
for j := 0; j < bounds.Dy(); j++ {
|
70 |
r, g, b, a := img.At(i, j).RGBA()
|
71 |
+
pixel := [3]uint8{uint8(r >> 8), uint8(g >> 8), uint8(b >> 8)}
|
72 |
|
73 |
+
for k := 0; k < 3; k++ { // Loop untuk setiap channel warna (RGB)
|
74 |
if dataIndex < panjangPesan {
|
75 |
isiPixel := intToBinaryString(int(pixel[k]))
|
76 |
isiPixel = isiPixel[:7] + string(binaryPesan[dataIndex])
|
|
|
80 |
}
|
81 |
}
|
82 |
|
83 |
+
newImg.Set(i, j, color.RGBA{pixel[0], pixel[1], pixel[2], uint8(a >> 8)})
|
84 |
}
|
85 |
}
|
86 |
return newImg
|
|
|
94 |
|
95 |
for i := 0; i < bounds.Dx(); i++ {
|
96 |
for j := 0; j < bounds.Dy(); j++ {
|
97 |
+
r, g, b, _ := img.At(i, j).RGBA()
|
98 |
+
pixel := [3]uint8{uint8(r >> 8), uint8(g >> 8), uint8(b >> 8)}
|
99 |
|
100 |
+
for k := 0; k < 3; k++ { // Loop untuk setiap channel warna (RGB)
|
101 |
binaryPixel := intToBinaryString(int(pixel[k]))
|
102 |
binaryMessage += string(binaryPixel[7])
|
103 |
dataIndex++
|
|
|
126 |
if r.Method == http.MethodPost {
|
127 |
start := time.Now()
|
128 |
|
129 |
+
file, _, err := r.FormFile("image")
|
130 |
if err != nil {
|
131 |
http.Error(w, err.Error(), http.StatusBadRequest)
|
132 |
return
|
133 |
}
|
134 |
defer file.Close()
|
135 |
|
136 |
+
img, _, err := image.Decode(file)
|
137 |
+
if err != nil {
|
138 |
+
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
139 |
return
|
140 |
}
|
141 |
|
142 |
message := r.FormValue("message")
|
143 |
binaryPesan := pesanKeBinary(message)
|
144 |
|
|
|
145 |
newImg := embedLSB(img, binaryPesan)
|
146 |
embedDuration := time.Since(start).Seconds()
|
147 |
|
148 |
var buf bytes.Buffer
|
149 |
+
err = png.Encode(&buf, newImg)
|
|
|
|
|
|
|
|
|
|
|
150 |
if err != nil {
|
151 |
http.Error(w, err.Error(), http.StatusInternalServerError)
|
152 |
return
|
|
|
163 |
http.ServeFile(w, r, "templates/index.html")
|
164 |
}
|
165 |
}
|
166 |
+
|
167 |
func extractHandler(w http.ResponseWriter, r *http.Request) {
|
168 |
if r.Method == http.MethodPost {
|
169 |
start := time.Now()
|
170 |
|
171 |
+
file, _, err := r.FormFile("image")
|
172 |
if err != nil {
|
173 |
http.Error(w, err.Error(), http.StatusBadRequest)
|
174 |
return
|
175 |
}
|
176 |
defer file.Close()
|
177 |
|
178 |
+
img, _, err := image.Decode(file)
|
179 |
+
if err != nil {
|
180 |
+
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
181 |
return
|
182 |
}
|
183 |
|
templates/index.html
CHANGED
@@ -39,6 +39,7 @@
|
|
39 |
type="file"
|
40 |
id="image"
|
41 |
name="image"
|
|
|
42 |
class="mt-1 block w-full border border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm"
|
43 |
/>
|
44 |
</div>
|
|
|
39 |
type="file"
|
40 |
id="image"
|
41 |
name="image"
|
42 |
+
accept="image/png"
|
43 |
class="mt-1 block w-full border border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm"
|
44 |
/>
|
45 |
</div>
|