-
Notifications
You must be signed in to change notification settings - Fork 0
/
tuxify.go
66 lines (58 loc) · 1.56 KB
/
tuxify.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
package tuxify
import (
"bytes"
"crypto/aes"
"crypto/rand"
"image"
"image/color"
)
// Tuxify ecb-encrypts the given image. If key is null, a key will be randomly generated.
// Alpha values are ignored in the source image. Returns the encrypted image and encryption key.
func Tuxify(key []byte, src image.Image) (image.Image, []byte, error) {
rect := src.Bounds()
// Put all raw RGB values into a buffer...
buffy := &bytes.Buffer{}
rgb := make([]byte, 3)
for y := 0; y < rect.Dy(); y++ {
for x := 0; x < rect.Dx(); x++ {
rgba := color.RGBAModel.Convert(src.At(x, y))
r, g, b, _ := rgba.RGBA()
rgb[0], rgb[1], rgb[2] = byte(r), byte(g), byte(b)
buffy.Write(rgb)
}
}
// Encrypt RGB values with the given key (or a random key if the key parameter is nil)
if key == nil {
key = make([]byte, 16)
rand.Read(key)
}
ciphertext, err := encrypt(key, buffy.Bytes())
if err != nil {
return nil, nil, err
}
// Put the encrypted RGB values into a new image...
r := bytes.NewReader(ciphertext)
dst := image.NewRGBA(rect)
for y := 0; y < rect.Dy(); y++ {
for x := 0; x < rect.Dx(); x++ {
r.Read(rgb)
dst.Set(x, y, color.RGBA{rgb[0], rgb[1], rgb[2], 255})
}
}
return dst, key, nil
}
func encrypt(key []byte, data []byte) ([]byte, error) {
cipher, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
bs := cipher.BlockSize()
if mod := len(data) % bs; mod != 0 {
zeros := make([]byte, bs-mod)
data = append(data, zeros...)
}
for block := data; len(block) > 0; block = block[bs:] {
cipher.Encrypt(block, block)
}
return data, nil
}