From d82d07628b90dd004d94acf14592b06373af20c8 Mon Sep 17 00:00:00 2001 From: milarin Date: Thu, 16 Feb 2023 14:36:52 +0100 Subject: [PATCH] send hashes in JSON format --- hash.go | 13 +++++++++++++ main.go | 7 ++++--- 2 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 hash.go diff --git a/hash.go b/hash.go new file mode 100644 index 0000000..ecaff8d --- /dev/null +++ b/hash.go @@ -0,0 +1,13 @@ +package main + +import ( + "encoding/hex" +) + +type Hash struct { + Hash string `json:"hash"` +} + +func NewHash(hash []byte) *Hash { + return &Hash{Hash: hex.EncodeToString(hash)} +} diff --git a/main.go b/main.go index d8232e7..4075d0a 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,6 @@ package main import ( "bytes" "crypto/sha512" - "encoding/hex" "encoding/json" "fmt" "io" @@ -60,8 +59,9 @@ func GetAllHashHandler(w http.ResponseWriter, r *http.Request) { return } + w.Header().Add("Content-Type", "application/json") hash := sha512.Sum512(b.Bytes()) - if _, err := fmt.Fprint(w, hex.EncodeToString(hash[:])); err != nil { + if err := json.NewEncoder(w).Encode(NewHash(hash[:])); err != nil { InternalServerError(w, err) return } @@ -113,7 +113,8 @@ func GetFileHashHandler(w http.ResponseWriter, r *http.Request) { return } - if _, err := fmt.Fprint(w, hex.EncodeToString(hasher.Sum(nil))); err != nil { + w.Header().Add("Content-Type", "application/json") + if err := json.NewEncoder(w).Encode(NewHash(hasher.Sum(nil))); err != nil { InternalServerError(w, err) return }