aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore2
-rw-r--r--go.mod3
-rw-r--r--main.go83
3 files changed, 88 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..8b9c5c3
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+hashimg
+out.png
diff --git a/go.mod b/go.mod
new file mode 100644
index 0000000..6291d63
--- /dev/null
+++ b/go.mod
@@ -0,0 +1,3 @@
+module github.com/benburwell/hashimg
+
+go 1.12
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..28a859d
--- /dev/null
+++ b/main.go
@@ -0,0 +1,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())
+}