-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
165 lines (140 loc) · 3.74 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
162
163
164
165
package main
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"errors"
"fmt"
"image"
_ "image/gif"
_ "image/jpeg"
"image/png"
_ "image/png"
"io"
"log"
"syscall/js"
"github.com/auyer/steganography"
)
func valueToByteArray(v js.Value) []byte {
binImage := make([]byte, v.Length())
js.CopyBytesToGo(binImage, v)
return binImage
}
func Encrypt(plaintext []byte, key []byte) (ciphertext []byte, err error) {
k := sha256.Sum256(key)
block, err := aes.NewCipher(k[:])
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
nonce := make([]byte, gcm.NonceSize())
_, err = io.ReadFull(rand.Reader, nonce)
if err != nil {
return nil, err
}
return gcm.Seal(nonce, nonce, plaintext, nil), nil
}
// Decrypt decrypts data using 256-bit AES-GCM. This both hides the content of
// the data and provides a check that it hasn't been altered. Expects input
// form nonce|ciphertext|tag where '|' indicates concatenation.
func Decrypt(ciphertext []byte, key []byte) (plaintext []byte, err error) {
k := sha256.Sum256(key)
block, err := aes.NewCipher(k[:])
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
if len(ciphertext) < gcm.NonceSize() {
return nil, errors.New("malformed ciphertext")
}
return gcm.Open(nil,
ciphertext[:gcm.NonceSize()],
ciphertext[gcm.NonceSize():],
nil,
)
}
// OpenImageFile open a image and return a image object
func OpenImageFile(imgByte []byte) (image.Image, error) {
img, _, err := image.Decode(bytes.NewReader(imgByte))
if err != nil {
return nil, err
}
return img, nil
}
func toBase64(b []byte) string {
return base64.StdEncoding.EncodeToString(b)
}
// func add(this js.Value, i []js.Value) interface{} {
// println(i)
// return js.ValueOf(i[0].Int() + i[1].Int())
// }
func imageEncode(this js.Value, i []js.Value) interface{} {
imgArr := i[0]
inBuf := make([]uint8, imgArr.Get("byteLength").Int())
js.CopyBytesToGo(inBuf, imgArr)
img, _ := OpenImageFile(inBuf)
w := new(bytes.Buffer) // buffer that will recieve the results
mssg := []byte(i[1].String())
//
key := []byte(i[2].String())
ct, err := Encrypt([]byte(mssg), key)
if err != nil {
panic(err)
}
EncryptText := base64.StdEncoding.EncodeToString(ct)
fmt.Println("Encrypted:", base64.StdEncoding.EncodeToString(ct))
//
errImg := steganography.Encode(w, img, []byte(EncryptText))
if errImg != nil {
log.Printf("Error Encoding file %v", err)
return ""
}
oi, _ := png.Decode(w)
var buff bytes.Buffer
png.Encode(&buff, oi)
encodeString := base64.StdEncoding.EncodeToString(buff.Bytes())
return js.ValueOf(encodeString)
}
func imageDecode(this js.Value, i []js.Value) interface{} {
imgArr := i[0]
inBuf := make([]uint8, imgArr.Get("byteLength").Int())
js.CopyBytesToGo(inBuf, imgArr)
img, _ := OpenImageFile(inBuf)
// img, _ = png.Decode(inBuf)
pass := i[1].String()
sizeOfMessage := steganography.GetMessageSizeFromImage(img) // retrieving message size to decode in the next line
fmt.Println(sizeOfMessage)
msg := steganography.Decode(sizeOfMessage, img) // decoding the message from the file
data1, err1 := base64.StdEncoding.DecodeString(string(msg))
if err1 != nil {
fmt.Println("error:", err1)
}
pt, err := Decrypt(data1, []byte(pass))
if err != nil {
panic(err)
}
return js.ValueOf(string(pt))
}
func registerCallbacks() {
js.Global().Set("add", js.FuncOf(add))
js.Global().Set("imageEncode", js.FuncOf(imageEncode))
js.Global().Set("imageDecode", js.FuncOf(imageDecode))
}
// exposing to JS
func main() {
c := make(chan struct{}, 0)
println("WASM Go Initialized")
// register functions
registerCallbacks()
<-c
}
// GOARCH=wasm GOOS=js go build -o lib.wasm main.go