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 }