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
/
hide.go
70 lines (55 loc) · 1.25 KB
/
hide.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
package main
import (
"errors"
"fmt"
"image"
"image/png"
"os"
"strconv"
)
var (
messageToLong = errors.New("Message to long!")
)
// add a bit to a byte
func setLsb(n uint8, c uint8) (r uint8) {
i, _ := strconv.ParseInt(string(c), 10, 64)
r = n&^1 | uint8(i)
return
}
func hideMessage(message, dst string, img image.Image, conf *Configuration) error {
message = conf.Encryption.Encrypt(message)
bMessage := bytes2Binary([]byte(fmt.Sprintf("%d%s", len(message), message)))
bMessageLength := len(bMessage)
index := 0
width, height := img.Bounds().Dx(), img.Bounds().Dy()
if bMessageLength > (width * height * 3) {
return messageToLong
}
nRGBA := toRGBA(img)
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
c := nRGBA.RGBAAt(x, y) // image color
if index+1 <= bMessageLength {
c.R = setLsb(c.R, bMessage[index])
}
if index+2 <= bMessageLength {
c.G = setLsb(c.G, bMessage[index+1])
}
if index+3 <= bMessageLength {
c.B = setLsb(c.B, bMessage[index+2])
}
nRGBA.SetRGBA(x, y, c) // set new pixel color to RGBA object
index += 3
}
}
dstW, err := os.Create(dst)
if err != nil {
return err
}
defer dstW.Close()
err = png.Encode(dstW, nRGBA)
if err != nil {
return err
}
return nil
}