From 2ff4ee36fc85a3ad6edd36ba48d6cafd2696577a Mon Sep 17 00:00:00 2001 From: n0rrman Date: Sun, 26 May 2024 14:56:02 +0700 Subject: [PATCH] utils.go removed and merged into appropriate files: 'bad practice' --- README.md | 2 -- hub/hub.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ hub/utils.go | 51 --------------------------------------------------- 3 files changed, 46 insertions(+), 53 deletions(-) delete mode 100644 hub/utils.go diff --git a/README.md b/README.md index 27de0dc..0f4d975 100644 --- a/README.md +++ b/README.md @@ -36,8 +36,6 @@ Subhub is a small WebSub hub demo implemented in Go using echo, http/net, and Po **db.go** holds all Postgres logic: connecting to the database, creating tables, and making queries. All SQL and database code is contained within this file. -**utils.go** contains a few general utility functions, such as random hex string generator, sha256 hasher, and random advice fetcher. - ### Flow The following diagrams shows the main action flow through the hub, from client to database. The three columns represent the different code files and how they interract. diff --git a/hub/hub.go b/hub/hub.go index b9a4a68..17a9d97 100644 --- a/hub/hub.go +++ b/hub/hub.go @@ -2,9 +2,15 @@ package main import ( "bytes" + "crypto/hmac" + "crypto/rand" + "crypto/sha256" + "encoding/hex" + "encoding/json" "fmt" "io" "net/http" + "os" "time" "github.com/labstack/echo/v4" @@ -203,3 +209,43 @@ func sendContent(url string, secret string, topic string, content string) bool { // Returns true of succesful, otherwise false return resp.StatusCode == 200 } + +// Random hex string generator +// +// Returns a random 64 byte hex string +func generateChallenge() string { + buf := make([]byte, 64) + _, err := rand.Read(buf) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + c := hex.EncodeToString(buf) + return c +} + +// SHA256 hasher of provided content using secret as key +// +// Returns sha256 hash for content using provided secret +func getHash(content []byte, secret string) string { + key := []byte(secret) + h := hmac.New(sha256.New, key) + h.Write(content) + hash := hex.EncodeToString(h.Sum(nil)) + return hash +} + +// Random advice generator +// +// Fetches random advice +// Returns random advice from https://api.adviceslip.com +func getRandomAdvice() string { + resp, _ := http.Get("https://api.adviceslip.com/advice") + + var data map[string]interface{} + json.NewDecoder(resp.Body).Decode(&data) + advice, _ := data["slip"].(map[string]interface{})["advice"].(string) + + defer resp.Body.Close() + return advice +} diff --git a/hub/utils.go b/hub/utils.go deleted file mode 100644 index e26869f..0000000 --- a/hub/utils.go +++ /dev/null @@ -1,51 +0,0 @@ -package main - -import ( - "crypto/hmac" - "crypto/rand" - "crypto/sha256" - "encoding/hex" - "encoding/json" - "fmt" - "net/http" - "os" -) - -// Utility function: random hex string generator -// -// Returns a random 64 byte hex string -func generateChallenge() string { - buf := make([]byte, 64) - _, err := rand.Read(buf) - if err != nil { - fmt.Println(err) - os.Exit(1) - } - c := hex.EncodeToString(buf) - return c -} - -// Utility function: sha256 hash content with key -// -// Returns sha256 hash for content using provided secret -func getHash(content []byte, secret string) string { - key := []byte(secret) - h := hmac.New(sha256.New, key) - h.Write(content) - hash := hex.EncodeToString(h.Sum(nil)) - return hash -} - -// Utility function: fetches random advice -// -// Returns random advice from https://api.adviceslip.com -func getRandomAdvice() string { - resp, _ := http.Get("https://api.adviceslip.com/advice") - - var data map[string]interface{} - json.NewDecoder(resp.Body).Decode(&data) - advice, _ := data["slip"].(map[string]interface{})["advice"].(string) - - defer resp.Body.Close() - return advice -}