summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSaumit Dinesan <justsaumit@protonmail.com>2023-09-05 11:34:21 +0530
committerSaumit Dinesan <justsaumit@protonmail.com>2023-09-05 11:34:21 +0530
commit3833e32b70f10e54bba47c89f4aea78e828ea5b0 (patch)
tree83673ebbb155a5bdb9c7ccbec9b88d5b3846e7af
parent1b9421884f3d403e3406e61cee742bd03d3a0fa5 (diff)
Adding Blake2 Hashing with /hasher endpoint
-rw-r--r--go.mod6
-rw-r--r--go.sum6
-rw-r--r--hasher/blake2-hash_generator.go30
-rw-r--r--message-orig.txt1
-rw-r--r--server.go15
5 files changed, 54 insertions, 4 deletions
diff --git a/go.mod b/go.mod
index a0fd901..cf63ed7 100644
--- a/go.mod
+++ b/go.mod
@@ -9,8 +9,8 @@ require (
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
- golang.org/x/crypto v0.11.0 // indirect
+ golang.org/x/crypto v0.12.0 // indirect
golang.org/x/net v0.12.0 // indirect
- golang.org/x/sys v0.10.0 // indirect
- golang.org/x/text v0.11.0 // indirect
+ golang.org/x/sys v0.11.0 // indirect
+ golang.org/x/text v0.12.0 // indirect
)
diff --git a/go.sum b/go.sum
index 412a24c..69040e2 100644
--- a/go.sum
+++ b/go.sum
@@ -21,6 +21,8 @@ github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQ
github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
golang.org/x/crypto v0.11.0 h1:6Ewdq3tDic1mg5xRO4milcWCfMVQhI4NkqWWvqejpuA=
golang.org/x/crypto v0.11.0/go.mod h1:xgJhtzW8F9jGdVFWZESrid1U1bjeNy4zgy5cRr/CIio=
+golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk=
+golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw=
golang.org/x/net v0.12.0 h1:cfawfvKITfUsFCeJIHJrbSxpeu/E81khclypR0GVT50=
golang.org/x/net v0.12.0/go.mod h1:zEVYFnQC7m/vmpQFELhcD1EWkZlX69l4oqgmer6hfKA=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
@@ -30,8 +32,12 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
+golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.11.0 h1:LAntKIrcmeSKERyiOh0XMV39LXS8IE9UL2yP7+f5ij4=
golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
+golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
+golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
diff --git a/hasher/blake2-hash_generator.go b/hasher/blake2-hash_generator.go
new file mode 100644
index 0000000..4d85a58
--- /dev/null
+++ b/hasher/blake2-hash_generator.go
@@ -0,0 +1,30 @@
+package hasher
+
+import (
+ "fmt"
+ "golang.org/x/crypto/blake2b"
+ "io"
+ "os"
+)
+
+func CalculateBLAKE2Hash(filePath string) (string, error) {
+ file, err := os.Open(filePath)
+ if err != nil {
+ return "", err
+ }
+ defer file.Close()
+
+ hash, err := blake2b.New256(nil)
+ if err != nil {
+ return "", err
+ }
+
+ _, err = io.Copy(hash, file)
+ if err != nil {
+ return "", err
+ }
+
+ hashBytes := hash.Sum(nil)
+ hashString := fmt.Sprintf("%x", hashBytes)
+ return hashString, nil
+}
diff --git a/message-orig.txt b/message-orig.txt
new file mode 100644
index 0000000..43be410
--- /dev/null
+++ b/message-orig.txt
@@ -0,0 +1 @@
+This is a sample message.
diff --git a/server.go b/server.go
index d610b92..cd2978d 100644
--- a/server.go
+++ b/server.go
@@ -7,6 +7,7 @@ import (
"fmt"
"github.com/justsaumit/go-fic-api/idgen"
+ "github.com/justsaumit/go-fic-api/hasher"
)
type HelloWorld struct {
@@ -19,6 +20,10 @@ func main() {
e.GET("/hello/:name", GreetingsWithParams)
e.GET("/hello-queries", GreetingsWithQuery)
e.GET("/genid", GenerateIDHandler)
+ e.GET("/hasher", hasherHandler)
+ e.GET("/", func(c echo.Context) error {
+ return c.String(http.StatusOK, "Hello, World!")
+ })
e.Logger.Fatal(e.Start(":3000"))
}
@@ -42,7 +47,6 @@ func GreetingsWithQuery(c echo.Context) error {
})
}
-
func GenerateIDHandler(c echo.Context) error {
id := idgen.GenerateID()
//Print the generated ID to the console.
@@ -52,3 +56,12 @@ func GenerateIDHandler(c echo.Context) error {
// Message: "Generated ID: " + id,
//})
}
+
+func hasherHandler(c echo.Context) error {
+ filePath := "./message-orig.txt"
+ hash, err := hasher.CalculateBLAKE2Hash(filePath)
+ if err != nil {
+ return c.String(http.StatusInternalServerError, "Error calculating hash")
+ }
+ return c.String(http.StatusOK, fmt.Sprintf("BLAKE2b hash of %s: %s\n", filePath, hash))
+}