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
|
package main
import (
"bytes"
"crypto/sha256"
"encoding/base64"
"flag"
"fmt"
"image"
"image/color"
"image/png"
"regexp"
"strings"
)
var usernamePattern = regexp.MustCompile("[a-zA-Z][a-zA-Z0-9]+")
func main() {
username := flag.String("username", "", "username to encode as image")
flag.Parse()
if strings.TrimSpace(*username) == "" {
fmt.Println("no username")
return
}
if !usernamePattern.MatchString(*username) {
fmt.Println("no match")
return
}
seed := hash(*username)
err, img := generateImage(seed)
if err != nil {
fmt.Printf("%v\n", err)
return
}
err, str := encodeImage(img)
if err != nil {
fmt.Printf("%v\n", err)
return
}
fmt.Println(str)
}
func hash(username string) [32]byte {
return sha256.Sum256([]byte(username))
}
func generateImage(hash [32]byte) (error, image.Image) {
img := image.NewNRGBA(image.Rect(0, 0, 5, 5))
colorA := color.NRGBA{
R: hash[0],
G: hash[1],
B: hash[2],
A: 255,
}
colorB := color.NRGBA{
R: hash[3],
G: hash[4],
B: hash[5],
A: 255,
}
offset := 6
for x := 0; x < 5; x++ {
for y := 0; y < 5; y++ {
on := hash[offset]&0xf0 > 0
color := colorA
if on {
color = colorB
}
img.Set(x, y, color)
offset++
}
}
return nil, img
}
func encodeImage(img image.Image) (error, string) {
var b bytes.Buffer
if err := png.Encode(&b, img); err != nil {
return err, ""
}
return nil, base64.StdEncoding.EncodeToString(b.Bytes())
}
|