From ee981394e80d55338d5d64bed905065793ce9bc9 Mon Sep 17 00:00:00 2001 From: Andrew Cuga Date: Fri, 2 Sep 2022 14:20:38 -0400 Subject: [PATCH 01/10] Initial commit --- go.mod | 3 +++ runMe.go | 0 2 files changed, 3 insertions(+) create mode 100644 go.mod create mode 100644 runMe.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..dba0d96 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module avatarme + +go 1.19 diff --git a/runMe.go b/runMe.go new file mode 100644 index 0000000..e69de29 From b61072b35d5d1c90e5bcfae82479ff11f8ebc3cd Mon Sep 17 00:00:00 2001 From: Andrew Cuga Date: Fri, 2 Sep 2022 15:24:34 -0400 Subject: [PATCH 02/10] Checkpoint --- runMe.go | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 runMe.go diff --git a/runMe.go b/runMe.go deleted file mode 100644 index e69de29..0000000 From af03983898046743c0661bef0f69d8265244e917 Mon Sep 17 00:00:00 2001 From: Andrew Cuga Date: Fri, 2 Sep 2022 15:24:48 -0400 Subject: [PATCH 03/10] Checkpoint --- src/identicon.png | Bin 0 -> 763 bytes src/runMe.go | 68 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 src/identicon.png create mode 100644 src/runMe.go diff --git a/src/identicon.png b/src/identicon.png new file mode 100644 index 0000000000000000000000000000000000000000..a12bfd3a4aa47aea7821e5de1cfa673903d7d1c1 GIT binary patch literal 763 zcmZ9Kv1*l35QHb%NFcpfh)oI`LG}p@H^d8aiIN8plzjpVYYC#I<*^F|&93auHtvDqZo1N)#vXZq$!p@kKM**C8Zn@u z6;tY~i2rskN95hVT?OV#!)aIJqR)<$QaYe@sOa-a3CD2H*SGuvK< z2YGpzRHi$C8V?@?Vp`1kqikKNt*MsJ++ Y`@nhkTYK+6y*Zrb_IS7Xa`f`uf7Vo9j{pDw literal 0 HcmV?d00001 diff --git a/src/runMe.go b/src/runMe.go new file mode 100644 index 0000000..b7f4c21 --- /dev/null +++ b/src/runMe.go @@ -0,0 +1,68 @@ +package main + +import ( + "crypto/sha512" + "encoding/base64" + "fmt" + "image" + "image/color/palette" + "image/png" + "os" +) + +func main() { + // make a hash of the personal info + userHash := getHash() + fmt.Println("The hashed value is: ", userHash) + + // bw := []color.Color{color.Black, color.White} + // fmt.Println(bw) + + outputImage := image.NewPaletted( + image.Rect(0, 0, 100, 200), + palette.WebSafe, + ) + writeImage(outputImage) + //outputPath := time.Now().Format("01-02-2006 15:04:05") + ".png" + + // TODO: + // make an image of the hash of the IP address + // save to file? +} +func writeImage(outputImage *image.Paletted) { + + outputPath := "identicon.png" + out, err := os.Create(outputPath) + if err != nil { + fmt.Println("Could not create image") + return + } + png.Encode(out, outputImage) + out.Close() + fmt.Println("identicon written to ", outputPath) +} +func getHash() string { + // get a string (eg IP address, email) + fmt.Println("Enter Your Personal Information: ") + var personalInfo string + fmt.Scanln(&personalInfo) + + hasher := sha512.New() + bv := []byte(personalInfo) + hasher.Write(bv) + + sha := base64.URLEncoding.EncodeToString(hasher.Sum(nil)) + return sha +} + +/** +import ( + "image" +) +func main() { + myImg := image.NewRGBA(image.Rect(0, 0, 12, 6)) + out, err := os.Create("cat.png") + png.Encode(out, myImg) + out.Close() +} +*/ From 46d455e39eaf78608ae900b930e4342a8941f922 Mon Sep 17 00:00:00 2001 From: Andrew Cuga Date: Fri, 2 Sep 2022 15:27:34 -0400 Subject: [PATCH 04/10] Removing image --- .gitignore | 1 + src/identicon.png | Bin 763 -> 104 bytes src/runMe.go | 11 ++++++----- 3 files changed, 7 insertions(+), 5 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..57dfb3f --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +src/identicon.png diff --git a/src/identicon.png b/src/identicon.png index a12bfd3a4aa47aea7821e5de1cfa673903d7d1c1..e46560893d1edfed1d84dab21d34ddfec907293a 100644 GIT binary patch delta 86 zcmey(nlV8lfRUMjfkEZdwI(3N7T^=&3Z(!4|G)I+{JTJwtfz}(NX48t2N-#Qyh94V jmv1}H&dkuTfgPlZ>jMMhK0cLfpcI3rtDnm{r-UW|+Lj!e literal 763 zcmZ9Kv1*l35QHb%NFcpfh)oI`LG}p@H^d8aiIN8plzjpVYYC#I<*^F|&93auHtvDqZo1N)#vXZq$!p@kKM**C8Zn@u z6;tY~i2rskN95hVT?OV#!)aIJqR)<$QaYe@sOa-a3CD2H*SGuvK< z2YGpzRHi$C8V?@?Vp`1kqikKNt*MsJ++ Y`@nhkTYK+6y*Zrb_IS7Xa`f`uf7Vo9j{pDw diff --git a/src/runMe.go b/src/runMe.go index b7f4c21..580baec 100644 --- a/src/runMe.go +++ b/src/runMe.go @@ -5,7 +5,7 @@ import ( "encoding/base64" "fmt" "image" - "image/color/palette" + "image/color" "image/png" "os" ) @@ -15,13 +15,13 @@ func main() { userHash := getHash() fmt.Println("The hashed value is: ", userHash) - // bw := []color.Color{color.Black, color.White} - // fmt.Println(bw) - + //hashSize := len(userHash) // 88 for sha 512 + bw := []color.Color{color.Black, color.White} outputImage := image.NewPaletted( image.Rect(0, 0, 100, 200), - palette.WebSafe, + bw, ) + writeImage(outputImage) //outputPath := time.Now().Format("01-02-2006 15:04:05") + ".png" @@ -29,6 +29,7 @@ func main() { // make an image of the hash of the IP address // save to file? } + func writeImage(outputImage *image.Paletted) { outputPath := "identicon.png" From 20d32fac87f69119261e289c3ac16792f7fdad6c Mon Sep 17 00:00:00 2001 From: Andrew Cuga Date: Fri, 2 Sep 2022 16:09:53 -0400 Subject: [PATCH 05/10] Working --- src/identicon.png | Bin 104 -> 103 bytes src/runMe.go | 46 ++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/identicon.png b/src/identicon.png index e46560893d1edfed1d84dab21d34ddfec907293a..acdcbefd9e4430b32b05ba58a3aacc84a1fa4db8 100644 GIT binary patch delta 89 zcmc~uSL_V%bLZuf;$mQ6;Pv!y2?Ej_Ak4@NB>x@MIWbXD)KJFL#WAE}PSS>yfaD43 n7Uvcm`Ou#z+ib4D!0`XS=p*L%{`TZ6K=llsu6{1-oD!M<`lcGz delta 90 zcmYe#Q0xrwbLZuf;$mQ6;Pv!y2?Ek7KzxFc8Az&py4EyNQPfD*)5S5QV$PcbjJ!b6 oLkho_Z#&M;%+Roboq^&1f36Pzopr0R00P`Tzg` diff --git a/src/runMe.go b/src/runMe.go index 580baec..81c45ce 100644 --- a/src/runMe.go +++ b/src/runMe.go @@ -6,6 +6,7 @@ import ( "fmt" "image" "image/color" + "image/draw" "image/png" "os" ) @@ -13,21 +14,49 @@ import ( func main() { // make a hash of the personal info userHash := getHash() - fmt.Println("The hashed value is: ", userHash) + fmt.Println("The hashed value is: ", base64.URLEncoding.EncodeToString(userHash)) - //hashSize := len(userHash) // 88 for sha 512 + // 64 + hashSize := len(userHash) + gridWidth := 8 bw := []color.Color{color.Black, color.White} outputImage := image.NewPaletted( - image.Rect(0, 0, 100, 200), + image.Rect(0, 0, gridWidth, gridWidth), bw, ) + for i := 0; i < hashSize; i++ { + hashValue := userHash[i] + myColor := bw[hashValue%2] + + //row = index % width + // i ==10 corresponds to row 1, cell 2 + x := i / gridWidth + y := i % gridWidth + fmt.Println(x, y, myColor, hashValue) + + start := image.Point{x, y} + end := image.Point{x + 1, y + 1} + rectangle := image.Rectangle{start, end} + draw.Draw(outputImage, rectangle, &image.Uniform{myColor}, image.Point{}, draw.Src) + } + + // for j := 0; j < hashSize; j++ { + // // for each space in the image, draw a pixel + // hashValue := userHash[(hashSize-1)%(i+j+1)] + // myColor := bw[hashValue%2] + + // start := image.Point{i, j} + // end := image.Point{i + 20, j + 20} + // rectangle := image.Rectangle{start, end} + // draw.Draw(outputImage, rectangle, &image.Uniform{myColor}, image.Point{}, draw.Src) + // } + // } + writeImage(outputImage) - //outputPath := time.Now().Format("01-02-2006 15:04:05") + ".png" // TODO: // make an image of the hash of the IP address - // save to file? } func writeImage(outputImage *image.Paletted) { @@ -42,7 +71,7 @@ func writeImage(outputImage *image.Paletted) { out.Close() fmt.Println("identicon written to ", outputPath) } -func getHash() string { +func getHash() []byte { // get a string (eg IP address, email) fmt.Println("Enter Your Personal Information: ") var personalInfo string @@ -52,8 +81,9 @@ func getHash() string { bv := []byte(personalInfo) hasher.Write(bv) - sha := base64.URLEncoding.EncodeToString(hasher.Sum(nil)) - return sha + //sha := base64.URLEncoding.EncodeToString(hasher.Sum(nil)) + //return sha + return hasher.Sum(nil) } /** From 654610a35f38fe9a46621d62e047edd065eae331 Mon Sep 17 00:00:00 2001 From: Andrew Cuga Date: Fri, 2 Sep 2022 16:12:35 -0400 Subject: [PATCH 06/10] Working --- src/runMe.go | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/src/runMe.go b/src/runMe.go index 81c45ce..b39a724 100644 --- a/src/runMe.go +++ b/src/runMe.go @@ -16,12 +16,13 @@ func main() { userHash := getHash() fmt.Println("The hashed value is: ", base64.URLEncoding.EncodeToString(userHash)) - // 64 + // 64 for sha 256 hashSize := len(userHash) gridWidth := 8 + scale := 10 bw := []color.Color{color.Black, color.White} outputImage := image.NewPaletted( - image.Rect(0, 0, gridWidth, gridWidth), + image.Rect(0, 0, gridWidth*scale, gridWidth*scale), bw, ) @@ -29,30 +30,18 @@ func main() { hashValue := userHash[i] myColor := bw[hashValue%2] - //row = index % width + // row = index % width // i ==10 corresponds to row 1, cell 2 x := i / gridWidth y := i % gridWidth fmt.Println(x, y, myColor, hashValue) - start := image.Point{x, y} - end := image.Point{x + 1, y + 1} + start := image.Point{x * scale, y * scale} + end := image.Point{x + scale, y + scale} rectangle := image.Rectangle{start, end} draw.Draw(outputImage, rectangle, &image.Uniform{myColor}, image.Point{}, draw.Src) } - // for j := 0; j < hashSize; j++ { - // // for each space in the image, draw a pixel - // hashValue := userHash[(hashSize-1)%(i+j+1)] - // myColor := bw[hashValue%2] - - // start := image.Point{i, j} - // end := image.Point{i + 20, j + 20} - // rectangle := image.Rectangle{start, end} - // draw.Draw(outputImage, rectangle, &image.Uniform{myColor}, image.Point{}, draw.Src) - // } - // } - writeImage(outputImage) // TODO: From aaebbd81c2ec0233214c0d886cb08aafbc00b167 Mon Sep 17 00:00:00 2001 From: Andrew Cuga Date: Fri, 2 Sep 2022 16:13:23 -0400 Subject: [PATCH 07/10] Working --- src/identicon.png | Bin 103 -> 100 bytes src/runMe.go | 7 +++---- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/identicon.png b/src/identicon.png index acdcbefd9e4430b32b05ba58a3aacc84a1fa4db8..7811323060b8f8f12102cdbf502266cdf722ef2c 100644 GIT binary patch delta 86 zcmYdKQS1!xbLZuf;$mQ6;Pv!y2?EjqAk4@NB)8m8csEf|R8P{=#WAE}PSOJXAN39A l#QK^74Q4PXFfjc8@3@QcfcK8?3xJ9lJYD@<);T3K0RYBe8ZH0; delta 89 zcmYdESL_V%bLZuf;$mQ6;Pv!y2?Ej_Ak4@NB>x@MIWbXD)KJFL#WAE}PSS>yfaD43 n7Uvcm`Ou#z+ib4D!0`XS=p*L%{`TZ6K=llsu6{1-oD!M<`9d1f diff --git a/src/runMe.go b/src/runMe.go index b39a724..e09096a 100644 --- a/src/runMe.go +++ b/src/runMe.go @@ -19,10 +19,9 @@ func main() { // 64 for sha 256 hashSize := len(userHash) gridWidth := 8 - scale := 10 bw := []color.Color{color.Black, color.White} outputImage := image.NewPaletted( - image.Rect(0, 0, gridWidth*scale, gridWidth*scale), + image.Rect(0, 0, gridWidth, gridWidth), bw, ) @@ -36,8 +35,8 @@ func main() { y := i % gridWidth fmt.Println(x, y, myColor, hashValue) - start := image.Point{x * scale, y * scale} - end := image.Point{x + scale, y + scale} + start := image.Point{x, y} + end := image.Point{x + 1, y + 1} rectangle := image.Rectangle{start, end} draw.Draw(outputImage, rectangle, &image.Uniform{myColor}, image.Point{}, draw.Src) } From af8e6d562108e263a3762713c2f41d5cff712f67 Mon Sep 17 00:00:00 2001 From: Andrew Cuga Date: Fri, 2 Sep 2022 16:20:35 -0400 Subject: [PATCH 08/10] Working --- src/identicon.png | Bin 100 -> 0 bytes src/runMe.go | 22 +++++----------------- 2 files changed, 5 insertions(+), 17 deletions(-) delete mode 100644 src/identicon.png diff --git a/src/identicon.png b/src/identicon.png deleted file mode 100644 index 7811323060b8f8f12102cdbf502266cdf722ef2c..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 100 zcmeAS@N?(olHy`uVBq!ia0vp^0U*rC3?#SQPk0BU*aCb)T!Hle|NocXoPQU{lJs Date: Fri, 2 Sep 2022 16:34:22 -0400 Subject: [PATCH 09/10] Dressing up comments --- README.md | 6 ++++++ src/runMe.go | 39 ++++++++++++++++++++++++++++----------- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index bba8994..ab09268 100644 --- a/README.md +++ b/README.md @@ -21,3 +21,9 @@ gravatar account attached. * http://golang.org/pkg/image/ * http://en.wikipedia.org/wiki/Identicon * http://haacked.com/archive/2007/01/22/Identicons_as_Visual_Fingerprints.aspx/ + +## To run the solution + +* From the src directory, type `go run .` +* When prompted, enter the unique personal information (email, IP address) +* Files are outputted in the same working directory, under the name `identicon.png` diff --git a/src/runMe.go b/src/runMe.go index f013295..dfb9505 100644 --- a/src/runMe.go +++ b/src/runMe.go @@ -11,11 +11,26 @@ import ( "os" ) +// main +// +// from the src directory, type 'go run .' +// when prompted, enter the unique personal information (email, IP address) +// files are outputted in the same working directory, under the name identicon.png func main() { // make a hash of the personal info userHash := getHash() - fmt.Println("The hashed value is: ", base64.URLEncoding.EncodeToString(userHash)) + // create the image + outputImage := createImage(userHash) + + // for the purposes of this PR, write image to the disk + writeImage(outputImage) +} + +// createImage +// +// Given a user hash, creates an associated image +func createImage(userHash []byte) *image.Paletted { // 64 for sha 256 hashSize := len(userHash) gridWidth := 8 @@ -40,13 +55,12 @@ func main() { rectangle := image.Rectangle{start, end} draw.Draw(outputImage, rectangle, &image.Uniform{myColor}, image.Point{}, draw.Src) } - - writeImage(outputImage) - - // TODO: - // make an image of the hash of the IP address + return outputImage } +// writeImage +// +// Writes out the image to a hard-coded location on disk, named identicon.png func writeImage(outputImage *image.Paletted) { outputPath := "identicon.png" @@ -56,9 +70,13 @@ func writeImage(outputImage *image.Paletted) { return } png.Encode(out, outputImage) - out.Close() + defer out.Close() fmt.Println("identicon written to ", outputPath) } + +// getHash +// +// Returns the hash from a user func getHash() []byte { // get a string (eg IP address, email) fmt.Println("Enter Your Personal Information: ") @@ -68,8 +86,7 @@ func getHash() []byte { hasher := sha512.New() bv := []byte(personalInfo) hasher.Write(bv) - - //sha := base64.URLEncoding.EncodeToString(hasher.Sum(nil)) - //return sha - return hasher.Sum(nil) + userHash := hasher.Sum(nil) + fmt.Println("The hashed value is: ", base64.URLEncoding.EncodeToString(userHash)) + return userHash } From 1cfd8b4c22d8cef8b27174f2cbe80d07993d4b2e Mon Sep 17 00:00:00 2001 From: Andrew Cuga Date: Fri, 2 Sep 2022 16:50:29 -0400 Subject: [PATCH 10/10] Fixing comment --- src/runMe.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/runMe.go b/src/runMe.go index dfb9505..d2671b3 100644 --- a/src/runMe.go +++ b/src/runMe.go @@ -31,7 +31,7 @@ func main() { // // Given a user hash, creates an associated image func createImage(userHash []byte) *image.Paletted { - // 64 for sha 256 + // 64 for sha 512. hashSize := len(userHash) gridWidth := 8 scale := 40