From e141becd41103939650292409ca49fc8af042761 Mon Sep 17 00:00:00 2001 From: Daniel Istrate Date: Sun, 22 Nov 2020 23:33:55 +0200 Subject: [PATCH 1/2] Identicon script --- src/main.go | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 src/main.go diff --git a/src/main.go b/src/main.go new file mode 100644 index 0000000..9dfb638 --- /dev/null +++ b/src/main.go @@ -0,0 +1,91 @@ +package main + +import ( + "crypto/sha1" + "flag" + "fmt" + "image" + "image/color/palette" + "image/draw" + "image/png" + "log" + "os" +) + +const hashLength = 40 + +func getHash(s string) string { + return fmt.Sprintf("%x", sha1.Sum([]byte(s))) +} + +func switchIndexDirection() func() int { + idx := 0 + currentSign := "+" + positiveSign := "+" + negativeSign := "-" + + return func() int { + if idx == 0 { + currentSign = positiveSign + } else if idx == hashLength-1 { + currentSign = negativeSign + } + if currentSign == positiveSign { + idx++ + return idx + } + idx-- + return idx + } +} + +func generateImage(s string, size, sideBlocks int) *image.RGBA { + scale := size / sideBlocks + img := image.NewRGBA(image.Rect(0, 0, sideBlocks*scale, sideBlocks*scale)) + + idx := switchIndexDirection() + for x := 0; x < sideBlocks; x++ { + for y := 0; y < sideBlocks; y++ { + col := palette.Plan9[s[idx()]] + startPoint := image.Point{x * scale, y * scale} + endPoint := image.Point{x*scale + scale, y*scale + scale} + rectangle := image.Rectangle{startPoint, endPoint} + draw.Draw(img, rectangle, &image.Uniform{col}, image.Point{}, draw.Src) + } + } + + return img +} + +func saveImage(img *image.RGBA, filename string) { + + f, err := os.Create(fmt.Sprintf("%s.png", filename)) + if err != nil { + log.Fatal(err) + } + + if err := png.Encode(f, img); err != nil { + f.Close() + log.Fatal(err) + } + + if err := f.Close(); err != nil { + log.Fatal(err) + } + +} + +func main() { + inputString := flag.String("input", "Cool", "value to be hashed and generate identicon with") + outputFile := flag.String("output", "image", "image file name") + imageSize := flag.Int("size", 256, "image size") + blocks := flag.Int("blocks", 8, "number of side blocks") + + flag.Parse() + + hash := getHash(*inputString) + fmt.Println(hash) + + img := generateImage(hash, *imageSize, *blocks) + saveImage(img, *outputFile) +} From b996e372438e524ed5728fc2acb68789d7034ac0 Mon Sep 17 00:00:00 2001 From: Daniel Istrate Date: Sun, 22 Nov 2020 23:41:48 +0200 Subject: [PATCH 2/2] Example of how to run it --- src/main.go | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main.go b/src/main.go index 9dfb638..a624de7 100644 --- a/src/main.go +++ b/src/main.go @@ -75,6 +75,7 @@ func saveImage(img *image.RGBA, filename string) { } +// Run it as: go run ./main.go -input=Awesome -size 256 -output image -blocks 8 func main() { inputString := flag.String("input", "Cool", "value to be hashed and generate identicon with") outputFile := flag.String("output", "image", "image file name")