summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSaumit Dinesan <justsaumit@protonmail.com>2023-11-28 19:22:05 +0530
committerSaumit Dinesan <justsaumit@protonmail.com>2023-11-28 19:22:05 +0530
commit4b8984bf085b8e4e5626d786f07ca4c0af63667e (patch)
tree9e6203a24f92bbe81a7f94a57272336aa933a612
parent6461eba5e8df73aab7fd325e026dac6fb7e77524 (diff)
handlers - Adding VerifyHash handler
-rw-r--r--handlers/handlers.go52
-rw-r--r--server.go3
2 files changed, 53 insertions, 2 deletions
diff --git a/handlers/handlers.go b/handlers/handlers.go
index fd5395d..037d6df 100644
--- a/handlers/handlers.go
+++ b/handlers/handlers.go
@@ -34,17 +34,19 @@ func init() {
// AddHash handles the file upload, store and response
func AddHash(c echo.Context) error {
- file, err := c.FormFile("file")
+ file, err := c.FormFile("FileInput")
if err != nil {
log.Println("Failed to bind request:", err)
return c.JSON(http.StatusBadRequest, map[string]string{"message": "Invalid request"})
}
+ // Open the file stream
src, err := file.Open()
if err != nil {
log.Println("Failed to Open File", err)
return err
}
+ // Close the file stream on function exit
defer src.Close()
id, err := utils.GenerateID()
@@ -71,3 +73,51 @@ func AddHash(c echo.Context) error {
log.Println("Hash added successfully to the database")
return c.JSON(http.StatusOK, data)
}
+
+// VerifyHash handles the file verification and response
+
+func VerifyHash(c echo.Context) error {
+ // Extract the ID from the request
+ id := c.FormValue("idInput")
+
+ // Handle file upload
+ file, err := c.FormFile("FileInput")
+ if err != nil {
+ log.Println("Failed to bind request:", err)
+ return c.JSON(http.StatusBadRequest, map[string]string{"message": "Invalid request"})
+ }
+
+ // Open the file stream
+ src, err := file.Open()
+ if err != nil {
+ log.Println("Failed to Open File", err)
+ return err
+ }
+ // Close the file stream on function exit
+ defer src.Close()
+
+ // Generate hash of the uploaded file
+ hash, err := utils.GenerateHash(src)
+ if err != nil {
+ log.Println("Failed to Generate Hash", err)
+ }
+
+ // Retrieve the stored hash from the database
+ var storedHash string
+ err = db.QueryRow("SELECT HashValue FROM hashes WHERE ID = ?", id).Scan(&storedHash)
+ if err != nil {
+ if err == sql.ErrNoRows {
+ return c.JSON(http.StatusNotFound, map[string]string{"message": "ID not found"})
+ }
+ log.Println("Failed to query database:", err)
+ return c.JSON(http.StatusInternalServerError, map[string]string{"message": "Failed to retrieve stored hash"})
+ }
+
+ // Compare the generated hash with the stored hash
+ if hash != storedHash {
+ log.Println("Hash verification failed")
+ return c.JSON(http.StatusUnauthorized, map[string]string{"message": "Hash verification failed"})
+ }
+ log.Println("Hash verified successfully")
+ return c.JSON(http.StatusOK, map[string]string{"message": "Hash verified successfully"})
+}
diff --git a/server.go b/server.go
index 1d73a0f..6720b41 100644
--- a/server.go
+++ b/server.go
@@ -8,6 +8,7 @@ import (
func main() {
e := echo.New()
e.POST("/upload", handlers.AddHash)
- //e.POST("/verify", handlers.VerifyHash)
+ e.POST("/verify", handlers.VerifyHash)
+ //e.Logger.Fatal(e.Start(":3000"))
e.Logger.Fatal(e.StartTLS(":3000", "/etc/letsencrypt/live/draconyan.xyz/fullchain.pem", "/etc/letsencrypt/live/draconyan.xyz/privkey.pem"))
}